Jump to content

[SOLVED] PHP loop problems


outatime

Recommended Posts

Hi everyone. I've been trying to figure this out for the last six hours. I'm trying to call info from a database into a table. I want to have alternating colors for the rows which I have succeeded in doing. My problem is that its repeating each line 4 times before it lists the next one. Here is the link: http://southernpartsequipment.com/dasher/minutes.php Here is my code. Hope someone can help. Thanks.

<?php

$con = mysql_connect("mysql","***","***");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("dasher", $con);

 

$result = mysql_query("SELECT * FROM minutes");

$num_rows = mysql_num_rows($result);

echo "<center><table border='0' cellspacing='3' cellpadding='1'>

 

<th>Event</th>

<th>Date</th>

<th>Minutes</th>

<th>PDF</th>

</tr>";while($row = mysql_fetch_array($result))

  {

 

 

$num = $result;

 

 

for($i=0;$i<$num;$i++){

 

 

if($i % 2 ==0)

{

$bgcolor='#adc5c8';

}

else

{

$bgcolor='#eeeeee';

}

 

  echo "<tr bgcolor=$bgcolor>";

  echo "<td>" . $row['event'] . "</td>";

  echo "<td>" . $row['date'] . "</td>";

  echo "<td>" . $row['minutes'] . "</td>";

  echo "<td>" . "<a href=\"{$row['pdf']}\">Download</a>" . "</td>";

  echo "</tr>";

 

}

}

echo "</table></center>";mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/153285-solved-php-loop-problems/
Share on other sites

The for loop within your while loop is causing the duplicate results. How your while loop should be:

// initiate counter
$i = 0;
while($row = mysql_fetch_array($result))
{
    // alternete row color
    $bgcolor= ($i%2 == 0) ? '#adc5c8' : '#eeeeee';

    echo "<tr bgcolor=$bgcolor>";
    echo "<td>" . $row['event'] . "</td>";
    echo "<td>" . $row['date'] . "</td>";
    echo "<td>" . $row['minutes'] . "</td>";
    echo "<td>" . "<a href=\"{$row['pdf']}\">Download</a>" . "</td>";
    echo "</tr>";

    // increment counter
    $i++;
}

 

Another way...(similar)

 

$RowCnt = 0;

while (!$rs->EOF && $nRecCount < $nStopRec) {

$nRecCount++;

if (intval($nRecCount) >= intval($nStartRec)) {

$RowCnt++;

$db_alum->CssClass = "ewTableRow";

$db_alum->CssStyle = "";

$db_alum->RowClientEvents = "onmouseover='ew_MouseOver(this);' onmouseout='ew_MouseOut(this);' onclick='ew_Click(this);'";

if ($RowCnt % 2 == 0) {

$db_alum->CssClass = "ewTableAltRow";

}

LoadRowValues($rs);

$db_alum->RowType = EW_ROWTYPE_VIEW;

RenderRow();

?>

Bye

Javier Navarrete (www.softein.com)

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.