Query the Query Results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

Query the Query Results

Post by transfield »

Hello,
I've queried a MySql database & displayed the query results succesfully using the code below. I now want to select certain records only by ticking a checkbox next to the query results & re-query these selected records. How do I do that? Thanks for your help. My code is below:-

Code: Select all

<?php 
$query2=("SELECT * FROM table"); 
$result2=mysql_query($query2); 
?> 
<table width=700 border=1 height="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0"><tr bgcolor ="cyan"> 
<td width="375" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Raw Data</a></font></strong></td> 
<td width="60" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Size</font></strong></td> 
<td width="60" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Price</font></strong></td> 
<td width="80" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Untick Bad Data</font></strong></td> 
</tr> 
<?php 
//the code below is to display the results in multi colours 
$color="1"; 
while($rows=@mysql_fetch_array($result2)){   
if($color==1){ 
echo "<tr bgcolor='#CCFFFF'> 
<td align=left><font face=arial size=1.5>{$rows['full_add']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['size']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['price']}</td></font> 
<td align=center><input type='checkbox' name='C1' value='ON' checked></td></font> 
</tr>"; 
// Set $color==2, for switching to other color 
$color="2"; 
} 

// When $color not equal 1, use this table row color 
else { 
echo "<tr bgcolor='#FFFFFF'> 
<td align=left><font face=arial size=1.5>{$rows['full_add']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['size']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['price']}</td></font> 
<td align=center><input type='checkbox' name='C1' value='ON' checked></td></font> 
</tr>"; 
// Set $color back to 1 
$color="1"; 
} 
} 
echo '</table>'; 
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Output the results into a form, allow each one to have a check box, then on the next processing page, do your coding magic to search based on the selected results.
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

Post by transfield »

Thanks for your reply, m3mn0n.
allow each one to have a check box
I don't know how to do this part and how to declare the check box as a variable. I appreciate your help on this matter. This is what I've done so far:-

Code: Select all

$query2=("SELECT * FROM table"); 
$result2=mysql_query($query2); 
?> 
<table width=700 border=1 height="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0"><tr bgcolor ="cyan"> 
<td width="375" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Raw Data</a></font></strong></td> 
<td width="60" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Size</font></strong></td> 
<td width="60" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Price</font></strong></td> 
<td width="80" align="center" bgcolor="#CCFFFF"><b><font face="arial" size="2.5">Untick Bad Data</font></strong></td> 
</tr> 
<?php 
//the code below is to display the results in multi colours 
$color="1"; 
while($rows=@mysql_fetch_array($result2)){    
if($color==1){ 
echo "<tr bgcolor='#CCFFFF'> 
<td align=left><font face=arial size=1.5>{$rows['full_add']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['size']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['price']}</td></font> 
<td align=center><input type='checkbox' name='C1' value='ON' checked></td></font> 
</tr>"; //I created the check boxes above. Not sure if this is the right thing to do.
// Set $color==2, for switching to other color 
$color="2"; 
} 

// When $color not equal 1, use this table row color 
else { 
echo "<tr bgcolor='#FFFFFF'> 
<td align=left><font face=arial size=1.5>{$rows['full_add']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['size']}</td></font> 
<td align=center><font face=arial size=1.5>{$rows['price']}</td></font> 
<td align=center><input type='checkbox' name='C1' value='ON' checked></td></font> 
</tr>"; //I created the check boxes above. Not sure if this is the right thing to do.
// Set $color back to 1 
$color="1"; 
} 
} 
echo '</table>'; 
?>
<table width="700" border="0">
<?php
switch($_POST['category'])
	{
	case 'condo':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_condo' type='submit' id='submit_condo' value='Re-calculate Condo'>";
echo "</td></tr></form>";
break;

	case 'house':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_house' type='submit' id='submit_house' value='Re-calculate Houses'>";
echo "</td></tr></form>";
break;

	case 'factory':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_factory' type='submit' id='submit_factory' value='Re-calculate Factory'>";
echo "</td></tr></form>";
break;

	case 'land':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_land' type='submit' id='submit_land' value='Re-calculate Land'>";
echo "</td></tr></form>";
break;

	case 'office':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_office' type='submit' id='submit_office' value='Re-calculate Office'>";
echo "</td></tr></form>";
break;

	case 'shop':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_shop' type='submit' id='submit_shop' value='Re-calculate Shop'>";
echo "</td></tr></form>";
	}
?>
</table>
<?php
$submit_house = $_POST['submit_house'];
$C1 = $_POST['C1']; //this is supposed to be the variable for the check boxes but it does not work.
if(isset($submit_house))
{
$username="something";
$password="something_else";
$database="house_values";
$host="localhost";
mysql_connect ("$host","$username","$password");
mysql_select_db($database) or die( "Where's the database man?");

$query5=("SELECT * FROM temp");
}
$result5=mysql_query($query5);
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Simply place a loop within <form> and </form> and have the loop output something like

Code: Select all

<input type="checkbox" name="recalc" value="$nameoftherow" /> $nameoftherow
You only need 1 form and 1 submit button.
transfield
Forum Commoner
Posts: 34
Joined: Wed Feb 22, 2006 6:00 am

Post by transfield »

My uderstanding of placing a loop within the <form> </form> tags is something like this. Nevertheless, it did not work. What type of loop are you referring to?

Code: Select all

//the switch started above this line
case 'house':
echo "<form name='form5' method='POST' action=''>";
echo "<tr bgcolor='#CCFFFF'><td align=right>";
echo "<input name='submit_house' type='submit' id='submit_house' value='Re-calculate Houses'>";
while($rows=@mysql_fetch_array($result2)){
echo "<td align=center><input type='checkbox' name='recalc' value='$nameoftherow' />"
}
echo "</td></tr></form>";
break;
//the switch continues below this line
Post Reply