bschultz Posted August 29, 2009 Share Posted August 29, 2009 I need to check a database for how many rows, and... - if there are less than 62 rows, print them out - if there are more than 62 rows, print out the first half in one table column...and then print out the rest in a second table column. There will NEVER be more than 124 rows! Here's what I have so far: <?php mysql_select_db('2009fb',$dbc); $sql = "SELECT * FROM bschultz_own"; $rs = mysql_query($sql,$dbc); $howmany = mysql_num_rows($rs); $halfhowmany = round($howmany / 2); $halfplus1 = ($halfhowmany + 1); echo $howmany; echo $halfhowmany; echo $halfplus1; if ($howmany <= 62){ $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; echo "<table border='0' cellspacing='0' cellpadding='0'><tr><td width='150' valign='top' align='left'><div class='roster'>$row[number] $row[name]</div></td>"; } } elseif ($howmany >= 63){ $sql2 = "SELECT * FROM bschultz_own LIMIT 2,$halfhowmany"; $rs2 = mysql_query($sql2,$dbc); $matches2 = 0; while ($row2 = mysql_fetch_assoc($rs2)) { $matches2++; echo "<table border='0' cellspacing='0' cellpadding='0'><tr><td width='150' valign='top' align='left'><div class='roster'>$row[number] $row[name]</div></td>"; } $sql3 = "SELECT * FROM bschultz_own LIMIT $halfplus1,$halfhowmany"; $rs3 = mysql_query($sql3,$dbc); $matches3 = 0; while ($row3 = mysql_fetch_assoc($rs3)) { $matches3++; echo "<td width='150' valign='top' align='left'><div class='roster'>$row[number] $row[name]</div></td>"; } } ?> The only thing that is printing is the three echo statements for $howmany $halfhowmany and $halfplus1. Any ideas? Thanks! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 29, 2009 Share Posted August 29, 2009 You don't count the number of rows by issuing a SELECT * and then using mysql_num_rows(). If you wan't to count records you use COUNT(*), like so: $q = mysql_query( "select count(*) as `n` from `the_table`" ); if( $q ) { $row = mysql_fetch_object( $q ); echo $row->n . ' rows!'; } Quote Link to comment Share on other sites More sharing options...
bschultz Posted August 30, 2009 Author Share Posted August 30, 2009 That did it...thanks! <?php mysql_select_db('2009fb',$dbc); $q = mysql_query( "select count(*) as `n` from `bschultz_own`" ); if( $q ) { $row = mysql_fetch_object( $q ); // echo $row->n . ' rows!'; } $numbers = $row->n; $howmany = $numbers; $halfhowmany = round($howmany / 2); $halfplus1 = ($halfhowmany + 1); if ($howmany <= 62){ $sql1 = "SELECT * FROM bschultz_own"; $rs = mysql_query($sql,$dbc); $matches = 0; while ($row1 = mysql_fetch_assoc($rs)) { $matches++; echo "<table border='0' cellspacing='0' cellpadding='0'><tr><td width='150' valign='top' align='left'><div class='roster'>$row1[number] $row1[name]</div>"; } echo "</td>"; } elseif ($howmany >= 63){ $sql2 = "SELECT * FROM bschultz_own LIMIT 2,$halfhowmany"; $rs2 = mysql_query($sql2,$dbc); $matches2 = 0; echo "<table border='0' cellspacing='0' cellpadding='0'><tr><td width='150' valign='top' align='left'>"; while ($row2 = mysql_fetch_assoc($rs2)) { $matches2++; echo "<div class='roster'>$row2[number] $row2[name]</div>"; } echo "</td>"; $sql3 = "SELECT * FROM bschultz_own LIMIT $halfplus1,$halfhowmany"; $rs3 = mysql_query($sql3,$dbc); $matches3 = 0; echo "<td width='150' valign='top' align='left'>"; while ($row3 = mysql_fetch_assoc($rs3)) { $matches3++; echo "<div class='roster'>$row3[number] $row3[name]</div>"; } echo "</td>"; } ?> 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.