Simple Array Problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Simple Array Problem

Post by Rasta »

I'm trying to get information out of a table, and so far I can the first row of information, but I want to be able to get subsequent rows, if they exist.

The way my files are set up, I have a class that I've made called "page.php" and it's the layout of the page with navigation and html tables and all that. Then I have an include called db.php and it has the call to the database. Finally, I have the actual page (ex. "home.php" "about_us.php" etc.) Those pages grab the page.php, and the db.php - making a call to the db, and getting the relevant information to that page ($pageid). The home.php page has a $pageid of 1, and grabs all the rows of the table w/ pageid=1.

Right now I'm only getting one row of the table, and not the subsequent rows (and yes, right now in the db there are several rows with pageid=1).

I know my problem is that I need a conditional loop that will run and check how many rows have been returned, and then another loop that will increment through the returned rows. I don't know how or where to put this loop.

Here's my code:

page.php (this is the page class, and this is only the part of the code i thought was applicable)

Code: Select all

<?
function SetContent($newcontent)
  &#123;
    $this->content = $newcontent;
&#125;
 function Display()
  &#123;
    echo "<html>\n<head>\n";
    echo $this->content;
    echo "</body>\n</html>\n";
  &#125;

?>
This is the db.php script.
It calls to the database, and returns the information from the table:

Code: Select all

<?
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); 
mysql_select_db('localhost') or die(mysql_error()); 

 $sql = "select * from content
	where pageid = '$pageid'
 	order by contentid desc
	";

 $sqlresult = mysql_query ($sql, $db);

 $row = mysql_fetch_array ($sqlresult);

// Close the connection
mysql_close($db);
?>
This is the home.php page.

Code: Select all

<? 
  $pageid = 1; 
  
  require ("../inc/page.php");
  include ("../inc/db.php");


  $homepage = new Page();

  $homepage -> SetContent("<h1>".$row&#1111;3]."</h1><p>".$row&#1111;4]."</p>"
  	);


  $homepage -> Display();


?>
Right now this is all working, but returning only one row of the table, (which I realize is all I'm calling in the home.php page ($row[3], $row[4]) but I haven't been able to successfully call any other variable in that field). I think that the conditional loop should appear in the db.php file, but I've tried several versions (while $i <= number of rows returned) {go through the table, and print an html table w/ the info) - but then I can't seem to pass that info to the home.php page as the variables that I've tried to set in the $homepage -> SetContent line don't receive any info .

I don't know if I'm explaining this clearly. So any questions, comments, or anything would be much appreciated. I've been trying to figure this out for a while now, and I keep running up against a wall. I understand it's basically just getting several rows of info out of an array, but I haven't been able to make it happen.

Thanks for any help.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

While I am not sure on this... I am still a bit of a newb. :D

I think you need to add while ($row=mysql_fetch_array($sqlresult)) statement in the SetContent on your home.php page, since that is where you are building your page content to be displayed in the Display function.

You could try this, although I really am not sure if you can put the while statement inside the SetContent :?

Code: Select all

<? 
  $pageid = 1; 
  
  require ("../inc/page.php"); 
  include ("../inc/db.php"); 

  $homepage = new Page(); 

  $homepage -> SetContent (
  while ($row=mysql_fetch_array($sqlresult))
   &#123;
     "<h1>".$row&#1111;3]."</h1><p>".$row&#1111;4]."</p>"
    &#125; );

  $homepage -> Display(); 


?>
Also,
you probably will want to remove the $row=mysql_fetch_array($sqlresult) and the mysql_close() from your db.php page and add the mysql_close() to the end of the home.php page.
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Post by Rasta »

Thanks ssand

I really appreciate the reply!

I'll give this a try, and let you know if I'm successful.

Thanks again.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

:oops: After I had time to think about it the "while" statement won't help much since you aren't displaying the values while it loops. So, I think you'll end up with only the last row of values from your database.

This defeats the purpose of your Functions, but should work the same:

Code: Select all

<? 
  $pageid = 1; 
  
  require ("../inc/page.php"); 
  include ("../inc/db.php"); 

  $homepage = new Page(); 

  echo "<html>\n<head>\n";
  while ($row=mysql_fetch_array($sqlresult)) 
   &#123; 
     echo "<h1>".$row&#1111;3]."</h1><p>".$row&#1111;4]."</p>";
    &#125;

  echo "</body>\n</html>\n";


?> 
Sorry...

Steve
Post Reply