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>";
   }
?>

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

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>";
}
?>

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>";
}
?>

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?

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.

 

 

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?

$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 :/

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.