Ok, ive created a very basic shoutbox. Now I have some basic questions about the code used.
The code to view the shoutbox is:
[code:1:77d09fb5b6]<?php
include 'db.php';
$query="SELECT * FROM shoutbox";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$email=mysql_result($result,$i,"email");
$message=mysql_result($result,$i,"message");
echo "$name<br><a href=\"mailto:$email\">Email</a><br>$message<hr>";
++$i;
}
include 'form.php';
?>[/code:1:77d09fb5b6]
in that code there is a loop so that the page will display all of the results from the database which its asking for. now why is it that in loops like this there is always the variable $i
has it got to be $i for any specific reason?
Also in that code above I would like to order the posts so that I get the latest one at the top. How do I do this? Each message in the db has got its own unique ID so its something to do with that.
Id also like to limit the amount of messages shown. Any ideas?
Thanks
Comments
And to order them, use the ORDER BY {field} [ASC|DESC] clause in your query. To limit, use the LIMIT {num} clause.
And you don't need to mysql_close(), it will be done at the end of the script automatically.
[code:1:0ea02b735a]<?php
include 'db.php';
$result = mysql_query("SELECT * FROM shoutbox ORDER BY id DESC LIMIT 15");
while($row = mysql_fetch_array($result))
{
extract($row, EXTR_SKIP);
echo "$name<br><a href=\"mailto:$email\">Email</a><br>$message<hr>";
}
include 'form.php';[/code:1:0ea02b735a]
Here we execute the query and store it in $results. We then loop through the results with the while loop. Since we fetch the results as an array (that is, they are $row, $row etc etc) we want to extract them so we can use them more easily.
And that is that
Webmaster-Talk.com
Chroder.com
thanks for that
The Royal Ram