Displaying results

Hello.
I have this code:

[php]

<?php // Connection information $db_host = "localhost"; $db_user = "**"; $db_pass = "**"; $db_name = "**"; // Connect to server $dbac = mysql_connect($db_host,$db_user,$db_pass); $today = getdate(); // Select database mysql_select_db ($db_name) or die ("Cannot connect to database"); // Get date from _GET param or current date if not available $_GET['date'] = @trim(stripslashes($_GET['date'])); $date = ($_GET['date'] && !empty($_GET['date'])) ? date('Y-m-d', strtotime(trim($_GET['date']))) : date('Y-m-d'); $result = mysql_query('SELECT * FROM news WHERE Date = "' . $date . '" ORDER BY Time'); $curtime = time(); if ($result && mysql_num_rows($result)) { $numrows = mysql_num_rows($result); $rowcount = 1; while ($row = mysql_fetch_assoc($result)) { while(list($var, $val) = each($row)) { extract ($row); print "$Title
$News
$Date   $Time
"; } print "
"; ++$rowcount; } } ?>

[/php]

I have a table (called news) in a MySQL database with 5 fields: (News, Title, Date, Time). The code works. However, when I display the results, it shows the same thing 5 times.

This is what shows:

[b]News system up and running![/b] We finally have this news system working. If you want to see older news (by date), just click the arrow on top. 2007-01-12 13:12:42

News system up and running!
We finally have this news system working. If you want to see older news (by date), just click the arrow on top.
2007-01-12 13:12:42

News system up and running!
We finally have this news system working. If you want to see older news (by date), just click the arrow on top.
2007-01-12 13:12:42

News system up and running!
We finally have this news system working. If you want to see older news (by date), just click the arrow on top.
2007-01-12 13:12:42

News system up and running!
We finally have this news system working. If you want to see older news (by date), just click the arrow on top.
2007-01-12 13:12:42

How can I make it so that it only shows a news post ONCE?

PS: I think it’s because of the extract($row) thing.

I think the problem is in this section:

[php]

while ($row = mysql_fetch_assoc($result)) {
  
  while(list($var, $val) = each($row)) {
    extract ($row);
    print "<b>$Title</b><br />$News<br /><font size='2'>$Date &nbsp; $Time</font><br />";
  }
  
  print "<br />";
  ++$rowcount;
} 

[/php]
More specifically I believe it’s in the way ytou are using the list construct. You are assigning values to $var and $val and aren’t using them. Maybe try doing that block of code like this:

[php]
while ($row = mysql_fetch_assoc($result)) {

    print '<b>'.$row['Title'].'</b><br />'.$row['News'].'<br /><font size="2">'.$row['Date'].' &nbsp; '.$row['Time'].'</font><br />';
  
  print "<br />";
  ++$rowcount;
}  

[/php]

Also I am not really sure if $rowcount is seving any purpose unless it’s to be used later on.

Sponsor our Newsletter | Privacy Policy | Terms of Service