![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| I'm trying to output a table with alternate background color rows. The following snippet outputs a table after connecting to a mySQL database with posts but they all have the same bgcolor. Any help with the syntax will be greatly appreciated. Thank you. Tony Ritter ......................................... <? <table width=100% cellpadding=3 cellspacing=1 border=1> <tr> <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">AUTHOR</font> </th> <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">POST</font></th> </tr>"; while ($posts_info = mysql_fetch_array($get_posts_res)) { $post_id = $posts_info['post_id']; $post_text = nl2br(stripslashes($posts_info['post_text'])); $post_create_time = $posts_info['fmt_post_create_time']; $post_owner = stripslashes($posts_info['post_owner']); $color1 = "#CCFFCC"; $color2 = "#BFD8BC"; $posts_info = 0; $row_color = ($posts_info % 2) ? $color1 : $color2; //add to display $display_block .= " <tr> <td width=35% valign=top bgcolor=\"$row_color\"><p>$post_owner<br>[$post_create_time]</td> <td width=65% valign=top bgcolor=\"$row_color\"><p>$post_text<br><br> <a href=\"replytopost.php?post_id=$post_id\"><strong> REPLY TO POST</strong></a></td> </tr>"; } //close up the table $display_block .= "</table>"; ?> | |||
| Advertisements |
| | #2 | ||
| I have only been monitoring this group for 5 days now and have seen 10 questions involving alternate row colors for PHP tables. Being an intermediate PHP programmer myself, I know that there are at least twenty tutorials on how to accomplish this on any typical Google search by simply typing in "PHP Alternate Row Colors". I am going to regard any question on this topic nothing more than simple spam harvesting from now on. GFYself JD "Anthony Ritter" <tony@gonefishingguideservice.com> wrote in message news:3f42eb4d@news.greennet.net... > I'm trying to output a table with alternate background color rows. > > The following snippet outputs a table after connecting to a mySQL database > with posts but they all have the same bgcolor. > > Any help with the syntax will be greatly appreciated. > Thank you. > Tony Ritter > ........................................ > <? > > <table width=100% cellpadding=3 cellspacing=1 border=1> > <tr> > <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">AUTHOR</font> > </th> > <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">POST</font></th> > </tr>"; > > while ($posts_info = mysql_fetch_array($get_posts_res)) { > $post_id = $posts_info['post_id']; > $post_text = nl2br(stripslashes($posts_info['post_text'])); > $post_create_time = $posts_info['fmt_post_create_time']; > $post_owner = stripslashes($posts_info['post_owner']); > > $color1 = "#CCFFCC"; > $color2 = "#BFD8BC"; > $posts_info = 0; > $row_color = ($posts_info % 2) ? $color1 : $color2; > > //add to display > $display_block .= " > <tr> > <td width=35% valign=top > bgcolor=\"$row_color\"><p>$post_owner<br>[$post_create_time]</td> > <td width=65% valign=top bgcolor=\"$row_color\"><p>$post_text<br><br> > <a href=\"replytopost.php?post_id=$post_id\"><strong> REPLY TO > POST</strong></a></td> > > </tr>"; > > } > > //close up the table > $display_block .= "</table>"; > ?> > > > > > | |||
| | #3 | ||
| On 19-Aug-2003, "JKD" <dev@null.org> wrote: > I have only been monitoring this group for 5 days now and have seen 10 > questions involving alternate row colors for PHP tables. Being an > intermediate PHP programmer myself, I know that there are at least twenty > tutorials on how to accomplish this on any typical Google search by simply > typing in "PHP Alternate Row Colors". I am going to regard any question on > this topic nothing more than simple spam harvesting from now on. > > GFYself Kinda harsh for a noobe. This question comes up a lot because there are a lot of beginners here, not because of anything sinister. Most of us started as beginners. :-) The next time someone stomps on you because you asked or answered a question poorly consider it karma. -- Tom Thackrey www.creative-light.com | |||
| | #4 | ||
| "Anthony Ritter" <tony@gonefishingguideservice.com> wrote in message news:<3f42eb4d@news.greennet.net>... > > I'm trying to output a table with alternate background color rows. > > The following snippet outputs a table after connecting to a mySQL > database with posts but they all have the same bgcolor. OK, let's look at your code: > while ($posts_info = mysql_fetch_array($get_posts_res)) { This means that $posts_info is an array. > $row_color = ($posts_info % 2) ? $color1 : $color2; But here you expect $posts_info to be a number... You are asking PHP to convert an array into a number, which PHP can't really do, so it tries to first convert it into a boolean (which comes out to be TRUE, because empty arrays evaluate to FALSE, all other arrays to TRUE), and then convert that boolean into an integer (1). The end result: $posts_info in this expression always evaluates to 1. Try this instead: if ($row_color == $color1) { $row_color = $color2; } else { $row_color = $color1; } Cheers, NC | |||
| | #5 | ||
| On 19-Aug-2003, "Anthony Ritter" <tony@gonefishingguideservice.com> wrote: > I'm trying to output a table with alternate background color rows. > > The following snippet outputs a table after connecting to a mySQL database > with posts but they all have the same bgcolor. > > Any help with the syntax will be greatly appreciated. > Thank you. > Tony Ritter > ........................................ > <? > > <table width=100% cellpadding=3 cellspacing=1 border=1> > <tr> > <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">AUTHOR</font> > </th> > <th bgcolor=\"#497fbf\"><font color=\"#ffffff\">POST</font></th> > </tr>"; > > while ($posts_info = mysql_fetch_array($get_posts_res)) { > $post_id = $posts_info['post_id']; > $post_text = nl2br(stripslashes($posts_info['post_text'])); > $post_create_time = $posts_info['fmt_post_create_time']; > $post_owner = stripslashes($posts_info['post_owner']); > > $color1 = "#CCFFCC"; > $color2 = "#BFD8BC"; > $posts_info = 0; > $row_color = ($posts_info % 2) ? $color1 : $color2; > > //add to display > $display_block .= " > <tr> > <td width=35% valign=top > bgcolor=\"$row_color\"><p>$post_owner<br>[$post_create_time]</td> > <td width=65% valign=top > bgcolor=\"$row_color\"><p>$post_text<br><br> > <a href=\"replytopost.php?post_id=$post_id\"><strong> REPLY TO > POST</strong></a></td> > > </tr>"; > > } > > //close up the table > $display_block .= "</table>"; > ?> You're pretty close. You can't use $posts_info to switch colors, because you set it to 0 so it doesnt change. Add this before the while loop: $row_count = 0; change the color test to: $row_color = ($row_count % 2) ? $color1 : $color2; add this somewhere in the loop: $row_count ++; You should also move the initialization of $color1 and $color2 outside the loop or just stick the literals into the color test: $row_color = ($row_count % 2) ? '#ccffcc' : '#bfd8bc'; -- Tom Thackrey www.creative-light.com | |||
| Featured Websites | ||||
|
![]() |
| Tags: output, table |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem: How to create text output to a specified table? | Richard | HTML | 6 | 07-01-2007 2:05 PM |
| HD Output | Andy Middleton | New Users Help And FAQ | 5 | 06-26-2007 9:36 AM |
| Output? | Dr. Compynei | Car audio | 2 | 06-18-2007 12:21 PM |
| Delete from a table using entries in temp table | n00bie | Database | 2 | 06-10-2007 12:25 AM |
| Only output values from a table that are NOT NULL? | Bill Y. Barool | Database | 1 | 05-31-2007 8:43 PM |
| Featured Websites | ||||
|