Jump to content

[SOLVED] Show table then alternate row colours...


dannyluked

Recommended Posts

Hi,

Not sure if this is the right board but...

I have this code:

<div id="content"><span id='maintitle'>Comments;</span>
<div id="spacer">  </div>
    <?php
   include "config.php";
   mysql_connect($server, $db_user, $db_pass) or die(mysql_error());

   mysql_select_db($database) or die(mysql_error());

$result = mysql_query("SELECT blog.title, blogcomments.blogid, blogcomments.commenttitle, blogcomments.name, blogcomments.date, blogcomments.time, blogcomments.userav, blogcomments.comment
FROM blog LEFT OUTER JOIN blogcomments ON blog.id = blogcomments.blogid
WHERE blog.id = '".$_GET['id']."'
ORDER BY blog.id") or die(mysql_error());

while($qry = mysql_fetch_array( $result )) {
echo "  <table width='497' height='114' border='0' cellspacing='0' cellpadding='0'>
    <tr>
      <td width='110' height='118' rowspan='2' align='center' valign='top' bgcolor='#FFFFFF' id='commentleft'><div id='name'>$qry[name]</div>
        <div id='avatarouter'><img src='$qry[userav]' id='avatar'></div>
        </td>
      <td width='387' height='94' valign='top' bgcolor='#FFFFFF' id='mainright'>
      <div id='comtitle'>$qry[commenttitle]</div>
      <div id='maincom'>$qry[comment]<br />
      </div>              
      </td>
    </tr>
    <tr>
      <td valign='top' bgcolor='#FFFFFF' id='cominfoholder' height='14'>
      <div id='cominfo'>Posted by <span id='smallname'>$qry[name]</span> on <span id='date'>$qry[date]</span> at <span id='time'>$qry[time]</span></div>
      </td>
    </tr>
  </table>";
}
?>
</div>

 

This displays my results the way I want. I have some things I would like to do though so here are my questions;

 

1. Is there anyway I can make this 'echo' only show if there is results for the table?

2. Is there anyway I can alternate the row background colours to make it the results clear?

Thanks for reading!!

 

PS. If the answer is yes to any of my questions please try and give me some guidence to achieve the results!

Compare your code and my code and find the differences

<div id="content"><span id='maintitle'>Comments;</span>
<div id="spacer">  </div>
    <?php
   include "config.php";
   mysql_connect($server, $db_user, $db_pass) or die(mysql_error());

   mysql_select_db($database) or die(mysql_error());

$result = mysql_query("SELECT blog.title, blogcomments.blogid, blogcomments.commenttitle, blogcomments.name, blogcomments.date, blogcomments.time, blogcomments.userav, blogcomments.comment
FROM blog LEFT OUTER JOIN blogcomments ON blog.id = blogcomments.blogid
WHERE blog.id = '".$_GET['id']."'
ORDER BY blog.id") or die(mysql_error());
echo "  <table width='497' height='114' border='0' cellspacing='0' cellpadding='0'>";
if(mysql_num_rows($result)>0){
while($qry = mysql_fetch_array( $result )) {
   if($bgcolor=="#cccccc")
  {
      $bgcolor="#fcfcfc";
  }else{
      $bgcolor="#cccccc";
  }
   echo" <tr bgcolor='$bgcolor'>
      <td width='110' height='118' rowspan='2' align='center' valign='top' bgcolor='#FFFFFF' id='commentleft'><div id='name'>$qry[name]</div>
        <div id='avatarouter'><img src='$qry[userav]' id='avatar'></div>
        </td>
      <td width='387' height='94' valign='top' bgcolor='#FFFFFF' id='mainright'>
      <div id='comtitle'>$qry[commenttitle]</div>
      <div id='maincom'>$qry[comment]<br />
      </div>              
      </td>
    </tr>
    <tr>
      <td valign='top' bgcolor='#FFFFFF' id='cominfoholder' height='14'>
      <div id='cominfo'>Posted by <span id='smallname'>$qry[name]</span> on <span id='date'>$qry[date]</span> at <span id='time'>$qry[time]</span></div>
      </td>
    </tr>";

}
}else{
  echo "<tr><td>No records found</td></tr>";
}
echo " </table>";
?>
</div>

Well you have 2 options.

 

1) Use PHP to decide which rows are alternate and supply a class="odd" to the <tr> element

2) Use CSS pseudo-selectors for "nth-child: background: black" or something along those lines (only works with CSS2+ compliant browsers)

Right, Thanks 'whatsmyname'. The second code worked perfectly.

 

Now I just need help in only displaying the table if there are results.

Also, all comments for my website are in one table so I cannot use odd/even numbers to define things. My MySQL table may look like this:

 

id    blogid  comment

1        5        .........

2        8        .........

3        5        .........

Right, Thanks 'whatsmyname'. The second code worked perfectly.

 

Now I just need help in only displaying the table if there are results.

Also, all comments for my website are in one table so I cannot use odd/even numbers to define things. My MySQL table may look like this:

 

id    blogid  comment

1        5        .........

2        8        .........

3        5        .........

same code works, i have put a condition, if there are result then it shows otherwise it shows "no result"

The code sadly dosent work properly. Before this code I have other code to query a database (code 1). Code 1 works off the id in the URL. If I put an ID in that doent exist in code 1 then it says 'no results' but if the id exists but there are no comments it still shows an empty comments box.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.