PHP in a 'require_once' php file

Hi,
This problem may sound odd but i have a site about movies and actors which works fine apart from one functionality i am trying to get to work.
The problem is that i have a top 100 movies in order of the highest average rating. But on the front page i want to have a small area on my left menu (which is in a separate 'leftmenu.php' file) where it displays the top 6 of the top 100. So i thought i would be able to copy the query and code from the 'top100.php' page which shows them in a table and just alter it so it shows the top 6 in list format.

However the first time i did this i used the following code;

    <?php
    $query = "SELECT * FROM Movie WHERE AvRating IS NOT NULL ORDER BY AvRating DESC LIMIT 0,6";
                 
    $result = mysql_db_query("MRDB", $query)
      or die(mysql_error());
   
    if ($result) {
          echo "<ul>";
      while ($row = mysql_fetch_array($result)) {
            $movie_id = $row['MovieID'];
        $movie_name = $row['MovieName'];
        $movie_rating = $row['AvRating'];
        echo "<li>&nbsp;<a href='movie_details.php?movie_id=$movie_id'>$movie_name</a> ($movie_rating)</li>";
      }
      echo "</ul>";
    } else { echo "problems with top 100"; }
          ?>

This worked great, but everything that loaded up after this code seemed to have errors in, and basically half of my site didnt work. ( the leftmenu.php is called at the top of every page after the <body> tag with <?php require_once 'leftmenu.php'; ?> )

So i tried altering the code to the following to see if it would have any change;

   <?php
   
   $top_query = "SELECT * FROM Movie WHERE AvRating IS NOT NULL ORDER BY AvRating DESC LIMIT 0,6";              
   $top_result = mysql_db_query("MRDB", $top_query)
      or die(mysql_error());
   
     while ($row = mysql_fetch_array($top_result)) {
        $movie_id = $row['MovieID'];
        $movie_name = $row['MovieName'];
        $movie_rating = $row['AvRating'];
       }

      $i = 0;
      $top_details = '';
      while ($i<sizeof($movie_id)) {
        $top_details .=<<<EOD
     
      <li>&nbsp;<a href='movie_details.php?movie_id=$movie_id'>$movie_name</a> ($movie_rating)</li>
     
      EOD;
        $i++;
      }
     ?>

<?php echo "$top_details"; ?>

But once again i had errors and the leftmenu would even load.
I have the following at the top of every page
<?php
include "conn.inc.php";
?>
so i dont think it is a connection problem, is it something to do with conflicting php?
Does anyone know of a more efficient way of doing this?
Im lost on ideas.

Thanks very much for your time and help.
Alex
alexlindleyAsked:
Who is Participating?
 
alextr2003frConnect With a Mentor Commented:
Also maybe the names of variables mix within the 2 include files, for example you add the result to an array
wich is already written in the other include. So maybe try to just use different names of variables in another one or re-inialise them before you use if you do not want to use new variables.
0
 
alextr2003frCommented:
Hi Alex, can you write/copy paste what kind of errors does it gives?
0
 
alexlindleyAuthor Commented:
Hi,
Im getting this error;
"Fatal error: [] operator not supported for strings in /drive2/fpgshttpd/moviereviews/top100.php on line 24"

However i've commented the top100 function in the leftnmenu out at the moment, so no errors should be seen on the site now.
Im just trying to find a safe way of getting the top6 of the top100 in the left hand pane! so frustrating!

Thanks for the help,

Alex
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
alextr2003frCommented:
check if you do not loop more than it should be for example if you have 6 elements and the loop is trying to go by more than 6 it will make an error.
echo "<li>&nbsp;<a href='movie_details.php?movie_id=$movie_id'>$movie_name</a> ($movie_rating)</li>";
should actually be
echo "<li>&nbsp;<a href=\"movie_details.php?movie_id=$movie_id\">$movie_name</a> ($movie_rating)</li>";
0
 
alextr2003frCommented:
can you copy paste the code at line 24? that error seems to come from the fact that it is trying to put an array into a string variable or something similar, try to check if there is variable type compatibilities also.
0
 
alextr2003frCommented:
try to change $movie_name,$movie_year,$movie_rating names in the other one to see if it works.
0
 
