Mr Chris Posted September 11, 2007 Share Posted September 11, 2007 Hi, Can someone please help me tidy up this query? <?php $query = mysql_query("SELECT * from somewhere;") or die(mysql_error()); $bg = '#eeeeee'; ?> <table class="player_stats" summary="Player stats" cellspacing="0"> <thead> <tr> <th>Pos</th> <th>Name</th> <th>Appearances</th> <th>Team Name</th> </tr> </thead> <tbody> <? $n=1; while($result = mysql_fetch_assoc($query)) { // Set the BG $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); ?> <tr> <td bgcolor="<?php echo $bg; ?>"><?php echo $n; ?></td> <td bgcolor="<?php echo $bg; ?>"><a href='profile.php?player_id=<?php echo $result['player_id']; ?>' class='link_text'><?php echo $result['player_name']; ?></a></td> <td bgcolor="<?php echo $bg; ?>"><?php echo $result['games_played']; ?></td> <td bgcolor="<?php echo $bg; ?>"><?php echo $result['team']; ?></td> </tr> <? $n++; } ?> </tbody> </table> Basically it works great, but I have two problems with it: 1) I want to get the opening <table> tags and <th> tags part of the query, so when there are no results that does not appear 2) if there are no results found I'd like a message saying 'no results found'. Would anyone be able to help me with this. I've tried fiddling with the code, but to no avail. Many Thanks Chris Quote Link to comment Share on other sites More sharing options...
Aeglos Posted September 11, 2007 Share Posted September 11, 2007 Try modifying the first block of code. Basically, if your $query variable is empty show a simple message, else continue with the rest of your code. <?php $query = mysql_query("SELECT * from somewhere;") or die(mysql_error()); $bg = '#eeeeee'; if (empty($query)) { echo 'No results found'; } else { ?> Do remember to include the closing bracket for that else at your last code block, after $n++ That would be a quick, dirty (and untested) solution, I hope. PS. Mixing shorthand tags (ie. <? ?>) with normal tags (ie. <?php ?>) is not the best of practices IMHO. In fact, it's best to avoid shorthands altogether unless you have a very good reason to use them and you can be 100% sure they will be always enabled server-side where you host your site, to avoid your code suddenly breaking with no apparent reason. Cheers. Quote Link to comment Share on other sites More sharing options...
Mr Chris Posted September 11, 2007 Author Share Posted September 11, 2007 Hi, Thanks, I've tried this and am still getting stuck: <?php $query = mysql_query("SELECT * from somewhere;") or die(mysql_error()); $bg = '#eeeeee'; if (empty($query)) { echo 'No results found'; } else { <table class="player_stats" summary="Player stats" cellspacing="0"> <thead> <tr> <th>Pos</th> <th>Name</th> <th>Appearances</th> <th>Team Name</th> </tr> </thead> <tbody> <? $n=1; while($result = mysql_fetch_assoc($query)) { // Set the BG $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); ?> <tr> <td bgcolor="<?php echo $bg; ?>"><?php echo $n; ?></td> <td bgcolor="<?php echo $bg; ?>"><a href='profile.php?player_id=<?php echo $result['player_id']; ?>' class='link_text'><?php echo $result['player_name']; ?></a></td> <td bgcolor="<?php echo $bg; ?>"><?php echo $result['games_played']; ?></td> <td bgcolor="<?php echo $bg; ?>"><?php echo $result['team']; ?></td> </tr> <? $n++; } ?> </tbody> </table> ?> I don't see how I can get around using php tags within the other else statement? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted September 11, 2007 Share Posted September 11, 2007 <?php $query = mysql_query("SELECT * from somewhere;") or die(mysql_error()); $bg = '#eeeeee'; if (empty($query)) { echo 'No results found'; } else { ?> //<-----ADD THIS also use code tags! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.