Jump to content

[SOLVED] If esle problem


mmarif4u

Recommended Posts

Hi guys,

i have a script where i put 5 if statements:

if($_GET['cusers'] == 1){require_once('queryrenewal/query1.php');
}
if($_GET['cusers'] == 2){require_once('queryrenewal/query2.php');
} else {error("Your selected childrens are only 1");}
if($_GET['cusers'] == 3){require_once('queryrenewal/query3.php');
} else {error("Your selected childrens are only 2");}
if($_GET['cusers'] == 4){require_once('queryrenewal/query4.php');
} else {error("Your selected childrens are only 3");}
if($_GET['cusers'] == 5){require_once('queryrenewal/query5.php');
} else {error("Your selected childrens are only 4");}

Now the problem is when i select 1 child from the prev page and come to this page and put

one or two children numbers it gives me same error for all:

(Your selected childrens are only 2).

 

How can i put these else statements that it works with all correctly.

for example if a user have 1 child and he fill 2 textboxes then error appear that "your selected

childrens are only 1".

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/47189-solved-if-esle-problem/
Share on other sites

Would be better to write a switch.

 

Like:

 

<?php

$var = ""; // this is the value

switch ($var)
{
case "1":
require_once('queryrenewal/query1.php');
break;

case"2":
require_once('queryrenewal/query2.php');
break;

case"3":
require_once('queryrenewal/query3.php');
break;

case "4":
require_once('queryrenewal/query4.php');
break;

case "5":
require_once('queryrenewal/query5.php');
break;

}
?>

Thanks guys for reply.

Yeh i know about that to use switch.

but the problem is not that, that its not running, its working fine if i remove

the else command from every if.

Now i just want to show:

if he select 2 childrens in the previous page,

Then in the next page they have to put the 2 chldren numbers,(Note:

i provide 5 textboxes) If he put some thing in the 3rd textbox then the

error msg appear that your selected children are only 2.

 

hope now its clear.

 

Here is some code from a script I wrote a while back doing something similar to what you are trying to do. I've generalized it, but you could probably take the concept and tailor it to your needs:

 

<?php

if($_POST['submit']){
// Remove blank elements from the "items" array (text boxes that werent filled in)
$_POST['items'] = array_diff($_POST['items'], array(""));

// Verify a match between the number chosen in the select box, and the
// number of textboxes filled in
if($_POST['numitems'] == count($_POST['items'])){
	echo 'Match! '.$_POST['numitems'].' item(s) chosen. '.count($_POST['items']).' name(s) filled in.<br />';
                          require_once('queryrenewal/query'.$_POST['numitems'].'.php'); // YOU COULD DO YOUR REQUIRE HERE
} else {
	echo 'Mismatch! '.$_POST['numitems'].' item(s) chosen, but '.count($_POST['items']).' name(s) filled in.<br />';
}
}

// Specify the maximum numitems to show
$max_numitems = 5;

// Create the form
?>

<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Choose the number of items:
<select name="numitems">

<?php
for($i=1;$i <= $max_numitems;$i++){
	echo '<option value="'.$i.'">'.$i.'</option>';
}
?>

</select>

Enter name for each item:

<?php
for($i=1;$i <= $max_numitems;$i++){
	echo '<input type="text" name="items[]"></input>';
}
?>

<input type="submit" name="submit" value="Submit!">
</form>
</body>
</html>

 

I've consoliated choosing the number of items and entering the information for those items on the same page, but you could split that up into two pages pretty easily.

 

A working example is available (temporarily) at the following URL:

 

http://www.wearethereids.com/test/form_match/form_match.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.