Web Analytics Made Easy -
StatCounter radio boxes populated from mysql query - CodingForum

Announcement

Collapse
No announcement yet.

radio boxes populated from mysql query

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • radio boxes populated from mysql query

    hi guys,

    I am in the process of making an online survey system. The system has a library with a list of pre developed surveys from a mysql database. Once the user has selected the survey they want from the drop down box, the questions and multiple choice options for that survey appear on-screen.

    I have coded the drop down box to get data from mysql and the correct questions and options appear. However, i would like 5 radio boxes to appear after each question, and for the option names to appear with these.

    So far, i have only managed to get the options to appear once....at the bottom of the page. I am assuming some sort of loop of if statement may be needed.

    I have attached the code. Really hope somebody can help with this...


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <!--
    
    -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    
    <title>Edit Survey</title>
    
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
    
    </head>
    
    <body>
    
    <!-- start header -->
    
    <div id="logo"> <h1><p></p> Edit Survey</p></h1>
    	
    	<p>The first answer to a successful website!</p>
    	
    </div>
    
    <div id="menu">
    
    	<ul>
    		<li class="current_page_item"><a href="#"> </a></li>
    		<li><a href="#"> </a></li>
    		<li><a href="#"> </a></li>
    		<li><a href="About_Us.php"> About Us </a></li>
    		<li><a href="Contact_Us.php"> Contact Us</a></li>
    
    	</ul>
    
    </div>
    
    <!-- end header -->
    
    <!-- Start Login  -->
    <div id="latest-post" class="wide-post">
    
    <h4 class="title"> <font color="maroon"> <p>  
    
    
    <?php 
    
    require_once ('Connect.php');
    
    
    $survey = $_POST ['survey'];
    
    $res = mysql_query("SELECT * FROM survey WHERE Survey_ID = '$survey'")or die(mysql_error());
    $numRows = mysql_num_rows($res);
     
    while($row = mysql_fetch_array($res))
    
    {
    	
    echo $row['Survey_Name']; 
    
    }
    
    ?>  
    
    </p> </font> </h4>
    <div class="entry">
    	
    	<br />
    	
    <font size="2"> <p align="left"> <strong> <i> We are continuously improving our website to make it as useful to you as possible. Please take a few moments </p> <br /> <p> to answer these questions about our 
    site.   
    	
    </strong> 
    	
    </i>
    		 
    </p> 
    
    </font> 
    
    	<br /> 
    	
    	<br />
    	
    	<br />
    
    <p>&nbsp;
    
    <?php
     
    $survey = $_POST['survey'];
    $table1="survey_and_question";
    $table2="questions";
     
    $res = mysql_query("SELECT * FROM $table1, $table2 WHERE $table1.Question_ID = $table2.Question_ID AND $table1.Survey_ID = '$survey'")or die(mysql_error());
    $numRows = mysql_num_rows($res);
     
    while($row = mysql_fetch_array($res))
    
    {
    	
    echo $row['Question_Text'];
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    echo "<br/>";
    
    }
    
    $table3="question_and_option";
    $table4="options";
    
    $res = mysql_query("SELECT * FROM $table3, $table4 WHERE $table3.Option_ID = $table4.Option_ID")or die(mysql_error());
    $numRows = mysql_num_rows($res);
    while($row = mysql_fetch_array($res))
    
    {
    
    echo "<strong>";
    
    echo $row['Option_Text']; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp";
    echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp"; echo "&nbsp";
    
    echo "</strong>";  
    
    }
    
    ?>
    
    <form name=myform>
    <input type="radio" name=myradio value= <?php echo $_POST ['Option_Text'];?> 
    <input type="radio" name=myradio value= <?php echo $_POST ['Option_Text'];?>
    
    
    </form> 
    
    
    
    
    </p>		
    		
    	<br />               
    	<br />                                                                                                    
    	                                                                                                           
    <form action="display_library.php" method="POST">                                                          
                                                                                                                
    
    
    	<br />
    
    
    <p> <center> 
             	                                                                             
    <p align="center"><input name="submit" type="submit" id="submit" value="Distribute Survey" />
    <input type="hidden" name="submitted" value="TRUE" />
    
    	  
    </p>
    	  
    </center> 
    	  
    </form>
    	
    </div>
    	
    <div class="bottom"></div>
    
    </div>
    
    <div id="footer">
    
    <p>&copy;2009 All Rights Reserved. &nbsp;&bull;&nbsp; FirstCheck Enterprises &nbsp;&bull;&nbsp; Designed by Sejal Modi </p>
    
    </div>		 
    
    
    </body>
    </html>

  • #2
    I had a quick look and it seem to me that you will have to re-design the database structure. The second MySQL query would have to go within the first MySQL query's while loop, a MySQL query should never be inside a loop. Can the questions and options appear along the same MySQL row and query the database once?

    survey_id | survey_question | option_1 | option_2 | option_3 | option_4 | option_5




    -----------
    Leonard Whistler

    Comment


    • #3
      hi,

      Thank you for the reply i have given the database structure below. There is a survey, question and options table. Additional tables also had to put in place to eliminate many-to many relationships. As all information is different tables, i doubt i would be able to call the database once.

      Is there a way around this?

      surveys:
      survey_ID
      creator_ID
      Survey_Name

      Survey_and_question:
      survey_ID
      Question_ID

      Questions:
      Question_ID
      Question_Text

      Question_and_option
      Question_ID
      Option_ID

      options:
      option_ID
      option_text

      Comment


      • #4
        Here is a layout that might work. I figure since that the 5 options go with each question that they can be in the same database row. Once the survey is selected only one database query is needed and one thing that must be avoided is a MySQL query within a loop.


        PHP Code:
        <?php
          
        // Survey number three selected
          
        $mySQL = array('What color is the sky?','Blue','Green','Black','Purple','White',
                         
        'What time is it?','Noon','1am','3pm','Midnight','4pm EST',
                         
        'What type of boat is that?','Sailboat','Rowboat','Yacht','Motorboat','Mega-Yacht',
                         
        'Which website do you like?','Google','Yahoo','MSN','Facebook','Ebay');

          foreach (
        $mySQL as $value) {

          
        $counter $counter 6;

          if (
        $counter == 0) {
          echo 
        "\tQ: $value\n";
          } else {
          echo 
        "\t\tOption $counter$value\n";

          }
          
        $counter++;
          }
        ?>
        The output is:

        Q: What color is the sky?
        Option 1: Blue
        Option 2: Green
        Option 3: Black
        Option 4: Purple
        Option 5: White
        Q: What time is it?
        Option 1: Noon
        Option 2: 1am
        Option 3: 3pm
        Option 4: Midnight
        Option 5: 4pm EST
        Q: What type of boat is that?
        Option 1: Sailboat
        Option 2: Rowboat
        Option 3: Yacht
        Option 4: Motorboat
        Option 5: Mega-Yacht
        Q: Which website do you like?
        Option 1: Google
        Option 2: Yahoo
        Option 3: MSN
        Option 4: Facebook
        Option 5: Ebay
        Leonard Whistler

        Comment


        • #5
          The mysql queries have been coded to pick up the data from the database. Once selected the questions for that survey and the options related dynamically appear on-screen.

          There are 4 surveys, each with different questions and options. If was to use the above code- would i need to do this for each survey within the database.

          Comment


          • #6
            There are 4 surveys, each with different questions and options. If was to use the above code- would i need to do this for each survey within the database.
            No ...... The code is different for an actual MySQL query and I have updated it below.

            Once selected the questions for that survey and the options related dynamically appear on-screen.
            With PHP the page has to reload once the survey is selected. When I think of "dynamically" I think of Javascript and no reloading of the page when content is changed, this would be more complicated. Using an all PHP method the first page would require one MySQL query to show the surveys and using the post GET method the survey_topic is passsed to the second page. Both pages require one MySQL query. Below is a VERY rough layout.



            Main Page
            id | survey_topic | survey_creator

            0001 | Space | Bill Brown
            0002 | Cars | Ben Smith
            0003 | Boats | Jack Black
            0004 | Websites | Bob


            ----------------------------------------

            Survey Page
            id | survey_questions | survey_topic | option_1 | option_2 | option_3 | option_4 | option_5

            0001 | How far is the moon? | Space | 20 miles | 100 miles | 250,000 miles | 500,000 Km | 750,000 miles
            0002 | How far is the sun? | Space | 187 miles | 789 miles | 1,250,000 miles | 4,500,000 Km | 45,750,000 miles
            0003 | Which website do you like? | Websites | Yahoo | MSN | Google | Facebook | Ebay
            0004 | How far is Mars? | Space | 2055 miles | 16500 miles | 25,000 miles | 65,500,000 Km | 45,750,000 miles
            0005 | What boat is this? | Boats | Sailboat | Rowboat | Big Boat | Small Boat | Red Boat
            0006 | What sort of engine is this? | Boats | Inboard | Outboard | 100 HP | Yamaha | 25 HP
            0007 | What sort of engine is this? | Cars | 350 | 454 | 455 | 327 | 400
            0008
            0009
            0010

            PHP Code:
            $survey_topic $_GET['id'];

            $sql mysql_query("SELECT * FROM surveys WHERE survey_topic = '$survey_topic'");

            while(
            $row mysql_fetch_array($sql)){

            echo 
            "$row[survey_questions]<br>";
            echo 
            "$row[option_1]<br>";
            echo 
            "$row[option_2]<br>";
            echo 
            "$row[option_3]<br>";
            echo 
            "$row[option_4]<br>";
            echo 
            "$row[option_5]<br><br>";

            The while loop coding for the MySQL query would be different than in my example on Post 4 and will need lots of tweaking.
            Last edited by Len Whistler; Apr 8, 2009, 03:01 PM.
            Leonard Whistler

            Comment

            Working...
            X