dannyluked Posted August 4, 2009 Share Posted August 4, 2009 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! Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/ Share on other sites More sharing options...
watsmyname Posted August 4, 2009 Share Posted August 4, 2009 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> Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890598 Share on other sites More sharing options...
patrickmvi Posted August 4, 2009 Share Posted August 4, 2009 In watsmyname's example above, the first "{" after the if(mysql_num_rows($result)>0) statement is missing. Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890599 Share on other sites More sharing options...
aschk Posted August 4, 2009 Share Posted August 4, 2009 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) Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890604 Share on other sites More sharing options...
dannyluked Posted August 4, 2009 Author Share Posted August 4, 2009 Thanks for the quick reply. I have added the { in and the code still shows my results in the same way, They arnt alternating colours and they still show even if there is no comments sadly. Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890611 Share on other sites More sharing options...
watsmyname Posted August 4, 2009 Share Posted August 4, 2009 I missed brackets and "=" in the code, i have modified it, please look at it, you should get what you want. Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890612 Share on other sites More sharing options...
dannyluked Posted August 4, 2009 Author Share Posted August 4, 2009 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 ......... Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890618 Share on other sites More sharing options...
watsmyname Posted August 4, 2009 Share Posted August 4, 2009 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" Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890621 Share on other sites More sharing options...
dannyluked Posted August 4, 2009 Author Share Posted August 4, 2009 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. Link to comment https://forums.phpfreaks.com/topic/168805-solved-show-table-then-alternate-row-colours/#findComment-890735 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.