Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/68813-help-tidying-a-query/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/68813-help-tidying-a-query/#findComment-345861
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/68813-help-tidying-a-query/#findComment-345888
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.