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
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++;
}

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.