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.
Silly Question, but hope someone knows
Moderator: General Moderators
-
irishmike2004
- Forum Contributor
- Posts: 119
- Joined: Mon Nov 15, 2004 3:54 pm
- Location: Lawrence, Kansas
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
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.
I am trying to understand what I am seeing.
Thanks for the help.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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 
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
Code: Select all
if (is_array($history)) {
for ($j=0;$j<sizeof($history);$j++) {
$songї$j] = $historyї$j]ї"title"];
//echo $songї$j].'<br>'; -- this was a debug test ;-)
}
$k=0;
foreach ($history as $song) {
$bgColor = ($k++ % 2) ? '#00CCCC' : '#FFFFFF';
echo '<tr><td bgcolor="'.$bgColor.'">'.$song.'</td></tr>';
}
} else {
echo "No Song History Available";
}Thanks,
Mike
-
irishmike2004
- Forum Contributor
- Posts: 119
- Joined: Mon Nov 15, 2004 3:54 pm
- Location: Lawrence, Kansas
Figured it out
Here is the solution that worked so far given my code:
Just wanted to share that I figured this out and got it to work this way.
Thanks for all your help
Code: Select all
if (is_array($history)) {
for ($j=0;$j<sizeof($history);$j++) {
$songї$j] = $historyї$j]ї"title"];
//echo $songї$j].'<br>';
$bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF';
echo '<tr><td bgcolor="'.$bgColor.'">'.$songї$j].'</td></tr>';
}
} else {
echo '<tr><td bgcolor="#FFFFFF">No Song History Available</td></tr>';
}Thanks for all your help