...

View Full Version : code syntax



grudz
06-10-2006, 10:52 PM
Hi,

Can anybody tell me why this code isnt working perfectly...
its giving me a large amount of records, even though i only have 2 of them in my database..im guessing it is because of the counter, but even if i take it out, it still gives me all those records.



<?php
$counter = 0;
do {
$color = ($counter & 1)? "#ccc" : "#fff";
$counter++;

mysql_select_db($database_gabriel, $gabriel);
$query_newsletter2 = "SELECT * FROM newsletter_articles ORDER BY year DESC, month DESC, day DESC";
$newsletter2 = mysql_query($query_newsletter2, $gabriel) or die(mysql_error());
$row_newsletter2 = mysql_fetch_assoc($newsletter2);
$totalRows_newsletter2 = mysql_num_rows($newsletter2);

$year2 = $row_newsletter2['year'];
$month2 = $row_newsletter2['month'];
$day2 = $row_newsletter2['day'];

?>
<tr style="background: <?php print $color;?>;">
<td align="left" valign="top"><a href="previousnewsletters.php?date=<? print "".$year2."-".$month2."-".$day2.""?>" class="LinkBlueLight" title="<?php echo $row_newsletter2['title']; ?>"><?php echo $row_newsletter2['title']; ?></a></td>
<td align="left" valign="top"><?php echo $row_newsletter2['description']; ?></td>
<td align="right"><?php echo $row_newsletter2['year']; ?> - <?php echo $row_newslette2['month']; ?> - <?php echo $row_newsletter2['day']; ?></td>
</tr>
<?php } while (
$row_newsletter2 = mysql_fetch_assoc($newsletter2)
);?>

fci
06-10-2006, 11:02 PM
why do you have mysql_select_db($database_gabriel, $gabriel); in there ? that could be why

chump2877
06-10-2006, 11:05 PM
something like this:


<?php
$counter = 2;

mysql_select_db($database_gabriel, $gabriel);
$query_newsletter2 = "SELECT * FROM newsletter_articles ORDER BY year DESC, month DESC, day DESC";
$newsletter2 = mysql_query($query_newsletter2, $gabriel) or die(mysql_error());
$totalRows_newsletter2 = mysql_num_rows($newsletter2);

if ($totalRows_newsletter2 >= '1')
{
while ($row_newsletter2 = mysql_fetch_assoc($newsletter2))
{
$year2 = $row_newsletter2['year'];
$month2 = $row_newsletter2['month'];
$day2 = $row_newsletter2['day'];

?>
<tr style="background: <?php

if ($counter % 2 == 0)
{
print '#ccc';
}
else
{
print '#fff';
}

?>;">
<td align="left" valign="top"><a href="previousnewsletters.php?date=<? print "".$year2."-".$month2."-".$day2.""?>" class="LinkBlueLight" title="<?php echo $row_newsletter2['title']; ?>"><?php echo $row_newsletter2['title']; ?></a></td>
<td align="left" valign="top"><?php echo $row_newsletter2['description']; ?></td>
<td align="right"><?php echo $row_newsletter2['year']; ?> - <?php echo $row_newslette2['month']; ?> - <?php echo $row_newsletter2['day']; ?></td>
</tr>
<?php

$counter++;
}
}

?>

grudz
06-10-2006, 11:05 PM
well its there because DreamWeaver automatically puts it..but i took it out, and it still gives me a huge amount of records

fci
06-11-2006, 01:05 AM
what happens if you run the query from phpMyAdmin or mysql itself? and, take out the first time you have this:

$row_newsletter2 = mysql_fetch_assoc($newsletter2);

chump2877
06-11-2006, 01:16 AM
the problem is that he's executing his query inside the do loop, when it onlt needs to be performed once (before the loop)...he's performing the the query over and over again, and the internal results pointer isn;t moving ahead, but rather stalling on the same record over and over again...he's not iterating through the result set, rather he's reproducing the result set over and over again and listing the first record that pops up...that's what it looks like



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum