Forum Moderators: coopster

Message Too Old, No Replies

Displaying a set number of results

         

nfs2

2:01 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Im making a blog, and on the main page id like to display the last 30 entries. Its not working though, and its only displaying the latest entry. Here's the code for the function that gets the information, and the code that displays it;

 function getEntryInfo($username){
$q = "SELECT * FROM ".JOURNAL_ENTRIES." WHERE journal_id = '$username' ORDER by entry_id DESC LIMIT 0, 30";
$result = mysql_query($q, $this->connection);
$dbarray = mysql_fetch_array($result);
return $dbarray;
}

Thats the database query, heres the code that displays the results;

<?
$entry = trim($_GET['user']);
if(!$entry ¦¦ strlen($entry) == 0 ¦¦
!eregi("^([0-9a-z])+$", $entry) ¦¦
!$database->usernameTaken($entry)){
die("Username not registered");
}
$journal_info = $database->getJournalInfo($entry);
$entry_info = $database->getEntryInfo($entry);
?>
<?php echo "<h2>".$journal_info['journal_name']."</h2>";?>
<div style="border: 1px solid #000000;">
<div align="center"><b><?php echo " ".$entry_info['entry_subject']."<br><br>";?></b><?php echo " ".$entry_info['entry_time']."<br><br>";?></div>
<div align="center"><?php echo " ".$entry_info['entry_text']."";?></div>
</div>
<?php
if(strcmp($session->username,$entry) == 0){
echo "<br><a href=\"update.php\">Update</a><br>";
}
echo "<br>Back To [<a href=\"index.php\">Home</a>]<br>";
?>

Im fairly new to php, but im fairly sure this should return the last 30 entries, but it only returns one. Can anybody help?

jatar_k

5:08 pm on Dec 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would try echoing the query and then executing that query directly in mysql either via the commandline or something like phpmyadmin.

You need to confirm that the query actually works, and is being properly constructed, before you can start picking apart the subsequent code.

nfs2

6:35 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Thank you for your reply

Doesn't the fact that it does bring up one entry at all (a correct one at that, the last entry for that user), mean that the query works, and the subsequent code is at fault?

jatar_k

7:10 pm on Dec 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not necessarily

maybe the query is only selecting a single result and the code is properly processing what it is getting

Mr_Fern

8:36 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



the fetch functions fetch one row at a time.

you would require something of the sort:


function getEntryInfo($username){
$q = "SELECT * FROM ".JOURNAL_ENTRIES." WHERE journal_id = '$username' ORDER by entry_id DESC LIMIT 0, 30";
$result = mysql_query($q, $this->connection);
while ($row = mysql_fetch_array($result))
$dbarray[] = $row;
return $dbarray;
}

and then in the page code


$entry_info = $database->getEntryInfo($entry);
$info_found = count($entry_info); // number of journal posts found (max 30 due to limit)

<?php echo "<h2>".$journal_info['journal_name']."</h2>";?> // display once (outside for loop)

for ($i = 0; $i < $info_found; $i++) {
$current_entry_info = $entry_info[$i];
?>
<div style="border: 1px solid #000000;">
<div align="center"><b><?php echo " ".$current_entry_info['entry_subject']."<br><br>";?></b><?php echo " ".$current_entry_info['entry_time']."<br><br>";?></div>
<div align="center"><?php echo " ".$current_entry_info['entry_text']."";?></div>
</div>
<?php
if(strcmp($session->username,$entry) == 0){
echo "<br><a href=\"update.php\">Update</a><br>";
}

} // ends for loop

echo "<br>Back To [<a href=\"index.php\">Home</a>]<br>";
?>

nfs2

9:35 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Thank you! This has been giving me a headache all day.. lol

Works great now :)

Mr_Fern

9:37 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Haha Great. I know how frustrating it can be. Glad to have helped.