Jump to content

Recommended Posts

Can anybody help me to check what's wrong with the coding.
First, i get the value of checkbox from the previous page.
So, if checkbox 1 and 2 is selected, result with result_id 4 will be shown.
if checkbox 1 and 3 is selected, result with result_id 5 will be shown.

Now the problem is...no matter which checkbox is selected, the program will only run 'if statement' ,
which is result_id 4.
Below is the coding.
Hope you guys can help me. >< .Thank alot!!

<?php
session_start();
if(isset($_GET['checkbox'])){
$checkbox = $_GET['checkbox'];
}

else {
header('Location:tutorial.php');
}

include("database.php");

for($i=0; $i < count($checkbox); $i++){
{
if ($checkbox[$i]= '1' && $checkbox[$i]= '2'){
$sql="SELECT * FROM result WHERE result.result_id = 4";}

elseif ($checkbox[$i]='1' && $checkbox[$i]= '3'){
$sql="SELECT * FROM result WHERE result.result_id = 5";}

else {}

}
}

$result=mysql_query($sql); 

echo "<form action = 'result.php' method='POST'> 
<br/>";
echo "<table>";
while($row = mysql_fetch_array($result))
{
$result_name=$row['result_name'];
$result_id=$row['result_id'];
$result_desc=$row['result_desc'];
$image=$row['image'];

echo "<h4>$result_name</h4> ";

echo "<tr >";
echo "<td>";
echo "</form>";?>
<img src="<?php echo $row ["image"];?>" width="500" height = "600" align="left" border="1" /><?php echo "</td>" ;


echo "</tr>";
}
echo "</table>";

?>

 

Link to comment
https://forums.phpfreaks.com/topic/277286-checkbox-ifelse-statement/
Share on other sites

One = is assignment, two ==s is comparison.

if ($checkbox[$i]== '1' && $checkbox[$i]== '2'){
	$sql="SELECT * FROM result WHERE result.result_id = 4";}

elseif ($checkbox[$i]== '1' && $checkbox[$i]== '3'){
	$sql="SELECT * FROM result WHERE result.result_id = 5";}
Next time, don't fiddle with the formatting of your post. Makes quoting awkward. Do use [code] tags around your code.

 

[edit] Other things:

1. header() will not stop your script. If you use it to redirect, exit; or die; immediately after.

2. else blocks are optional. If you don't need one then just leave it out.

3. You have an extra pair of {}s around your loop. It's not bad per se, just unusual and potentially confusing.

4. Only the last checkbox[$i] (with a value 1-4) will matter. Everything before it will be overwritten each time through the loop. Code that does this probably has some fundamental problem, like there only being one actual item or not putting something important, like output, inside the loop as well.

5. What happens if none of the checkboxes (I presume they were) were checked? Or something not 1-4? Even if it's not supposed to happen you should try to account for it. If you're not sure what to do you can redirect to the previous page.

6. Your HTML is very invalid.

Edited by requinix

Sorry, I'm new to forum. ><

there are 3 checkbox actually, i would like to display different result when different checkbox are selected.

the value of checkbox is get from the previous page.

 

the rule is:

IF checkbox=1 THEN result=1

IF checkbox=2 THEN result=2

IF checkbox=3 THEN result=3

IF checkbox=1 AND checkbox=2 THEN result=4

IF checkbox=1 AND checkbox=3 THEN result=5

 

IF checkbox=2 AND checkbox=3 THEN result=6

IF checkbox=1 AND checkbox=2 AND checkbox=3 THEN result=7

 

how will the code look like when i want a combination ? im keep trying but i can't solve it.

Can you please teach me? thank you

It's okay. I shouldn't be telling you not to use different fonts, actually: they're there to be used after all.

 

 

I'd take an unusual approach to this: sort the checkbox array, implode() all the values into one string, then look that up in an array.

$checkboxes = (array)$_GET['checkbox'];
sort($checkboxes, SORT_NUMERIC);
$checkboxstr = implode(',', $checkboxes);

$ids = array(
	'1' => 1,
	'2' => 2,
	'3' => 3,
	'1,2' => 4,
	'1,3' => 5,
	'2,3' => 6,
	'1,2,3' => 7
);
if(isset($ids[$checkboxstr])) {
	$id = $ids[$checkboxstr];
} else {
	// invalid combination of checkboxes
}
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.