interpim Posted March 13, 2007 Share Posted March 13, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 13, 2007 Share Posted March 13, 2007 try: $query="SELECT * FROM main ORDER BY level WHERE level > 0"; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 13, 2007 Share Posted March 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
interpim Posted March 13, 2007 Author Share Posted March 13, 2007 thanks for the input... I will have to try again later though... looks like my host just nuked my server... getting HTTP 400 error for several of my site folders. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 14, 2007 Author Share Posted March 14, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 you put the if statement in the correct place. try putting this one in the same spot: (($row['id'] == $current_user_id) ? ("")); Quote Link to comment Share on other sites More sharing options...
interpim Posted March 14, 2007 Author Share Posted March 14, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 don't put 'if' in front of my code. Quote Link to comment Share on other sites More sharing options...
interpim Posted March 14, 2007 Author Share Posted March 14, 2007 I did it both ways... with and without the if... still no output. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 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? Quote Link to comment Share on other sites More sharing options...
interpim Posted March 14, 2007 Author Share Posted March 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 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? Quote Link to comment Share on other sites More sharing options...
interpim Posted March 14, 2007 Author Share Posted March 14, 2007 $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 :/ Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 haha, i told you it was important no worries, tho. everybody makes obvious mistakes. practice makes perfect. 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.