Jump to content

[SOLVED] displaying database and getting unwanted results...


interpim

Recommended Posts

OK, basically I want to display everyone in the database, but only if the 'level' field is larger than 0.  Well, this works fine for most of the database... but if the last entry in the database is 0, then it prints it anyways.

 

Is there anyway around this?  I tried adding an if statement before the last bit where I echo out the table rows, but it was killing the entire table, or still showing the same results...

 

<?php
include('config.php');
include('includes/functions.php');
loggedin_check();

mysql_connect($server,$username,$password);   
mysql_select_db($database) or die("unable to select database because ".mysql_error());  

$query="SELECT * FROM main ORDER BY level";    
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

while ($row = mysql_fetch_assoc($result)) 
   {
   echo "<center><table border='2'><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td></tr>";
   $i=0;
   while ($i < $num) 
      {
      if (mysql_result($result,$i,'level')== 0)
         {
         $i++;
         }
      $id=mysql_result($result,$i,'id');
      $name=mysql_result($result,$i,'toon_name');
      $level=mysql_result($result,$i,'level');
      $wins=mysql_result($result,$i,'win');
      $loss=mysql_result($result,$i,'loss');
      echo "<tr><td>$id</td><td>$name</td><td>$level</td><td>$wins</td><td>$loss</td><td><a href='battle.php?atk=$id'>ATTACK!</a></td></tr>";
      $i++;
      }  
   echo "</table></center>";
   }
?>

Link to comment
Share on other sites

If you only want to display the record where the "level > 0", only retrieve those records.

<?php
$query="SELECT * FROM main WHERE level > 0 ORDER BY level";    
$result=mysql_query($query) or die("Problem with the query: <pre>$query</pre><br>" . mysql_error());
$num=mysql_num_rows($result);
if ($num > 0) {
       echo "<center><table border='2'><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td></tr>";
       while ($row = mysql_fetch_assoc($result)) 
       {
            echo '<tr><td>' . $row['id'] . '</td><td>' . $row['name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['wins'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>';
       }
       echo "</table></center>";
}
?>

 

Ken

Link to comment
Share on other sites

Great that worked for not showing the less than 0 fields...

 

Now, is there a way to keep the current user from being displayed? 

 

I tried putting an if statement that checked the cookie variable and not print that data... but it didn't work.

 

<?php
include('config.php');
include('includes/functions.php');
loggedin_check();
$user=$_COOKIE['user'];

mysql_connect($server,$username,$password);   
mysql_select_db($database) or die("unable to select database because ".mysql_error()); 

$query="SELECT * FROM main WHERE level > 0 ORDER BY level";    
$result=mysql_query($query) or die("Problem with the query: <pre>$query</pre><br>" . mysql_error());
$num=mysql_num_rows($result);
if ($num > 0) {
              echo "<center><table border='2'><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td></tr>";
       while ($row = mysql_fetch_assoc($result)) 
       {
           // THIS IS WHERE I PUT THE IF STATEMENT... BUT IT DIDN'T WORK

            echo '<tr><td>' . $row['id'] . '</td><td>' . $row['toon_name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['win'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>';
       }
       echo "</table></center>";
}
?>

Link to comment
Share on other sites

breaking the output with that... code now is

<?php
include('config.php');
include('includes/functions.php');
loggedin_check();
$user=$_COOKIE['user'];

mysql_connect($server,$username,$password);   
mysql_select_db($database) or die("unable to select database because ".mysql_error()); 

$query="SELECT * FROM main WHERE level > 0 ORDER BY level";    
$result=mysql_query($query) or die("Problem with the query: <pre>$query</pre><br>" . mysql_error());
$num=mysql_num_rows($result);
if ($num > 0) {
              echo "<center><table border='2'><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td></tr>";
       while ($row = mysql_fetch_assoc($result)) 
       {
           if (($row['id'] == $user) ? (""));
            echo '<tr><td>' . $row['id'] . '</td><td>' . $row['toon_name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['win'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>';
       }
       echo "</table></center>";
}
?>

Link to comment
Share on other sites

try this:

if ($num > 0) {
              echo "<center><table border='2'><tr><td>ID</id><td>NAME</td><td>LEVEL</td><td>WINS</td><td>LOSSES</td></tr>";
       while ($row = mysql_fetch_assoc($result)) 
       {
           if($row['id'] != $user){
                   echo '<tr><td>' . $row['id'] . '</td><td>' . $row['toon_name'] . '</td><td>' . $row['level'] . '</td><td>' . $row['win'] . '</td><td>' . $row['loss'] . '</td><td><a href="battle.php?atk=' . $row['id'] . '">ATTACK!</a></td></tr>';
           }
       }
       echo "</table></center>";
}

 

also, what is the value of $user? is it the id, the name, or the level?

Link to comment
Share on other sites

OK... the output is working again... but still showing current user in the table....

 

It would probably be easier to just to have the script I link to at the end check if it is current user and kick it back with "You cannot attack yourself" LOL.

 

 

Link to comment
Share on other sites

OK... the output is working again... but still showing current user in the table....

 

It would probably be easier to just to have the script I link to at the end check if it is current user and kick it back with "You cannot attack yourself" LOL.

 

 

 

that would involve the exact same type of if statement... again, what is the output of $user? is it stored in id, toon_name, level, what?

Link to comment
Share on other sites

$user is pulled from a cookie...

 

when I echo user to check if it holds any data it prints the right information.

 

LOL... and reading what you just posted just made me realize I need to pull the user_name field from the database...

 

Thanks for your help.  I can't believe how stupid this makes me feel :/

Link to comment
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.