undefined variable

Hi

I had different parts (one php file for each part like navigation.php, banner.php, …) of my website and included these files in an index.php. Everything worked fine.

Now I’m trying to follow IPO, so I try to prepare the content of my website in rack.php and to display the content from index.php. The whole website is shown - until I enter the part I call “maincontent-header”. When I do this, variables from another website-part are not shown anymore. What can make this happen? I have no clue…

[php]<?php
$root = $_SERVER[‘DOCUMENT_ROOT’];
require_once ($root."/config.php");

 if (isset($_GET['site'])) :
     $current_site = $_GET['site'];
 else :
     $current_site = 'start';
 endif;

/***************************************************************************
Site-Header
***************************************************************************/

/***************************************************************************
Banner
***************************************************************************/

/***************************************************************************
Maincontent-Header, where it should be. If enabled, $content is undefined.
**************************************************************************/
/

$url_query = “SELECT title, titletext, title_imgname,
title_imgalttext, title_imgwidth FROM sites WHERE name=?”;

 $url_stmt = mysqli_stmt_init($verbindung);
 if (mysqli_stmt_prepare($url_stmt, $url_query)) :
     if (!$url_stmt) :
         die('mysqli error: '.mysqli_error($verbindung));
     endif;

     mysqli_stmt_bind_param($url_stmt, "s", $current_site);
     mysqli_stmt_execute($url_stmt);
     mysqli_stmt_store_result($url_stmt);
     mysqli_stmt_bind_result($url_stmt, $title, $titletext, 

$title_imgname, $title_imgalttext, $title_imgwidth);
mysqli_stmt_fetch($url_stmt);
mysqli_stmt_free_result($url_stmt);
(int) $maintitlewidth = (12-$title_imgwidth);

     //Prüfen, ob Logo angezeigt werden soll
     if(isset($title_imgname)) :
         $logo = '<img src="/images/'.$title_imgname.'" 

alt="’.$title_imgalttext.’">’;
else :
$logo = NULL;
endif;
endif;

 // Adminbutton ein- oder ausblenden
 if(isset($_SERVER['REMOTE_USER'])) :
     $mainheader_editbutton = '<div 

class=“mainheader_adminbutton”>’;
else :
$mainheader_editbutton = NULL;
endif;
/
/
**************************************************************************
Content
***************************************************************************/
//Alle verfügbaren Seiten werden hier eingetragen
$result = mysqli_query($verbindung, “SELECT name FROM sites”);
while ($row = mysqli_fetch_assoc($result)) :
$whitelist[] = $row[‘name’];
endwhile;
//print_r($whitelist);
//$whitelist = array(“start”, “gallery”, “kontakt”, “news”,
“fullscreen”, “gallery_img_add”, “gallery_img_delete”,
“gallery_img_edit”, “design”);

 if(isset($_GET["site"])) :
     //Wenn eine Seite angegeben wurde, wird sie $site zugewiesen
     $site = $_GET["site"];
 else:
     $site = "start";
 endif;

 //Prüfen, ob die Datei sich (nicht) in der Whitelist befindet
 if(!in_array($site, $whitelist)):
     //Nein, also Startseite anzeigen! Man könnte hier auch eine 

Fehlerseite eintragen.
$site = “start”;
endif;

 //Zusammensetzen des Dateinamens
 $filename = "pages/".$site.".php";

 //Prüfen, ob die erforderliche Datei existiert
 if(file_exists($filename)) :
     //Datei existiert und kann benutzt werden!
     //Output-Buffer starten
     ob_start();

     //Datei einbinden
     include($filename);

     //Output-Buffer beenden und Ergebnis speichern
     $content = ob_get_clean();
 else :
      $filename = "../pages/".$site.".php";
      if(file_exists($filename)) :
         ob_start();

         //Datei einbinden
         include($filename);

         //Output-Buffer beenden und Ergebnis speichern
         $content = ob_get_clean();
     else :
         $content = "Leider kann diese Seite nicht angezeigt werden!";
     endif;
 endif;

/***************************************************************************
Sidebar
***************************************************************************/

?>[/php]

index.php
[php]<?php
$root = $_SERVER[‘DOCUMENT_ROOT’];
require_once($root.’/config.php’);
include($root.’/mainheader.php’); //sollte eigentlich auch ohne
funktionieren, mainheader ist in rack.php enthalten. funktioniert aber
nicht, Seitentitel ($title) im Browserfenster kann nicht gelesen werden.
include($root.’/rack.php’);
//echo $title;

?>

<?=$sitehead?> <?=$banner?> <?php $root = $_SERVER['DOCUMENT_ROOT']; include($root.'/navigation.php'); ?>
     <div class="wrap">
         <main role="main">
             <section id="content">
                 <header class="cf">
                     <?=$mainheader_editbutton?>
                     <h2 class="grid12">
                         <?=$title?>
                     </h2>
                     <div class="grid8 headertext">
                         <p>
                             <?=$titletext?>
                         </p>
                     </div>

                     <div class="grid4 headerbild">
                         <?=$logo?>
                     </div>
                 </header>
                 <?=$content?>
             </section>
         </main>
         <?=$sidebar?>
     </div>
<?php include($root.'/footer.php'); ?>
 </body>
[/php]

Well, Michael, If this is all of the code as you say, I do not see where you assign the variables used
in the index.php file. As an example, you have " <?=$sitehead?> " to insert your site’s header
into the HTML tags. First, always use <?PHP echo $sitehead ?> instead. Some browsers
seem to have issues with the shorthand codes for this. Found that after several days of debugging
a site’s pages…

So, you insert whatever is inside of the $sitehead variable. But, in the all of the code you showed us,
there is no place where you set this variable. One thing that might be the issue is if you load the PHP
code from a file and then echo it out as text, any further PHP commands inside that will not be used.
(Echo’ing text from a PHP file does not make the text followed inline.) So, instead of actually printing
the text by using the echo command, you need to INCLUDE the text if you wish the PHP inside it to be
executed.

Hope that makes sense. If not ask further questions… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service