Help - Search - Members - Calendar
Full Version: alternating row colors
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
er0x
so i have this script to alternate row colors. it works and all but the problem is that at the end of it, it always adds an extra row thats not pulling anything from the db *obviousley, bc its not supposed to be there*

so here is the code...

CODE
while($r=mysql_fetch_array($result))
{


for($i = 0; $i < $numofrows; $i++)
    {
    $r = mysql_fetch_array($result);
    $id = $r[id];

    if($i % 2)
        {
    echo "<div class=\"post2\">";
    echo "<div class=\"date\"> <span class=\"month\">".$r[month]."</span><pan class=\"day\">".$r[day]."</span></div>";
    echo "<p> <span class=\"title\">".$r[title]."</span>";
    echo "<a href=\"?page=entries&id=".$id."\">View</a> :: Total Views - ".$r[views]." :: Author - $author :: Category - ".$r[cat]."</p>";
    echo "</div>";
            }
    else
        {
    echo "<div class=\"post\">";
    echo "<div class=\"date\"> <span class=\"month\">".$r[month]."</span><pan class=\"day\">".$r[day]."</span></div>";
    echo "<p> <span class=\"title\">".$r[title]."</span>";
    echo "<a href=\"?page=entries&id=".$id."\">View</a> :: Total Views - ".$r[views]." :: Author - $author :: <b>Category</b> - ".$r[cat]."</p>";
    echo "</div>";;
        }
    }
}


so what do i need to change, add, take out, etc... any help greatly appreciated.
Demonslay
Before I go into what could be a problem, I'll just point out one way you could optimize this code.

If you examine what you are echo'ing, you should notice the only difference is the class you make the container div element. Thus, you shouldn't repeat yourself, and could optimize your code with a ternary operator like this.

CODE
echo "<div class=\"post".(($i % 2) ? '2' : '')."\">";
    echo "<div class=\"date\"> <span class=\"month\">".$r[month]."</span><pan class=\"day\">".$r[day]."</span></div>";
    echo "<p> <span class=\"title\">".$r[title]."</span>";
    echo "<a href=\"?page=entries&id=".$id."\">View</a> :: Total Views - ".$r[views]." :: Author - $author :: Category - ".$r[cat]."</p>";
    echo "</div>";


Just a little tip. bigwink.gif

Now, as to your problem, I fear you might be subject to a very common problem most programmers un-knowingly run into when using loops: miscounting. Be careful when you are starting at 0 for use in arrays, and the using an ending number equal to how many items you are iterating. Your code does look correct, in that you are starting at index 0, then continuing while your $i variable is less than the total number of rows. Try debugging your $numofrows variable and make sure it is correct.

Also, I'm wondering if your method of iterating database results is correct... shouldn't each iteration of your main loop (while loop) be where you execute finding the results? In other words, it looks like you are going through each result, then doing a loop of that record several times. Your code should look like this.

CODE
while($r = mysql_fetch_array($result)){
    $r = mysql_fetch_array($result);
    $id = $r['id'];

    echo '<div class="post"'.(($i % 2) ? '2' : '').'>';
    echo '<div class="date"> <span class="month">'.$r['month'].'</span><span class="day">'.$r['day'].'</span></div>';
    echo '<p><span class="title">'.$r['title'].'</span>';
    echo '<a href="?page=entries&id='.$id.'">View</a> :: Total Views - '.$r['views'].' :: Author - '.$author.' :: Category - '.$r['cat'].'</p>';
    echo '</div>';
}


The while loop will automatically exit when all results have been iterated, as mysql_fetch_array() will return a value that will make the while loop condition not true when it has passed the pointer in the fetched query.

Also be wary of your usage of quotes, use single quotes for array indexes and when you don't need to parse variables within quotes. Just good habit to get into for performance and readability reasons. bigwink.gif
er0x
thank you for such a great reply. after reading what you had written, i looked at what you were talking about "looping inside the loop" basically? anyway, i took out the while statement and just left the for statement and it worked. it took out the extra row which is great!

now, as you were talking about optimizing the script so its smaller and such so im working on that and i will post here what i got and maybe you can tell me if it looks right to you/

thanks again tho, main problem is solved.
er0x
so this code works....

CODE
    echo '<div class="post'.(($i % 2) ? '2' : '').'">';
    echo '<div class="date"> <span class="month">'.$r['month'].'</span><span class="day">'.$r['day'].'</span></div>';
    echo '<p><span class="title">'.$r['title'].'</span>';
    echo '<a href="?page=entries&id='.$id.'">View</a> :: Total Views - '.$r['views'].' :: Author - '.$author.' :: Category - '.$r['cat'].'</p>';
    echo '</div>';


all i had to do was move a "
Demonslay
Opps, my bad. Whistle.gif

Glad it works for you.

And ya, I tend to ramble on what I think could be the problem, and then notice what the simpler solution is as I'm typing... so I leave the rant just for interesting reading in-case it happens to also be correct. bigwink.gif

Unrelated: Just noticed this awesome smiley, I should really find some use for it.

THIS_IS_SPARTA_v2_0_by_DoooM.gif THIS_IS_SPARTA_v2_0_by_DoooM.gif THIS_IS_SPARTA_v2_0_by_DoooM.gif
Hayden
I'm a little bored, so here's how I was thinking it.

CODE
while($r = mysql_fetch_array($result)){
    $r = mysql_fetch_array($result);
    $id = $r['id'];
    $css = (($i % 2)==0) ? 'color' : '';

    $html ='<div class="post %s"><div class="date"><span class="month">%s</span><span class="day">%d</span></div><p><span class="title">%s</span><a href="?page=entries&amp;id=%s">View</a> :: Total Views - %d :: Author - %s :: Category - %s</p></div>';
    $out = sprintf($html,$css,$r['month'],$r['day'],$r['title'],$id,$r['views'],$author,$r['cat']);
}
echo $out;


er0x
QUOTE (Hayden @ Oct 18 2009, 08:00 PM) *
I'm a little bored, so here's how I was thinking it.

CODE
while($r = mysql_fetch_array($result)){
    $r = mysql_fetch_array($result);
    $id = $r['id'];
    $css = (($i % 2)==0) ? 'color' : '';

    $html ='<div class="post %s"><div class="date"><span class="month">%s</span><span class="day">%d</span></div><p><span class="title">%s</span><a href="?page=entries&amp;id=%s">View</a> :: Total Views - %d :: Author - %s :: Category - %s</p></div>';
    $out = sprintf($html,$css,$r['month'],$r['day'],$r['title'],$id,$r['views'],$author,$r['cat']);
}
echo $out;



well looking at it that way just made it confusing as hell. I started reading on the sprintf on php.net and i didnt really understand it that much lol. your example i can understand better without an explanation then they did with one. but the whole %d's and s's are really throwing me off lol. thanks tho, it did show a new thing for me to learn
Hayden
%d is just a placeholder for an integer
%s is a placeholder for a string (aka text)
%f (this is one that is sort of confusing. lol) is a placeholder for floats (aka number with decimals like 1.03)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.