checkbox

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
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

checkbox

Post 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++;
	}

}
?>
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

Post by zyklone »

please give some example of your problem. thanks!
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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++;
	}
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

it looks like you're overwriting your query result within your loop.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

so who would mind showing me more detail

thank much !!!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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"???
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post 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());
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You might want to use backticks (`) instead of single quotes (') on your field names.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

Why did I receive mess " warning : implode(): Bad arguments"???
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Read the manual in implode(). It expects a delimiter and an array.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

It seemed that UPDATE can't be with IN ???
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It should be able to. Basically it is telling the database server to do something where these records meet this criteria.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Post Reply