Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,771 members, 8,003,729 topics. Date: Friday, 15 November 2024 at 06:14 PM |
Nairaland Forum / Science/Technology / Webmasters / Very Easy Html/php Quiz :) (2968 Views)
Website Developer Needed To Design A Single Page HTML/PHP / Most Confusing And Simple Maths Quiz / Help With Html/php Form Preview (2) (3) (4)
Very Easy Html/php Quiz :) by DualCore1: 4:07pm On Sep 22, 2010 |
Well, for me this no be quiz na bug. I'm working on something very complex but I have simplified part of it to pure html and php lets say i have the following PHP code //i have used select statement to get some data from the mysql database and its result is what i have put in $result. while($row = mysql_fetch_array($result)){ $db_stuff1[] = $row['stuff1']; $db_stuff2[] = $row['stuff2']; $db_stuff3[] = $row['stuff3']; $db_event_id[] = $row['event_id']; } $event_count = count($db_event_id); for($i=0; $i < $event_count; $i++){ echo'<tr> <td><input type="radio" name="'.$db_event_id[$i].'" value="stuff1" checked="checked">'.$db_stuff1[$i].'</br></td> <td><input type="radio" name="'.$db_event_id[$i].'" value="stuff2">'.$db_stuff2[$i].'</br></td> <td><input type="radio" name="'.$db_event_id[$i].'" value="stuff3">'.$db_stuff3[$i].'</br></td> </tr>'; } lets assume this loop goes through 3 iterations. the html source code will look like so: <input type = "radio" name = "food" value="stuff1" checked="checked">rice</br> <input type = "radio" name = "food" value="stuff2">beans</br> <input type = "radio" name = "food" value="stuff3">Pop Corn</br> <input type = "radio" name = "type" value="stuff1" checked="checked">flat</br> <input type = "radio" name = "type" value="stuff2">pointed</br> <input type = "radio" name = "type" value="stuff3">tiny</br> <input type = "radio" name = "make" value="stuff1" checked="checked">nokia</br> <input type = "radio" name = "make" value="stuff2">iphone</br> <input type = "radio" name = "make" value="stuff3">china phone</br> <input type = "submit" value = "submit" name = "submit" id = "submit" /> Hoping you understand to this point. What I want done is. . . what is it sef? Ok, what I want is not straightforward at all but let me get it down to one question.\ How do you get the values of multiple radio button groups parsed to php. Note, they are dynamically generated. If this was static it would be $var = $_POST['make']; or $var = $_POST['type']; e.t.c I am praying one mad geek understands this whole thing and wont ask me to explain further. If you need further explanation on any part of this thing please ask questions. For the first time ever, google has failed or na me don run out of search keywords |
Re: Very Easy Html/php Quiz :) by chukxy: 5:10pm On Sep 22, 2010 |
Hi Dual Core , I don't think is complex as such, just throw more light on what you set to achieve, let me see what I can do. |
Re: Very Easy Html/php Quiz :) by DualCore1: 5:11pm On Sep 22, 2010 |
Update: A lady actually told me to take a walk and come back to this later. I didnt take a walk but i went to do some domestic stuff to clear my head. Then I thought of something. . . Javascript! Ok, so I am going to try to use Javascript as the bridge somehow. Will update this thread if it works or not. chukxy:I dont mean to stress u too much but can you ask questions as to where isnt very clear? |
Re: Very Easy Html/php Quiz :) by chukxy: 5:21pm On Sep 22, 2010 |
I want to be sure the part you are facing challenge. Is it the point you want to retrieve the value of selected radio button from the group? Or is it the part you want to assign value to radio button with database result? |
Re: Very Easy Html/php Quiz :) by DualCore1: 5:43pm On Sep 22, 2010 |
chukxy: yes retrieve value of selected button from each group. There are three groups there. Please note that they are generated dynamically based on the value of $event_count. chukxy:No, that is what I have done already using the WHILE and FOR loop. |
Re: Very Easy Html/php Quiz :) by chukxy: 5:52pm On Sep 22, 2010 |
Oga Dual Core , I was waiting for your response. |
Re: Very Easy Html/php Quiz :) by DualCore1: 6:21pm On Sep 22, 2010 |
Bro I don answer na, this thing is scattering my dada |
Re: Very Easy Html/php Quiz :) by chukxy: 7:20pm On Sep 22, 2010 |
Ok, if it is to retrieve the value from the selected button group, the following would be of help: <html> <head> </head> <body> <form method="post" action="radio.php"> <input type = "radio" name = "food[0]" value="stuff1" checked="checked">rice</br> <input type = "radio" name = "food[0]" value="stuff2">beans</br> <input type = "radio" name = "food[0]" value="stuff3">Pop Corn</br> <input type = "radio" name = "food[1]" value="stuff10" checked="checked">flat</br> <input type = "radio" name = "food[1]" value="stuff2">pointed</br> <input type = "radio" name = "food[1]" value="stuff3">tiny</br> <input type = "radio" name = "food[2]" value="stuff25" checked="checked">nokia</br> <input type = "radio" name = "food[2]" value="stuff2">iphone</br> <input type = "radio" name = "food[2]" value="stuff3">china phone</br> <input type="submit" value="printselect"> </form> </body> </html> Then the code below would retrieve the values: <? for($d=0;$d<3;$d++) { echo $_POST[food][$d]; } ?> I tried it, I was able to retrieve the values. Basically, you need to use array in order to loop and get the values. I hope this helped else I would try further. |
Re: Very Easy Html/php Quiz :) by DualCore1: 7:29pm On Sep 22, 2010 |
You're good, I was about updating this thread saying I had solved it and I saw your solution and its the same principle. I got the solution from http://stackoverflow.com/questions/1656260/php-multiple-radio-buttons its similar to what i have in mind although i need to add a few things Your solution may not really fit what I want it for as everything is dynamic, even the name of the radio groups. If I use urs, I may end up getting\ <input type = "radio" name = "food[1]" value="stuff1" checked="checked">rice</br> <input type = "radio" name = "food[2]" value="stuff2">beans</br> <input type = "radio" name = "food[3]" value="stuff3">Pop Corn</br> <input type = "radio" name = "food[4]" value="stuff10" checked="checked">flat</br> <input type = "radio" name = "food[5]" value="stuff2">pointed</br> <input type = "radio" name = "food[6]" value="stuff3">tiny</br> <input type = "radio" name = "food[7]" value="stuff25" checked="checked">nokia</br> <input type = "radio" name = "food[8]" value="stuff2">iphone</br> <input type = "radio" name = "food[9]" value="stuff3">china phone</br> But for you to think this up, you're good. Hat down for you. Thanks for your time. I will post my code when I finish writing it but its no different from yours in principle. |
Re: Very Easy Html/php Quiz :) by DualCore1: 7:38pm On Sep 22, 2010 |
Here's what I have done I have changed the naming pattern for the radio groups while($row = mysql_fetch_array($result)){ $db_stuff1[] = $row['stuff1']; $db_stuff2[] = $row['stuff2']; $db_stuff3[] = $row['stuff3']; $db_event_id[] = $row['event_id']; } $event_count = count($db_event_id); for($i=0; $i < $event_count; $i++){ echo'<tr> <td><input type="radio" name="item_'.$db_event_id[$i].'" value="stuff1" checked="checked">'.$db_stuff1[$i].'</br></td> <td><input type="radio" name="item_'.$db_event_id[$i].'" value="stuff2">'.$db_stuff2[$i].'</br></td> <td><input type="radio" name="item_'.$db_event_id[$i].'" value="stuff3">'.$db_stuff3[$i].'</br></td> </tr>'; } <input type = "submit" value = "submit" name = "submit" id = "submit" /> When the form is submitted, I run it through a loop to get the value and go on to do what i want with the value gotten. for($i = 1; $i < 50; $i++){ if(isset($_POST["item_$i"])){ if($_POST["item_$i"] == "stuff1"{ //do something } if($_POST["item_$i"] == "stuff2"{ //do something } if($_POST["item_$i"] == "stuff3"{ //do something } } } /hope this is helpful for someone else someday. Chukxy, thank you. |
Re: Very Easy Html/php Quiz :) by yawatide(f): 8:42pm On Sep 22, 2010 |
Hmm, I thought the whole point of a radio button is that you click only one value which means that all names are to be the same. I guess I am missing something I would have imagined in your database, you have one table for radio button names (for lack of a better word) and another that has a 1-to-1 relationship between a value and the name of the radio button, at the very least. In other words: table 1 would be: tbl_radio_groups { 1: food 2: cars } and table 2 would be: tbl_radio_options { amala: 1 ewedu: 1 hummer: 2 camry: 2 } Then in your php, you just query both tables where the unique ID of one is equal to the sec. ID of the other and loop accordingly. Again, I guess I am missing something. What do I know? |
Re: Very Easy Html/php Quiz :) by DualCore1: 9:07pm On Sep 22, 2010 |
yawa-ti-de:Your guess is as good as mine. Cant explain but i dont think that wont have worked for what I am doing. If you translate your sentences into pseudo-codes maybe it will aid understanding. |
Re: Very Easy Html/php Quiz :) by yawatide(f): 9:28pm On Sep 22, 2010 |
Let's leave it at that then. If need be, you can bring it offline. |
Re: Very Easy Html/php Quiz :) by chukxy: 11:44pm On Sep 22, 2010 |
Hi Dual, with the solution you posted I was able to get clearer picture of what you intend to achieve; hence I decided to modify my code to achieve that. The code is pasted below: <? $db_name="nairaland"; $connection=@mysql_connect('localhost','spike','9sj7En4') or die(mysql_error()); $db=@mysql_select_db($db_name,$connection) or die(mysql_error()); $sql ="select * from radiobutton"; $result = mysql_query($sql) or die(mysql_error()); $form_block=" <FORM METHOD=\"POST\" ACTION=\"radio.php\"> <table cellspacing=5 cellpadding=5> <tr><th>Number</th><th>Choice1</th><th>Choice2</th><th>Choice3</th> </tr>"; while($row = mysql_fetch_array($result)) { $form_block.=" <tr> <td>$row[id]</td> <td><input type=\"radio\" name=\"item[$row[id]]\" value=\"$row[choice1]\" checked=\"checked\">$row[choice1]</td> <td><input type=\"radio\" name=\"item[$row[id]]\" value=\"$row[choice2]\">$row[choice2]</td> <td><input type=radio name=\"item[$row[id]]\" value=\"$row[choice3]\">$row[choice3]</td> <br/> </tr>"; } $form_block.="<tr><td align=center colspan=3><input type=\"submit\" value=\"Get Result\"></td> </tr> </table> </form>"; ?> <html> <head> <title>Dynamic Generation of Radio Button </title> </head> <body> <? echo "$form_block"; ?> </body> </html> The code above dynamically draws the radio button irrespective of the number stored in the database. The code below dynamically get the values selected by the user <? for($d=0;$d<=count($_POST[item]);$d++) { echo $_POST[item][$d]; echo "<br/>"; } ?> The attached the screen shoot of the drawn table.
|
Re: Very Easy Html/php Quiz :) by DualCore1: 8:26am On Sep 23, 2010 |
Perfect. This will work too. Thanks for your time and brain juice Yawa thanks for stopping by also. Funny thing is, during planning and analysis I estimated to do this in 20 minutes. 20 minutes turned to a whole evening. |
Re: Very Easy Html/php Quiz :) by ogzille(m): 8:49am On Sep 23, 2010 |
Dual Core: Bros dat one is small na, just an evening |
Re: Very Easy Html/php Quiz :) by DualCore1: 8:52am On Sep 23, 2010 |
^^ Na you know where you go o! If you were online it wouldn't have taken the whole evening. I IM'd you and your ID went offline , at that point I knew this was an OYO situation I was getting into. |
(1) (Reply)
*~Yawa-ti-de Voted Webmasters Section Poster Of The Year 2012*~Congratulations / Tips For Successful Networking With Other Bloggers / 5 Helpful Tips To Use Wordpress Themes
(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. 48 |