Jump to content

Recommended Posts

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";

}
?>
Try this

[code]

<?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";

}
?>
[/code]
Remove all of this:
[code]$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");[/code]

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>.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.