Jump to content

Looping Data


Akenatehm

Recommended Posts

Hey Guys,

 

I am trying to create a mini news system.

 

Here is the code:

<html>
<body>
<?php
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "pass";
$dbname = "game";
$connect = mysql_connect($dbhost,$dbuser,$dbpass);
$selectdb =	mysql_select_db($dbname);

$selectentries = mysql_query("SELECT * FROM news");

$filterentries = mysql_fetch_assoc($selectentries);
$title = $filterentries['title'];
$content = $filterentries['content'];
$author = $filterentries['author'];
$date = $filterentries['date'];

$arr=array("$title" );

foreach ($arr as $value)
{
echo "$title - " . "$date" ."<br /> <br /> ";
echo "Content: " . $content . "<br /> <br />";
echo "Posted By: " . "$author <br />";
}
?>
</body>
</html>

 

It is only displaying the first entry in the database. Could you please help me in making it show all of them.

 

Thanks in Advanced,

Cody

Link to comment
https://forums.phpfreaks.com/topic/148722-looping-data/
Share on other sites

your loop is completely wrong.

 

$filterentries = mysql_fetch_assoc($selectentries);

fetches the first row and then the next if it is called again...and so on...

 

change it to

 

while($filterentries = mysql_fetch_assoc($selectentries)) {

 

and remove

$arr=array("$title" );

foreach ($arr as $value)
{

 

should work (not tested)

 

aseaofflames

Link to comment
https://forums.phpfreaks.com/topic/148722-looping-data/#findComment-780899
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.