scale down 9 queries into one mysql with a while

Discussion in 'PHP' started by Dirty-Rockstar, Dec 19, 2008.

  1. #1
    The only way to properly explain this is to show you the wrong way of doing things, which i did not implement, however it is the only way I know of doing this. Which I know is so wrong.

    ok I have a table that holds numeric data. lets use money for example. I want to use html, to display a header, then under each header show the "users" that meet that criteria. Look below:

    
    print"
    <table>
    	<tr>
    		<td colspan='2'>Users and Money</td>
    	</tr>
    	<tr>
    		<td colspan='2'>$1,000,000+</td>
    	</tr>
    	<tr>
    		<td>User</td>
    		<td>Amount</td>
    	</tr>";
    $sql="SELECT user,money FROM user_table where money >= '1000000' order by money desc";
    $query=mysql_query($sql) or die(mysql_error());
    while($result=mysql_fetch_array($query)){
    	print "<tr>
    			<td>$result[user]</td>
    			<td>$result[money]</td>
    			</tr>
    			";
    }
    print"
    <tr>
    		<td colspan='2'>$900,000-$999,999</td>
    	</tr>
    	<tr>
    		<td>User</td>
    		<td>Amount</td>
    	</tr>";
    $sql="SELECT user,money FROM user_table where money >= '900000' and <='9999999' order by money desc";
    $query=mysql_query($sql) or die(mysql_error());
    while($result=mysql_fetch_array($query)){
    	print "<tr>
    			<td>$result[user]</td>
    			<td>$result[money]</td>
    		</tr>
    			";
    }
    print"
    <tr>
    		<td colspan='2'>$800,000-$899,999</td>
    	</tr>
    	<tr>
    		<td>User</td>
    		<td>Amount</td>
    	</tr>";
    $sql="SELECT user,money FROM user_table where money >= '800000' and <='899999' order by money desc";
    $query=mysql_query($sql) or die(mysql_error());
    while($result=mysql_fetch_array($query)){
    	print "<tr>
    			<td>$result[user]</td>
    			<td>$result[money]</td>
    		</tr>
    			";
    }
    
    
    PHP:

    etc,etc there are 9 queries in total im showing repetitive 3. but you get the idea. is it possible to just do 1 query, escape the while to add more html to add "headers" for each new set of money stats? I tried myself looking up foreach,and other methods. but im a little lost. sorry for the vars splitting up the mysql. I use it for debugging.

    something tells me i need to use "as" in the query. like select money as something where. I tried that too and the code exploded on me.
    Thanks for any help in the matter.
     
    Dirty-Rockstar, Dec 19, 2008 IP