Page 1 of 1

checkbox

Posted: Tue Jan 02, 2007 8:04 am
by hrubos
How to make condition to make loop after chosing by checkbox???
I use form so I have 2 pages : edit_display.php and edit_function.php

Code: Select all

$i=0;

while ($i < $num ) {

	$id_numberRoom=mysql_result($result,$i,"id_numberRoom");
	$number_room=mysql_result($result,$i,"number_room");
	$building=mysql_result($result,$i,"building");
	$floor=mysql_result($result,$i,"floor");
	$number_beg=mysql_result($result,$i,"number_beg");
	$price=mysql_result($result,$i,"price");
	$state_room=mysql_result($result,$i,"state_room");

?>
<tr>
<?php

$n = 0;

echo "<td width= '90'><div align= 'center'>$number_room</div></td>" ;
echo "<td width= '90'><div align= 'right'>$floor</div></td>";
echo "<td width= '90'><div align= 'right'>$building</div></td>";
echo "<td width= '90'><div align= 'right'>$number_beg</div></td>";
echo "<td width= '90'><div align= 'right'>$price</div></td>";
echo "<td width= '90'><div align= 'right'>$state_room</div></td>";
echo "<td width= '60'><div align= 'right'><input type='checkbox' name='id_numberRoom[]' value= '$id_numberRoom'></div></td>" ;
$i++;
}

Code: Select all

$id_numberRoom = $_POST['id_numberRoom'];
if(isset($_POST['submit'])) {
  for ($t=0; $t<count($id_numberRoom);$t++) {
	$query="SELECT DISTINCT p.id_numberRoom,p.number_room,p.state_room,
p.id_typeRoom,t.price
FROM room p,type_room t
WHERE p.id_typeRoom = t.id_typeRoom
     AND id_numberRoom = '$id_numberRoom[t]'";

	$result=mysql_query($query)or die(mysql_error());
	$num=mysql_numrows($result);
}


	$i=0;

	while ($i<$num){
		$id_numberRoom=mysql_result($result,$i,"id_numberRoom");
		$number_room=mysql_result($result,$i,"number_room");
		$price=mysql_result($result,$i,"price");
		$state_room=mysql_result($result,$i,"state_room");
?>
<tr>
<?php

echo "<td><input type='hidden' name='new_id_numberRoom' value='$id_numberRoom'></td>" ;
echo "<td><input type='text' name='new_number_room' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td><input type='text' name='new_price' size='10' value= '$price' size='8' style='font-family:Arial'></td>";
echo "<td><input type='text' name='new_state_room' size='10' value= '$state_room' size='8' style='font-family:Arial'></td>";
$i++;
	}

}
?>

Posted: Tue Jan 02, 2007 10:42 am
by zyklone
please give some example of your problem. thanks!

Posted: Tue Jan 02, 2007 11:52 am
by hrubos
When chosing 2 rows, output wil be return 2 results.

But in fact, mycode only return one last result.So example that :

I want to edit 2 rooms so, I do click on 2 checkbox, but result which I receive is the last cliking.

So, please help me how to make condition

I did that but still not run
edit_display.php

Code: Select all

echo "<td width= '60'><div align= 'right'><input type='checkbox' name='id_numberRoom[]' value= '$id_numberRoom'></div></td>" ;
edit_function.php

Code: Select all

$id_numberRoom = serialize($_POST['id_numberRoom']);
if(isset($_POST['submit'])) {
  
	$query="SELECT DISTINCT p.id_numberRoom,p.number_room,p.state_room,
p.id_typeRoom,t.price
FROM room p,type_room t
WHERE p.id_typeRoom = t.id_typeRoom
     AND id_numberRoom = '$id_numberRoom'";

	$result=mysql_query($query)or die(mysql_error());
	$num=mysql_numrows($result);

$i=0;
	while ($i<$num){
		$id_numberRoom=mysql_result($result,$i,"id_numberRoom");
		$number_room=mysql_result($result,$i,"number_room");
		$price=mysql_result($result,$i,"price");
		$state_room=mysql_result($result,$i,"state_room");
?>
<tr>
<?php

echo "<td width = '30'><input type='hidden' name='new_id_numberRoom' value='$id_numberRoom'></td>" ;
echo "<td width = '90'><input type='text' name='new_number_room' size='10' align='right' value= '$number_room' size='8' style='font-family:Arial'></td>" ;
echo "<td width = '90'><input type='text' name='new_price' size='10' align='right' value= '$price' size='8' style='font-family:Arial'></td>";
echo "<td width = '90'><input type='text' name='new_state_room' size='10' align='right' value= '$state_room' size='8' style='font-family:Arial'></div></td>";
$i++;
	}
}

Posted: Tue Jan 02, 2007 12:56 pm
by Burrito
it looks like you're overwriting your query result within your loop.

Posted: Tue Jan 02, 2007 12:57 pm
by aaronhall
Why serialize? $_POST['id_numberRoom'] is an array -- you would have to cycle through the array and append ANDs to your WHERE clause for each $_POST['id_numberRoom'] that is equal to "on", and then call that query and loop through those results.

You should check out mysql_fetch_assoc and foreach.

Posted: Tue Jan 02, 2007 1:26 pm
by hrubos
so who would mind showing me more detail

thank much !!!

Posted: Tue Jan 02, 2007 3:52 pm
by Burrito
I would use the 'IN' operator for MySQL and implode() your array into a string.

ex:

Code: Select all

$values = implode(",",$_POST['checkboxarray']);
$query = "SELECT * FROM `table` WHERE `whatever` IN ($values)";
obviously if your field is of type varchar etc, you'll need to add single quotes around your values.

Posted: Tue Jan 02, 2007 5:08 pm
by hrubos
Burrito wrote:I would use the 'IN' operator for MySQL and implode() your array into a string.

ex:

Code: Select all

$values = implode(",",$_POST['checkboxarray']);
$query = "SELECT * FROM `table` WHERE `whatever` IN ($values)";
obviously if your field is of type varchar etc, you'll need to add single quotes around your values.
So it's wonderful , it's clever command
thank much !!!

Ha'lo, it's value for query "UPDATE"???

Posted: Tue Jan 02, 2007 5:48 pm
by hrubos
Can I use ??? that

Code: Select all

$valId = implode(",",$_POST['new_id_numberRoom']);
$valNum = implode(",",$_POST['new_number_room']);
$valPri = implode(",",$_POST['new_price']);
$valSta = implode(",",$_POST['new_state_room']);

$query = "UPDATE room p,type_room t SET 'p.number_room' IN ($valNum),'t.price' IN ($valPri),
               'p.state_room' IN ($valSta)
                     WHERE 'p.id_numberRoom' IN ($valId); 
                        AND p.id_typeRoom = t.id_typeRoom ";
                        
                        
mysql_query($query) or die(mysql_error());

Posted: Tue Jan 02, 2007 6:09 pm
by RobertGonzalez
You might want to use backticks (`) instead of single quotes (') on your field names.

Posted: Tue Jan 02, 2007 6:31 pm
by hrubos
Why did I receive mess " warning : implode(): Bad arguments"???

Posted: Tue Jan 02, 2007 7:01 pm
by RobertGonzalez
Read the manual in implode(). It expects a delimiter and an array.

Posted: Tue Jan 02, 2007 7:19 pm
by hrubos
It seemed that UPDATE can't be with IN ???

Posted: Tue Jan 02, 2007 7:50 pm
by RobertGonzalez
It should be able to. Basically it is telling the database server to do something where these records meet this criteria.

Posted: Tue Jan 02, 2007 7:57 pm
by Burrito
hrubos wrote:It seemed that UPDATE can't be with IN ???
not the way you have it no. The IN works in the WHERE clause not in your SET directive.