mmarif4u Posted April 16, 2007 Share Posted April 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
marcus Posted April 16, 2007 Share Posted April 16, 2007 To lower character count and file size, you can always use switch. switch($variable){ case 1: //php stuff break; case 2: //php stuff break; case 3: //php stuff break; //etc.... } Quote Link to comment Share on other sites More sharing options...
Hughesy1986 Posted April 16, 2007 Share Posted April 16, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 16, 2007 Author Share Posted April 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 16, 2007 Author Share Posted April 16, 2007 Its great heyray2 u did it,,, I will try to adopt it to my script. Thanks man. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 My pleasure. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 16, 2007 Author Share Posted April 16, 2007 I make some modification to it, and finally succeded in the task. Thanks again for ur help man. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.