Simple Array Problem
Posted: Tue Aug 20, 2002 11:09 am
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)
This is the db.php script.
It calls to the database, and returns the information from the table:
This is the home.php page.
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.
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)
{
$this->content = $newcontent;
}
function Display()
{
echo "<html>\n<head>\n";
echo $this->content;
echo "</body>\n</html>\n";
}
?>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);
?>Code: Select all
<?
$pageid = 1;
require ("../inc/page.php");
include ("../inc/db.php");
$homepage = new Page();
$homepage -> SetContent("<h1>".$rowї3]."</h1><p>".$rowї4]."</p>"
);
$homepage -> Display();
?>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.