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 = "<P><em>You 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 = "
<P>Showing posts for the <strong>$topic_title</strong> topic:</p>
<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']);
$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_owner<br>[$post_create_time]</td>
<td width=65% valign=top class=\"$row_class\"><p>$post_text<br><br>
<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:#ffffff;
}
td {border:1px solid #0000ff;
font-family:arial;
font-size:.5em;
color: #2d73b9}
.light {
color:#000;
background-color:#eee;
}
.dark {
color:#000;
background-color:#aaa;
}
</style>
</head>
<body>
<title>Posts in Topic</title>
</head>
<body>
<h1>Posts 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 = "<P><em>You 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 = "
<P>Showing posts for the <strong>$topic_title</strong> topic:</p>
<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']);
//add to display
$display_block .= "
<tr>
<td width=35% valign=top><p>$post_owner<br>[$post_create_time]</td>
<td width=65% valign=top><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>";
}
?>
<html>
<head>
<style>
p {font-family:sans arial;
font-size: 1.25em;
}
th {
font-family:arial;
font-size: .75em;\
font-color:#ffffff;
}
td {border:1px solid #0000ff;
font-family:arial;
font-size:.5em;
color: #2d73b9}
.light {
color:#000;
background-color:#eee;
}
.dark {
color:#000;
background-color:#aaa;
}
</style>
</head>
<body>
<title>Posts in Topic</title>
</head>
<body>
<h1>Posts in Topic</h1>
<?php print $display_block; ?>
</body>
</html>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php