Radio Buttons and mysql

Discussion in 'PHP' started by white10, Jul 5, 2007.

  1. #1
    Hi Guys

    I am loading a test from a database, which contain all the questions and possible answers, Now what i want to do is display the possible answers as radio buttons and check if the user selected the correct radio button.(i will use the variable $cat_letter to check the value of the radio button selected.)

    I want to checked if each radio button selected is equal to $cat_letter and then add 1 to it if not add 0 and conitue with the while loop

    Can any of you guys be of help to me...

    
    <?php
      session_start();
        include "db_connect.php";
     
    $get_cats = "select * from
        tests1";
    	 
      $get_cats_res = mysql_query($get_cats) or die(mysql_error());
      
     if (mysql_num_rows($get_cats_res) < 1) {
       $display_block = "<P><em>Sorry, no categories to browse.</em></p>";
    } else {
      
      $display_block .= "  <table  width='574' border='1' cellpadding='0' cellspacing='0' >
       <tr>
    				 <td>
       <h2>$cat_title </h2>
        </td>
    				 </tr>";
      $hold[];
      
    while ($cats = mysql_fetch_array($get_cats_res)) {
           $cat_id = $cats[id];
    	    $cat_description = stripslashes($cats[description]);
            $cat_letter = stripslashes($cats[letter]);
    		$cat_answer_a = stripslashes($cats[answer_a]);
    		$cat_answer_b = stripslashes($cats[answer_b]);
    		$cat_answer_c = stripslashes($cats[answer_c]);
    		
            $display_block .= "<tr>	 <td width ='30' class='style17'>$cat_id	 <td/>	 <td align='center' width ='294' bgcolor='white' class='style19'>$cat_description<td/> <td width ='80'>
    					 $cat_answer_a
    					  <INPUT TYPE='radio' NAME='one' VALUE='a' 'checked' >
    					
    					 $cat_answer_b
    					  <INPUT TYPE= 'radio' NAME=\"one\" VALUE=\"b\" >
    					 
    					
    					 $cat_answer_c 
    					  <INPUT TYPE= 'radio' NAME=\"one\" VALUE=\"c\" >
    					 
    					 <td>
    					 
    					 </tr>";
    					 	 
    					 
    					 
    	   }
    
    
    
     $display_block .= "</table>";
    }
    
    ?>
    
    Code (markup):

     
    white10, Jul 5, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, you seem to be missing the <FORM> tags altogether when you build the page?

    I'm not quite sure what you mean to happen, but, looks to me like you'll list a load of questions with 3 possible answers each. You need to name them differently each time round the loop (rather than calling them "one" each time). So you'd be better off with...

    <INPUT TYPE='radio' NAME='".$cat_id." VALUE= etc.
     
    ecentricNick, Jul 5, 2007 IP
  3. white10

    white10 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi nick,
    Thanks for the advice, but I left the form out because I dont want to display the submit button each time a row is called I only want to show it once for the entire table.
     
    white10, Jul 6, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    so put the form tag and the submit button outside of the while loop? that way it prints form, then loops through the rows, then prints the button...
     
    ansi, Jul 6, 2007 IP
  5. white10

    white10 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried that , (putting the form outside the loop )but now the form doesn't want to submit
     
    white10, Jul 6, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    i don't see why it wouldn't. are you setting an action/method in it?
     
    ansi, Jul 6, 2007 IP
  7. white10

    white10 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    here is the code,

    <?php
    session_start();
    include "db_connect.php";

    $get_cats = "select * from
    tests1";

    $get_cats_res = mysql_query($get_cats) or die(mysql_error());

    if (mysql_num_rows($get_cats_res) < 1) {
    $display_block = "<P><em>Sorry, no results.</em></p>";
    } else {

    $display_block .= "<form method = 'post' action ='test_take2.php'> <table width='574' border='1' cellpadding='0' cellspacing='0' >
    <tr>
    <td>
    <h2>$cat_title </h2>
    </td>
    </tr>";
    $i = 0;

    while ($cats = mysql_fetch_array($get_cats_res)) {
    $i++;
    $cat_id = $cats[id];
    $cat_description = stripslashes($cats[description]);
    $cat_letter = stripslashes($cats[letter]);
    $cat_answer_a = stripslashes($cats[answer_a]);
    $cat_answer_b = stripslashes($cats[answer_b]);
    $cat_answer_c = stripslashes($cats[answer_c]);

    $display_block .= " <tr> <td width ='30' class='style17'>$cat_id <td/> <td align='center' width ='294' bgcolor='white' class='style19'>$cat_description<td/> <td width ='80'>

    $cat_answer_a
    <INPUT TYPE='text' NAME='one[$i]' VALUE='a' 'checked' >

    $cat_answer_b



    $cat_answer_c


    </td>

    </tr> ";


    $display_block .= "

    ";


    }



    $display_block .= "<P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p></table> </form>";
    }

    ?>
     
    white10, Jul 6, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    try something like this.

    
    <?php
    session_start();
    include "db_connect.php";
    
    $get_cats = "select * from tests1";
    $get_cats_res = mysql_query($get_cats) or die(mysql_error());
    
    if (mysql_num_rows($get_cats_res) < 1)
    {
    	$display_block = "<P><em>Sorry, no results.</em></p>";
    }
    else
    {
    	$display_block .= "<form method = 'post' action ='test_take2.php'> <table width='574' border='1' cellpadding='0' cellspacing='0' >".
    	."<tr><td><h2>".$cat_title."</h2></td></tr>";
    
    	$display_block .= '<form action="foo.php" method="post">'; // added this
    
    	$i = 0;
    	while ($cats = mysql_fetch_array($get_cats_res))
    	{
    		$i++;
    		$cat_id = $cats[id];
    		$cat_description = stripslashes($cats[description]);
    		$cat_letter = stripslashes($cats[letter]);
    		$cat_answer_a = stripslashes($cats[answer_a]);
    		$cat_answer_b = stripslashes($cats[answer_b]);
    		$cat_answer_c = stripslashes($cats[answer_c]);
    
    		$display_block .= " <tr> <td width ='30' class='style17'>".$cat_id." <td/> <td align='center' width ='294' bgcolor='white' class='style19'>".$cat_description."<td/>".
    			"<td width ='80'>".$cat_answer_a."<INPUT TYPE='text' NAME='one[".$i."]' VALUE='a' 'checked' >".$cat_answer_b." ".$cat_answer_c."</td></tr> ";
    		$display_block .= "";
    	}
    	$display_block .= "<P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p></table> </form>";
    }
    
    ?>
    
    PHP:
    also, check your table. you're printing the rows in tr's and the error message and submit button in p's. shouldn't they be in a tr/td as well?
     
    ansi, Jul 6, 2007 IP
  9. white10

    white10 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It only seems to work if its inside the loop, thanks anyways for the help I appreciate it..
     
    white10, Jul 6, 2007 IP