Forum Moderators: coopster

Message Too Old, No Replies

Duo coloring table cells

How to

         

closer

10:11 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Hello,

I have my code working and it displays 10 results per page and has pagination.

Now, what I would like is to have the table cells switching in color.

table cell 1: white
table cell 2: grey
table cell 3: white
and so on...

If anyone could help me out or can point me to a topic that has handled this subject before (could't find one myself) it would be greatly appreciated!

Current code:


<?
$HTTP_GET_VARS["cat"];
if ($cat == "") ($section = "12");
else $section = $cat;

$DBHost = "****";$DBUser = "****";$DBPass = "****";$DBName = "****";
$DBConn = mysql_connect($DBHost,$DBUser,$DBPass) or die("Unable to connect to database");
$DBSele = mysql_select_db("$DBName") or die("Unable to select database $DBName");
$DBQuery = "SELECT id FROM ringtones WHERE sectionid = $section";

/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 10;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results. You need to edit the sql to fit your needs */
$result = mysql_query("$DBQuery") or die (mysql_error());

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '<a href="index.php?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
$HTTP_GET_VARS["cat"];
if ($cat == "") ($cat = "12");
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= '<b>'.$i.'</b>';
}
else
{
$pagination .= ' <a href="header.php?page='.$i.'&cat='.$cat.'">'.$i.'</a>';
}
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= ' <a href="header.php?page='.$next.'&cat='.$cat.'">Next</a>';
}

echo '<table width="100%">';
$result=mysql_query("SELECT * FROM ringtones WHERE sectionid = $section LIMIT $from, $max_results ");

while ($i = mysql_fetch_array($result))
{
/* This is where you print your current results to the page */

echo '<tr><td bgcolor="#FFFFFF">'. $i["ringtoneid"] . ' - ' . $i["artist"] . ' - ' . $i["song"] . '</td></tr>';
}
echo '</table><br />';
echo $pagination;
?>

fentisen

10:25 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



while ($i = mysql_fetch_array($result))
{
/* This is where you print your current results to the page */

echo '<tr><td bgcolor="#FFFFFF">'. $i["ringtoneid"] . ' - ' . $i["artist"] . ' - ' . $i["song"] . '</td></tr>';
}


Replace that code with this code:
$n = 0

while ($i = mysql_fetch_array($result))
{
/* This is where you print your current results to the page */

if ($n % 2 == 0) {
$color = #FFFFFF;
}else{
$color = #000000;
}
echo '<tr><td bgcolor="'.$color.'">'. $i["ringtoneid"] . ' - ' . $i["artist"] . ' - ' . $i["song"] . '</td></tr>';

$n++;
}

That would do the trick.

StupidScript

12:42 am on Nov 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks good fentisen.

Similarly, I usually use:

if ($cnt) {

$color = #FFFFFF;$cnt=0;

}else{

$color = #000000;$cnt=1;

}

closer

12:31 am on Nov 10, 2005 (gmt 0)

10+ Year Member



Thanks for the reply! did the trick...

could you explain what if $n % 2 == 0 means? First thought it meant if number is even .... but guess not.

coopster

2:30 pm on Nov 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are correct. % is the modulus [php.net] operator.