?
Solved

PHP in a 'require_once' php file

Posted on 2005-04-15
13
Medium Priority
?
215 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:alexlindley
13 Comments
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789307
Hi Alex, can you write/copy paste what kind of errors does it gives?
0
 

Author Comment

by:alexlindley
ID: 13789604
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
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789629
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
alextr2003fr earned 1050 total points
ID: 13789643
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
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789655
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
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789699
try to change $movie_name,$movie_year,$movie_rating names in the other one to see if it works.
0
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789736
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
 
LVL 6

Expert Comment

by:alextr2003fr
ID: 13789782
also check if include_once "conn.inc.php"; will give better results
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 13789974
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
 
LVL 7

Expert Comment

by:Promethyl
ID: 13790127

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
 

Author Comment

by:alexlindley
ID: 13790590
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
 
LVL 7

Expert Comment

by:aib_42
ID: 13795854
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
 

Author Comment

by:alexlindley
ID: 13800637
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question