News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

PHP code with alternating row color

Started by Azoula, May 07, 2015, 10:47:39 AM

Previous topic - Next topic

Azoula

Hi guys , as the title says can anyone tell me how to do it here:

<?php
    while($data = mysql_fetch_array($req)){
?>
<?php
$up = $data['update'];
        echo "$up<br />";
?>

margarett

This should work (sorry, I'm not a fan of that "inline PHP")
<?php
$alternate
= false;
while(
$data = mysql_fetch_array($req)){

$up = $data['update'];
if ($alternate)
echo '<div style="..........">'
else
echo '<div style="..........">'

echo $up;
echo ' </div>';
$alternate = !$alternate;
}
?>
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Azoula

Sorry bro didn't work

<?php
$alternate
= false;
while(
$data = mysql_fetch_array($req)){

$up = $data['update'];
if ($alternate)
echo '<div style="color:#FFFFFF">'
else
echo '<div style="color:#0000FF">'

echo $up;
echo ' </div>';
$alternate = !$alternate;
}
?>

margarett

Didn't work is not a good description, sorry...

But I see the error: there is a ; missing in each opening div. I tested with this
<?php
$alternate
= false;

//Some dummy data becase I don't have your query
$datas = array('row1', 'row2', 'row3', 'row4', 'row5', 'row6');

//while($data = mysql_fetch_array($req)){
foreach ($datas as $data) {

//$up = $data['update'];
$up = $data;
if ($alternate)
echo '<div style="color:#A0A0A0">';
else
echo '<div style="color:#0000FF">';

echo $up;
echo ' </div>';
$alternate = !$alternate;
}
?>


And it worked. You should just need to fix this --> echo   '<div style="color:#FFFFFF">' to this --> echo   '<div style="color:#FFFFFF">';
Same for the other div ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Azoula

sorry i'm not good in PHP language this is my code:



// get data from SQL
<?php
  $base
= mysql_connect (...........,...........,..............);
   
mysql_select_db (............., $base);
   
$sql = 'SELECT * FROM updates';
   
$req = mysql_query($sql);
?>

// add rows using your code is that correct
<?php
   $alternate
= false;
   while(
$data = mysql_fetch_array($req)){
   
$up = $data['update'];
   if (
$alternate)
echo '<div style="color:#A0A0A0">';
else
echo '<div style="color:#0000FF">';

echo $up;
echo ' </div>';
$alternate = !$alternate;
}
?>


Edit:
Bro it's working but your code just change the text color.
i want the whole row
Done Fixed thx.

Advertisement: