Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,352 members, 7,822,667 topics. Date: Thursday, 09 May 2024 at 02:49 PM

Help On Php Code Simpllification - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On Php Code Simpllification (1099 Views)

Using Finger Print Scanner On PHP / Help On Php Code For A Dice Game / Free Ebooks On Php Progaming (2) (3) (4)

(1) (Reply) (Go Down)

Help On Php Code Simpllification by GoodMuyis(m): 7:32pm On Aug 04, 2011
Pls look at the cod e below


<?PHP

$score =0;

if(isset($_POST["submit"])){


if($_POST["one"] == "yes"wink{$score += 1;}
if($_POST["two"] == "yes"wink{$score += 1;}

echo $score;

}else{
echo "
<form method='POST' action=''>
Select an Option 
Yes: <input type='radio' name='one' value='yes'/>
No: <input type='radio' name='one' value='no'/><br>
Select Option 2
Yes: <input type='radio' name='two' value='yes'/>
No: <input type='radio' name='two' value='no'/><br>

<input type='submit' name='submit' value='Submit'>
</form>";

}

?>



Pls is there a way of reducing the code in Bold such that i will just loop thru the html form and get the number of radio buttton with value='yes' which i can use for the value of my $score instead of repeating the if in number of times

regards
Re: Help On Php Code Simpllification by Fayimora(m): 11:26pm On Aug 04, 2011
[size=15pt]Yeah you could just use an array instead. So the name of each checkbox/radio shud be somefin like `arrayName[]`[/size]
Re: Help On Php Code Simpllification by GoodMuyis(m): 3:10pm On Aug 06, 2011
Fayimora:

[size=15pt]Yeah you could just use an array instead. So the name of each checkbox/radio shud be somefin like `arrayName[]`[/size]

pls can you give me sample code ?
Re: Help On Php Code Simpllification by Fayimora(m): 3:19pm On Aug 06, 2011
Ok.

sample form. .
<input type = 'checkbox' name = 'foods[]' value = 'chicken' />Chicken

<input type = 'checkbox' name = 'foods[]' value = 'beef' />Beef

<input type = 'checkbox' name = 'foods[]' value = 'pork' />Pork

OR
<input type = 'checkbox' name = 'foods[chicken]' value = 'chicken' />Chicken

<input type = 'checkbox' name = 'foods[beef]' value = 'beef' />Beef

<input type = 'checkbox' name = 'foods[pork]' value = 'pork' />Pork
dats if you want an associative array. You get your form data by POST and you store $_POST['foods'] into a variable  and just loop through it and do/check whatever you want. Give it  shot!
Re: Help On Php Code Simpllification by GoodMuyis(m): 8:10pm On Aug 06, 2011
<input type = 'checkbox' name = 'foods[]' value = 'chicken' />Chicken

You have forgotten that am using radio button  undecided
and i will have something like this


Question one: Blaa Bla weeee
A: <input type = 'radio' name = 'one' value = 'yes' />
B: <input type = 'radio' name = 'one' value = 'no' />
C: <input type = 'radio' name = 'one' value = 'no' />

up to like 10 Question
Re: Help On Php Code Simpllification by Fayimora(m): 8:54pm On Aug 06, 2011
Well radio/checkbox all the same to me. So ma point still holds.
Re: Help On Php Code Simpllification by Mobinga: 10:45pm On Aug 06, 2011
I suggest you change your 'one' to int 1 so you can loop


 
<?php
for($i = 1; $i<=10; $i++){
   
   if (($_POST["{$i}"]) == "yes"wink{score += 1;}
 
}
?>



Since you want an easy way out. It should work though. grin
Re: Help On Php Code Simpllification by GoodMuyis(m): 10:29am On Aug 08, 2011
Thats what i did exactly yesterday and it work out,

Thanks for your help
Re: Help On Php Code Simpllification by Mobinga: 10:32am On Aug 08, 2011
It did? Lol. Nice.

(1) (Reply)

Pls Able Programmers, Help! / Latent Semantic Indexing / HELP!! Java Programmers In The House Pls Help.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.