Page 1 of 1

Silly Question, but hope someone knows

Posted: Sun Nov 28, 2004 2:40 pm
by irishmike2004
Hi There:

I am writing a webpage that I want to dynamically update a table and colorize every other row... kind of how these forums list topics a bit, only the data is coming from a Shoutcast server and will be an array like songhistory[0], songhistory[1]... etc.

so the table will be defined in HTML inside of PHP and basically I am doing the last 4 songs played or actually it is 5 because songhistory[0] is aka the current track playing...

can someone give me some sample code on how this might work... basically it will start with a message that there is no available song history so one table row, and then it needs to add a row as it gets to the end and like I mentioned earlier, 4 rows maximum.

Thanks for any help.

Posted: Sun Nov 28, 2004 3:37 pm
by rehfeld

Code: Select all

<?php


$i = 0;
while ($row = mysql_fetch_assoc($result)) {
    $bg_color = ($i++ % 2) ? 'dark' : 'light';
    echo "<tr class="$bg_color">";
}

// thats a 1 liner. this is what its doing, might be easier to understand


$i = 0;
while ($row = mysql_fetch_assoc($result)) {
    if (($i % 2) == 0) {
        $bg_color = 'dark';
    } else {
        $bg_color = 'light';
    }
    $i++;
    echo "<tr class="$bg_color">";
}



?>

% is the modulous operator, not percentage

RE:Silly Question, but hope someone knows

Posted: Sun Nov 28, 2004 3:52 pm
by irishmike2004
That looks like what I kind of thought it would be, but i am unsure of the mysql part, is there any database needed for that to work or it is just how PHP "understands" rows?


I am trying to understand what I am seeing.

Thanks for the help.

Posted: Sun Nov 28, 2004 4:11 pm
by rehfeld
i just figured you were calling a db.

it has nothing to do w/ rows, i was just simulating some type of a loop

just replace that part w/ your chosen method of looping

Posted: Sun Nov 28, 2004 4:22 pm
by John Cartwright

Code: Select all

<?php
$i = 0;
foreach ($songhistory as $song)
{    
     $bg_color = ($i++ % 2) ? 'dark' : 'light';    
     echo '<tr><td class="'.$bg_color.'">'. $song .'</td></tr>';
}


?>

this almost worked

Posted: Sun Nov 28, 2004 8:56 pm
by irishmike2004
here is the code as it stands, I am getting "ARRAY" but with the proper background colors, I am not sure why I can't get the array data, perhaps I don't fully understand what the 'foreach' statement is doing, but can someone please help me fix this code :-)

Code: Select all

if (is_array($history)) &#123;
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>'; -- this was a debug test ;-)
			&#125;
			$k=0;
			
			foreach ($history as $song) &#123;
				$bgColor = ($k++ % 2) ? '#00CCCC' : '#FFFFFF';
	 			echo '<tr><td bgcolor="'.$bgColor.'">'.$song.'</td></tr>';
			&#125;
			
		&#125; else &#123;
			echo "No Song History Available";
		&#125;
I am not worried about the no song history error part yet,I will simply make it the first row of the table.

Thanks,

Mike

Figured it out

Posted: Sun Nov 28, 2004 9:20 pm
by irishmike2004
Here is the solution that worked so far given my code:

Code: Select all

if (is_array($history)) &#123;
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>';
			  $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF'; 
			  echo '<tr><td bgcolor="'.$bgColor.'">'.$song&#1111;$j].'</td></tr>';
			&#125;
	&#125; else &#123;
			echo '<tr><td bgcolor="#FFFFFF">No Song History Available</td></tr>';
		&#125;
Just wanted to share that I figured this out and got it to work this way.

Thanks for all your help :-)