alextr2003frCommented:
try something like this instead :
$movie_name = array();
$movie_year = array();
$movie_rating = array();
while ($row = mysql_fetch_array($top_result)) {
  $top_flag =1;
  array_push($movie_name, $row['MovieName']);
  array_push($movie_year,$row['MovieYear']);
  array_push($movie_rating,$row['AvRating']);
}
0
 
alextr2003frCommented:
also check if include_once "conn.inc.php"; will give better results
0
 
RoonaanCommented:
Although this might sound awfully stupid, but in my opinion I don't think editing leftmenu.php would fix the syntax error in top100.php.

Best would be like alextr2003fr be to show us code from the top100.php file.

-r-
0
 
PromethylCommented:

Before this line :
     while ($row = mysql_fetch_array($result)) {
Add:
     if (mysql_error()) echo mysql_error() ;

See if your query is showing an error.

Also...
$query = "SELECT * FROM Movie WHERE AvRating IS NOT NULL ORDER BY AvRating DESC LIMIT 0,6";
Might be told as :
$query = "SELECT * FROM Movie WHERE AvRating IS NOT NULL ORDER BY AvRating DESC LIMIT 6";

Disparing the queryset by 0 records makes little sense. (To me.)


0
 
alexlindleyAuthor Commented:
thanks for ur input not had chance to work on it properly yet, been really busy today.

Its weird because its effecting my whole site, its like it makes the whole site have the $movie_id = 16 becuase when i view movies every movie calls itself 'hitch' and when i edit movies from the administrator page Hitch always comes up in the edit title input box.

I really dont think its isolated on top100.php but the code where its getting the error is;

$top_query = "SELECT * FROM Movie WHERE AvRating IS NOT NULL ORDER BY AvRating DESC LIMIT 0,100";
                 
$top_result = mysql_db_query("MRDB", $top_query)
  or die(mysql_error());

while ($row = mysql_fetch_array($top_result)) {
      $top_flag =1;     //  <-------------------------Line 24
  $movie_name[] = $row['MovieName'];
  $movie_year[] = $row['MovieYear'];
  $movie_rating[] = $row['AvRating'];
}

$i = 0;
$top_details = '';
while ($i<sizeof($movie_rating)) {
      $j = $i+1;
  $top_details .=<<<EOD
  <tr>
    <td align="right">
      <b>$j.</b>
    </td>
    <td align="center">
      $movie_rating[$i]
    </td>
    <td>
      $movie_name[$i] ($movie_year[$i])
    </td>
  </tr>

EOD;
  $i++;
}


Ohh actually do you think it might be that in the 'leftmenu.php' ive not got the flag=1 ?
i'm gonna try adding another top_flag to the leftmenu code.
Also is it possible for these two arrarys to conflict in some way?
Does using $row for each row in the different while statements conflict? Im guessing not at all.

Anyway, i'll try that out.

Thanks,
Alex
0
 
aib_42Commented:
A good practice would be making leftmenu.php a script of functions, something that doesn't have any code or output of its own when just included. This would solve a lot of the scope issues as well as the check against including it twice:

<?php /* start of leftmenu.php */

/* Code here will get executed every time this file is included, which IMHO is a bad practice as include files should not have standalone code but just functions. This actually reminds me of VC++6, which let you have global code (outside of main() and any other function). When multiple object files with global code were linked together, it was impossible to tell which one would execute first. Actually, I think it was decided by the alphabetical order of the filenames... */

function leftmenu_head()
{
    echo "Welcome to the left menu!<table border=0><tr><td>";
}

function leftmenu_foot()
{
    echo "</td></tr>";
}

function printtop6($db_connection) /* might be required, or use global variables */
{
    global $db_connection; /* either this or above, obviously... and that is only if you need your connection resource */

    mysql_query("SELECT * ...", $db_connection);
    while(...) {
        echo $row[0].$row[1]...
...
}

?>
0
 
alexlindleyAuthor Commented:
Hi,
It was a problem with the variables, they were conflicting with others that are declared in the main pages, thus casuing all sorts of problems. Anyway i changed them and everything seems to work fine now.

Thanks for the help guys.
Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.