Silly Question, but hope someone knows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Silly Question, but hope someone knows

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

RE:Silly Question, but hope someone knows

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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>';
}


?>
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

this almost worked

Post 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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Figured it out

Post 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 :-)
Post Reply