[PHP] mysql output
The following code snippet outputs a table from a mySQL database. In version 1, I am able to get alternating background cell colors. However, the output gives me the same post five times. In version 2, I am able to receive five separate posts - which is what I'm looking for - but I'd like to get alternating cell backgrounds. Somewhere my syntax is failing me. Any advice would be greatly appreciated. Thank you. Tony Ritter // version 1: ?php //check for required info from the query string if (!$_GET[topic_id]) { header(Location: topiclist.php); exit; } //connect to server and select database $conn = mysql_connect(localhost, root, thisisapassword) or die(mysql_error()); mysql_select_db(sitename,$conn) or die(mysql_error()); //verify the topic exists $verify_topic = select topic_title from forum_topics where topic_id = $_GET[topic_id]; $verify_topic_res = mysql_query($verify_topic, $conn) or die(mysql_error()); if (mysql_num_rows($verify_topic_res) 1) { //this topic does not exist $display_block = PemYou have selected an invalid topic. Please a href=\topiclist.php\try again/a./em/p; } else { //get the topic title $topic_title = stripslashes(mysql_result($verify_topic_res,0, 'topic_title')); //gather the posts $get_posts = select post_id, post_text, date_format(post_create_time, '%b %e %Y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc; $get_posts_res = mysql_query($get_posts,$conn) or die(mysql_error()); //create the display string $display_block = PShowing posts for the strong$topic_title/strong topic:/p table width=100% cellpadding=3 cellspacing=1 border=1 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\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']); $i = 0; while ($posts_info = @mysql_fetch_array($get_posts_res)) {// begin light-dark loop $row_class = (($i % 2) == 0) ? 'light' : 'dark'; $display_block .= tr td width=35% valign=top class=\$row_class\p$post_ownerbr[$post_create_time]/td td width=65% valign=top class=\$row_class\p$post_textbrbr a href=\replytopost.php?post_id=$post_id\img src=\submit.gif\ border=\0\/a/td /tr; $i++; } } //close up the table $display_block .= /table; } ? html head style p {font-family:sans arial; font-size: 1.25em; } th { font-family:arial; font-size: .75em;\ font-color:#ff; } td {border:1px solid #ff; font-family:arial; font-size:.5em; color: #2d73b9} .light { color:#000; background-color:#eee; } .dark { color:#000; background-color:#aaa; } /style /head body titlePosts in Topic/title /head body h1Posts in Topic/h1 ?php print $display_block; ? /body /html . // version 2: ?php //check for required info from the query string if (!$_GET[topic_id]) { header(Location: topiclist.php); exit; } //connect to server and select database $conn = mysql_connect(localhost, root, thisisapassword) or die(mysql_error()); mysql_select_db(sitename,$conn) or die(mysql_error()); //verify the topic exists $verify_topic = select topic_title from forum_topics where topic_id = $_GET[topic_id]; $verify_topic_res = mysql_query($verify_topic, $conn) or die(mysql_error()); if (mysql_num_rows($verify_topic_res) 1) { //this topic does not exist $display_block = PemYou have selected an invalid topic. Please a href=\topiclist.php\try again/a./em/p; } else { //get the topic title $topic_title = stripslashes(mysql_result($verify_topic_res,0, 'topic_title')); //gather the posts $get_posts = select post_id, post_text, date_format(post_create_time, '%b %e %Y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc; $get_posts_res = mysql_query($get_posts,$conn) or die(mysql_error()); //create the display string $display_block = PShowing posts for the strong$topic_title/strong topic:/p table width=100% cellpadding=3 cellspacing=1 border=1 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\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']); //add to display $display_block .= tr td width=35% valign=topp$post_ownerbr[$post_create_time]/td
Re: [PHP] mysql output
* Thus wrote Anthony Ritter ([EMAIL PROTECTED]): The following code snippet outputs a table from a mySQL database. [snipped 255+ lines of code] That is way to much information, please be a brief in your code that you post. You'll find you'll get more people to analyze your code. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql output
From: Curt Zirzow [EMAIL PROTECTED] * Thus wrote Anthony Ritter ([EMAIL PROTECTED]): The following code snippet outputs a table from a mySQL database. [snipped 255+ lines of code] That is way to much information, please be a brief in your code that you post. You'll find you'll get more people to analyze your code. You also asked a very, very common question, i.e. how to alternate colors in table rows... there are a ton of websites/tutorials out there that explain ways to do this. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql output
You also asked a very, very common question, i.e. how to alternate colors in table rows... there are a ton of websites/tutorials out there that explain ways to do this. ---John Holmes... Apologies for the lengthy code. I've tried using a few tutorials and am still adrift. Here's a snippet: Thank you for any assistance. Tony Ritter .. ? table width=100% cellpadding=3 cellspacing=1 border=1 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\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_ownerbr[$post_create_time]/td td width=65% valign=top bgcolor=\$row_color\p$post_textbrbr a href=\replytopost.php?post_id=$post_id\strongREPLY TO POST/strong/a/td /tr; } //close up the table $display_block .= /table; ? . -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql output
Anthony Ritter wrote: You also asked a very, very common question, i.e. how to alternate colors in table rows... there are a ton of websites/tutorials out there that explain ways to do this. ---John Holmes... Apologies for the lengthy code. I've tried using a few tutorials and am still adrift. Here's a snippet: Thank you for any assistance. Tony Ritter .. ? table width=100% cellpadding=3 cellspacing=1 border=1 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\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; Move the above line outside of your while loop $row_color = ($posts_info % 2) ? $color1 : $color2; $row_color = ($posts_info++ % 2) ? $color1 : $color2; You were setting $posts_info to zero in each loop, so it's never going to change. You must set it to zero outside of the loop, then increment it within. You could make this real easy and just do: $row_color = ($row_color == $color1) ? $color2 : $color1; -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql output
Thanks very much John. I found the following and inserted it into my snippet for alternating backgoriund colors. However, I'm not sure I understand the logic. This part: ... $alternate = 2; 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']); if ($alternate == 1) { $color = #eaf3da; $alternate = 2; } else { $color = #d5eae9; $alternate = 1; } Best... Tony . $display_block = PShowing posts for the strong$topic_title/strong topic:/p table width=100% cellpadding=3 cellspacing=1 border=0 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\POST/font/th /tr; $alternate = 2; 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']); if ($alternate == 1) { $color = #eaf3da; $alternate = 2; } else { $color = #d5eae9; $alternate = 1; } //add to display $display_block .= tr td width=35% valign=top bgcolor=\$color\pa href=mailto:$post_owner$post_ownerbr/a[$post_create_time]/td td width=65% valign=top bgcolor=\$color\p$post_textbrbr a href=\replytopost.php?post_id=$post_id\img src=\reply.gif\ border=\0\ align=\right\/a/td /tr; } //close up the table $display_block .= /table; - Original Message - From: John W. Holmes [EMAIL PROTECTED] To: Anthony Ritter [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Tuesday, August 19, 2003 9:59 PM Subject: Re: [PHP] mysql output Anthony Ritter wrote: You also asked a very, very common question, i.e. how to alternate colors in table rows... there are a ton of websites/tutorials out there that explain ways to do this. ---John Holmes... Apologies for the lengthy code. I've tried using a few tutorials and am still adrift. Here's a snippet: Thank you for any assistance. Tony Ritter .. ? table width=100% cellpadding=3 cellspacing=1 border=1 tr th bgcolor=\#497fbf\font color=\#ff\AUTHOR/font /th th bgcolor=\#497fbf\font color=\#ff\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; Move the above line outside of your while loop $row_color = ($posts_info % 2) ? $color1 : $color2; $row_color = ($posts_info++ % 2) ? $color1 : $color2; You were setting $posts_info to zero in each loop, so it's never going to change. You must set it to zero outside of the loop, then increment it within. You could make this real easy and just do: $row_color = ($row_color == $color1) ? $color2 : $color1; -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals www.phparch.com --- [This E-mail scanned for viruses by gonefishingguideservice.com] --- [This E-mail scanned for viruses by gonefishingguideservice.com] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql output
Anthony Ritter wrote: I found the following and inserted it into my snippet for alternating backgoriund colors. However, I'm not sure I understand the logic. [snip] if ($alternate == 1) { $color = #eaf3da; $alternate = 2; } else { $color = #d5eae9; $alternate = 1; } It's just a long way of alternating colors. When $alternate is one, color1 is set, then $alternate is set to two. The next time through the loop, the color will be set to color2, then $alternate is set back to one. Then the cycle repeats. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ PHP|Architect: A magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php