Jump to content


alternating row colors


7 replies to this topic

#1 er0x

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 17 October 2009 - 09:15 PM

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...

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.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 972 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 October 2009 - 10:15 PM

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.

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. :)

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.

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. :)

#3 er0x

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 17 October 2009 - 10:34 PM

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.

#4 er0x

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 17 October 2009 - 10:46 PM

so this code works....

	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 "

#5 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 972 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 October 2009 - 10:57 PM

Opps, my bad. :)

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. ;)

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

:) :sparta: :sparta:

Edited by Demonslay, 17 October 2009 - 11:00 PM.


#6 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 18 October 2009 - 02:00 PM

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

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;


#7 er0x

    Young Padawan

  • Members
  • Pip
  • 43 posts

Posted 19 October 2009 - 04:13 PM

View PostHayden, on Oct 18 2009, 08:00 PM, said:

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

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

#8 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 20 October 2009 - 07:36 PM

%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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users