Jump to content

[SOLVED] number rows problem


bschultz

Recommended Posts

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!

Link to comment
Share on other sites

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!';
}

Link to comment
Share on other sites

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

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.