PDA

View Full Version : table alternate row color



pilotvtm
12-14-2009, 05:30 PM
i am trying to change this function to show me alternate row color, not alternate column color.

how can i modify this function without remaking it completely and without java-script (it can be made directly)..?
i tried with come css 3 command i found on google

tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
and with examples from here:
http://lorenzod8n.wordpress.com/2007/06/02/alternating-row-color-in-htmlcss-with-php/ (http://lorenzod8n.wordpress.com/2007/06/02/alternating-row-color-in-htmlcss-with-php/)
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23003019.html
and more....
and i dind't manage to remake it...
can someone please help me?
thank you in advance!!!


function table($res) {

// Columns
$cols = explode(",", $site_config["torrenttable_columns"]);
$cols = array_map("strtolower", $cols);
$cols = array_map("trim", $cols);
$colspan = count($cols);
// End

echo '<table align=center cellpadding="3" cellspacing="0" class="class1" width=99%>
<tr >';

foreach ($cols as $col) {
switch ($col) {
case 'text':
echo "<td class=ttable_head>Data I<b></td>";
break;
case 'text2':
echo "<td class=ttable_head>Data II<b></td>";
break;
case 'modif':
echo "<td class=ttable_head>Data III<b></td>";
break;

}
}


echo "</tr>";


while ($row = mysql_fetch_assoc($res)) {
$id = $row["id"];
print("<tr class=testtabel5>\n");
$x = 1;

foreach ($cols as $col) {


switch ($col) {

case 'text':

print("<td align=center>".$row['data1']);
break;
case 'text2':
print("<td class=class2 align=center>".$row['data2']);
break;
case 'modif':
$char1 = 35; //cut name length
$smallname =CutName(htmlspecialchars($row["name"]), $char1);
$test = $row["data1"];
$dispname = "<b>".$smallname."</b>";


print("<td class=class3 nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"snow.php?id=$id\">Snows</a>");
break;
}
if ($x == 2)
$x--;
else
$x++;
}

print("</tr>\n");

}

print("</table><BR>\n");

}

djr33
12-15-2009, 07:06 AM
foreach($rows as $row) {
///this is where your stuff for a row goes... like alternate tr class
foreach($row as $cell) {
///this is where your stuff for a "column" goes,
///though it is generated cell by cell within each row
///alternate columns here, using td class
}
}

So the simple answer is just move anything from the column area (inner loop) to the row area (outer loop).

Your code is also spaced very strangely. It would be a lot easier to make this stuff work in it if all of the tabs lined up (and also to help you).

pilotvtm
12-15-2009, 09:57 AM
foreach($rows as $row) {
///this is where your stuff for a row goes... like alternate tr class
foreach($row as $cell) {
///this is where your stuff for a "column" goes,
///though it is generated cell by cell within each row
///alternate columns here, using td class
}
}

So the simple answer is just move anything from the column area (inner loop) to the row area (outer loop).

Your code is also spaced very strangely. It would be a lot easier to make this stuff work in it if all of the tabs lined up (and also to help you).


thanks for the answer!
i will try it an d come back and announce you if it worked! :)

pilotvtm
12-15-2009, 10:19 AM
i ralised that this posts one row 5 times...why this happens?
i need 10 on page, now are all on page and are in alphabetical order not in date order...
how can i arrange them??


the second part became something like this:




$rowclass = 0;

foreach ($cols as $rows) {

echo "<tr class=row$rowclass>";

foreach ($cols as $col) {


$rowclass = 1 - $rowclass;


switch ($col) {

case 'text':

print("<td align=center>".$row['data1']);
break;
case 'text2':
print("<td class=class2 align=center>".$row['data2']);
break;
case 'modif':
$char1 = 35; //cut name length
$smallname =CutName(htmlspecialchars($row["name"]), $char1);
$test = $row["data1"];
$dispname = "<b>".$smallname."</b>";


print("<td class=class3 nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"snow.php?id=$id\">Snows</a>");
break;
}
if ($x == 2)
$x--;
else
$x++;
}
}
print("</tr>\n");


}

print("</table><BR>\n");

}
thanks for your help!!!!!!

pilotvtm
12-16-2009, 05:42 PM
sorry for the double post, but i didn't manage to make this work....any other ideas?

djr33
12-16-2009, 07:10 PM
I think this may be because you are using $cols in the loop. The code looks ok, but I don't know what the data inserted is.
You can insert the method in my code into your original code easily: just put the alternation code in the outer loop. You don't need to use foreach loops like that-- that was just the easiest example for the code.

pilotvtm
12-18-2009, 12:49 PM
I think this may be because you are using $cols in the loop. The code looks ok, but I don't know what the data inserted is.
You can insert the method in my code into your original code easily: just put the alternation code in the outer loop. You don't need to use foreach loops like that-- that was just the easiest example for the code.

did it but in another way...this way didn't work..



while ($row = mysql_fetch_assoc($res)) {
$id = $row["id"];

$rowclass = 1 - $rowclass;

print("<tr class=rowt$rowclass>\n");


echo "<tr class=row$rowclass>";

$char1 = 35; //cut name length
$smallname = CutName(htmlspecialchars($row["name"]), $char1);
$dispname = "<b>".$smallname."</b>";

print("<td class=rowt$rowclass nowrap><a title=\"".$row["name"]."\" href=\"titluri-detalii.php?id=$id&amp;hit=1\">$dispname</a>");
print("<td class=rowt$rowclass align=center>".$row['data1']);
print("<td class=rowt$rowclass class=ttable_col4 align=center>".$row['data2']);
print("<td class=rowt$rowclass nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"titluri-modificari.php?id=$id\">Modificari</a>");



$rowclass = 0;
print("</tr>\n");}
print("</table><BR>\n");
$rowclass = 1 - $rowclass;


thanks for your help!!!