Jump to content


Photo

PHP loop for showing all data from MySQL database


  • Please log in to reply
5 replies to this topic

#1 richarddddd

richarddddd
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 18 May 2006 - 12:02 PM

Dear all,
I need your help!!!! I think my problem is not hard to solve, but I am a newbie and I am designing my first PHP MySQL website. I would like to know how I can make a loop so that PHP is retrieving all the data that is in the MySQL database. Right now, It is looping, but it publish the same content in each field. Please could you help me?

This is the PHP file:

<?php
$query = "SELECT postdate, title, newstextshort, newstextlong, source, sourcehyperlink, category FROM news ORDER BY postdate DESC";
$result= mysql_query($query) or die("unable to find selected job number");

$postdate=mysql_result($result,$i,"postdate");
$title=mysql_result($result,$i,"title");
$newstextshort=mysql_result($result,$i,"newstextshort");
$newstextlong=mysql_result($result,$i,"newstextlong");
$source=mysql_result($result,$i,"source");
$sourcehyperlink=mysql_result($result,$i, "sourcehyperlink");
$category=mysql_result($result,$i,"category");

while ($row = mysql_fetch_array($result))
{

$extract($row);

echo "<table width='75%' border='0'>\n";
echo "<tr>\n";
echo "<td width='15%'><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Date:</font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$postdate</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'></font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'><b>$title</b></font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$newstextshort [<a href='$sourcehyperlink'><i>more</i></a>]</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Published by:</font></div></td>\n";
echo "<td width='55%'><font size='2' face='Arial, Helvetica, sans-serif'>$source</font></td>\n";
echo "<td width='30%'><font size='2' face='Arial, Helvetica, sans-serif'>Click <a href='$sourcehyperlink'><b>here</b></a> to enter website</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'>Category:</font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$category</font></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<hr>\n";

}
?>

#2 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 18 May 2006 - 12:11 PM

Try this


 <?php
$query = "SELECT postdate, title, newstextshort, newstextlong, source, sourcehyperlink, category FROM news ORDER BY postdate DESC";
$result= mysql_query($query) or die("unable to find selected job number");

while ($row = mysql_fetch_assoc($result))
{

$extract($row);

echo "<table width='75%' border='0'>\n";
echo "<tr>\n";
echo "
Helvetica, sans-serif'>Date:
\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$postdate</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'></font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'><b>$title</b></font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$newstextshort [<a href='$sourcehyperlink'><i>more</i></a>]</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'>Published by:</font></div></td>\n";
echo "<td width='55%'><font size='2' face='Arial, Helvetica, sans-serif'>$source</font></td>\n";
echo "<td width='30%'><font size='2' face='Arial, Helvetica, sans-serif'>Click <a href='$sourcehyperlink'><b>here</b></a> to enter website</font></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><div align='right'><font size='2' face='Arial, Helvetica, sans-serif'><font face='Arial, Helvetica, sans-serif'><font size='2'>Category:</font></font></font></div></td>\n";
echo "<td colspan='2'><font size='2' face='Arial, Helvetica, sans-serif'>$category</font></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<hr>\n";

}
?>

Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 18 May 2006 - 12:12 PM

Remove all of this:
$postdate=mysql_result($result,$i,"postdate"); 
$title=mysql_result($result,$i,"title"); 
$newstextshort=mysql_result($result,$i,"newstextshort"); 
$newstextlong=mysql_result($result,$i,"newstextlong"); 
$source=mysql_result($result,$i,"source");
$sourcehyperlink=mysql_result($result,$i, "sourcehyperlink"); 
$category=mysql_result($result,$i,"category");

The extract() does all of that for you within the while loop.

I'm not sure why you're not getting the rest of the results however. The rest of that looks fine.

I will mention, however, that your HTML is totally hosed. You should never use a DIV to align text within a <td> and the <font> tag is depreciated. You should really look into CSS or at least use inline CSS with the "style" attribute of the <td>.

Info: PHP Manual


#4 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 18 May 2006 - 12:15 PM



I guess it is not working because it needs an associative array for using extract()....

just a thought...
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 18 May 2006 - 12:27 PM

Well, I just noticed something else. He is sending an associative array to extract (that's what $row is), but there is a $ before the extract.

Remove the $. extract() is a function, not a variable.

Info: PHP Manual


#6 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 18 May 2006 - 01:00 PM


Bingo !!

Nice observation i must admit !!
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users