Jump to content

Php/ Mysql Radio Button Value Retreival


jcc5018

Recommended Posts

Ok, I am doing a website for a class in which i want users to have the opportunity to be rated based on a list of traits that are included into a database. But I am having a hard t ime with the code that would allow me to have a form in HTML that is a set of radio buttons valued -5 --> 5. With -5 being a negative trait such as "SAD" and 5 being a positive trait; "Happy". I have a list of about 10 traits currently (will be updated to 120 divided into 3 catagories once I figure it out) So my idea is that the PHP has to retreive the value from the database using sql one row at a time, then it will display the positive and negative trait into the appropriate lable on the HTML Form, then that data has to be saved while it loops to the next row of data and displays another Pos/ neg value into a copy of the same form. It will do this 10 times for now. But the value of the radio button has to be recorded for each set of traits (Happy/ Sad = 1 set) and then that value will be entered into a seperate database table. My problem is that I dont know how to make it loop and display/ record a seperate set of traits for each form.Here's the link to what i managed so farhttp://zen.smeal.psu.edu/~jcc5018/project/survey.php

<?phpHeres the code:// the idea is to loop this by calling one row at a time and storing them as a variable. Im not sure how to put the chart in the loop and then return the seperate trait scores back to the rest of the PHP page. for ($i=1; $i<=10; $i++){$dbc=mysql_connect("______", "____","____");mysql_select_db("_____") or did("Can't find database ______"); $query[$i]="Select negative, positive from personalitytraits where id='$i'";$result[$i]= mysql_query($query[$i]);while($row = mysql_fetch_array($result[$i]))  { // this calls one row at a time and stores the result as the negative and positive variables  $negative[$i]= $row['negative'];  $positive[$i]= $row['positive'];    } echo '<form id="form1" name="form1" method="post" action="survey.php">';  echo '<table width="800" border="1">';        echo 	'<tr>      <td colspan="2">$negative[$i]</td>      <td width="54"> </td>      <td width="54"> </td>      <td width="85"> </td>      <td width="128"><div align="center">Dont know </div></td>      <td width="78"> </td>      <td width="54"> </td>      <td width="54"> </td>      <td colspan="2">$positive[$i] </td>    </tr>    <tr>      <td width="54" height="57"><label>        <div align="center">          <input type="radio" name="rank" value="-5" id="rank_11" />          <br />        -5</div>      </label></td>      <td width="54"><label>        <div align="center">          <input type="radio" name="rank" value="-4" id="rank_12" />          <br />        -4</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="-3" id="rank_13" />          <br />        -3</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="-2" id="rank_14" />          <br />        -2</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="-1" id="rank_15" />          <br />        -1</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="0" id="rank_16" />          <br />        0</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="1" id="rank_17" />          <br />        1</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="2" id="rank_18" />          <br />        2</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank" value="3" id="rank_19" />          <br />        3</div>      </label></td>      <td width="54"><label>        <div align="center">          <input type="radio" name="rank" value="4" id="rank_20" />          <br />        4</div>      </label></td>      <td width="61"><label>        <div align="center">          <input type="radio" name="rank" value="5" id="rank_21" />          <br />        5</div>      </label></td>    </tr>  </table>  <p> </p>';    echo "</form>";  $score= $_post('rank');       } ?>

The following is the list of tables, sql statements I think I'd need to implement, and the general process. Keep in mind, right now i am only focusing on one set of traits (personality) and I am probably not including the user preferences at this point in the design, I just need to get the survey working and recording the data. Please help me if you can. This is due by tuesday or thursday. \\User enters data into create new account form, Data is retrived with PHP, USER ID and rater id values are automatically assigned a random number 8 characters long. \\** may need to run a sequence that will prevent duplicate ID’s **INSERT INTO `jcc5018`.`info’ (‘visitorid’,‘fname’,’lname’,’email’,’username’,’password’,’userid’,’raterid)VALUES (NULL, ‘fname’,’lname’,’email’,’username’,’password’,’userid’,’raterid’);\\ If user is creating a survey they also will be entering the following into userinfo table\\INSERT INTO `jcc5018`.`userinfo’ (‘userid’,’street’, ’street2’, ‘city’, ‘state’, ‘zip’, ‘phone’, ‘description’, ‘comments’, ‘joindate’)VALUES (‘userid’,’street’, ’street2’, ‘city’, ‘state’, ‘zip’, ‘phone’, ‘description’, ‘comments’, ‘joindate’);\\ If user is only rating a person the following will take place…1. They will create account that inserts into the info table2. Then they will enter additional information based on the person they are ratingSelect surveyid FROM surveyed WHERE userid =’userid’ AND raterid=’raterid’;Insert into ‘jcc5018’.’raterinfo’ (‘surveyed’,’######’,’timeknown’, ‘relationship’, ‘anonymous’) Values (‘surveyed’,’######’,’timeknown’, ‘relationship’, ‘anonymous’)INSERT INTO `jcc5018`.`personalityscore’ (‘surveyid’, ’personalid’, ’perscore’) VALUES (‘surveyid’,’personalid’,’perscore’);Select AVG(perscore) from personalityscore where (Select surveyid from surveyid where userid=’userid’ );Things to make website workConnect to database Database will consist of multiple tablesInfo table: This table is used for both raters and users as both will be required to have a password, email, and username. From here, a rater ID and User ID is assigned that each of the tables below expands upon. This should allow users to also be raters without filling in additional information. Basic ID, fname, lname, email, username, password, user ID, rater IDUser Informational table:User Id, street, street 2, city, state, zip, phone, picture, description, comments, joindateUser preferences: (Yes or No, or radio button options) (NOT INCLUDED YET)User id, personality, Image, lifestyle, det_pers, det_image, det_life, results, searchable, plan, payment, Invite codeNotifications:(NOT INCLUDED YET)User ID, new results, new featuresBlock list: (NOT INCLUDED YET)User Id, rater email SurveyIDSurvey ID, User ID, Rater IDPersonality Score:survey Id, personal Id, score_per, Image Score:(NOT INCLUDED YET)survey Id, image id, score_img, Lifestyle Score:(NOT INCLUDED YET)survey Id, lifestyle_id, score_lifeRater Information:Survey Id, ######, time known, relationship, anonymous PersonalityPersonal_Id, positive, negativeImage(NOT INCLUDED YET)Image_Id, positive, negativeLifestyle(NOT INCLUDED YET)Lifestyle_Id, positive, negativeResults (used for the payment plan as a user will have the option to purchase previous month results, each 30 days of results must be paid for to have full options based on plan purchased)(NOT INCLUDED YET)User Id, plan, total results, time Program logic:1. User creates account—Information entered into “Informational” table2. User chooses preferences—Information entered into “User preference” table3. User provided automatic random invite code—provided as a direct link to users survey4. User sends the link to friends or makes it available for anyone5. Raters click link and fill out basic information—store data in “rater” table6. Survey will retrieve traits based on the following code:a. MYSQL will check to see what options user has checked. b. PHP will create a For loop that will retrieve the positive and negative value based on the ID value in each Trait category and display it in a graph as follows.i. $i=Personal_ID, Image_id, or lifestyle_idFor (for ($i=0; $i<=50; $i++){Each trait category should have its own page automatically added depending on if the options have been included in the survey or not. - Each radio button is coded with the following: (values different based on button) - <input type="radio" name="rank" value="5" id="rank_21" />7. The PHP code needs to retrieve the value for each Trait [$i] and record it into the “Score” tablea. It should do this by using the value clicked, assigning it to the Id of the set of traits, then inserting that value into the database with an insert command8. Results will be viewed by displaying the trait names based on another for loop. a. Scores for each section must be averaged together using AVG query based on category Id, and user IDb. If user has payment option 1, query must first separate values based on ######, time known, and relationship then average each set of results separatelyc. If user has one month paid for and one month not paid for, results for the unpaid months must be displayed as “unknown” otherwise they will be color coded based on the result choice user prefers. d. If user has payment option 2, individual user results will be displayed for each trait.

Link to comment
Share on other sites

oh on my traits table , The ID is autoincrimented, but of course if you delete a set the ID numbers do not auto correct, so i end up with ID's like 1,3,4,5,8,10... I feel like this could pose a problem with the for loops in PHP? so is this a problem and does anyone know how to have the numbers reordered automatically?

Link to comment
Share on other sites

Hi, i know this is due today but anyway, you can print using a foreach instead of a for, like this:

foreach($traits as $tra){    echo '<input ... value="$tra['trait_name']" />';}

this way you don't need to know the id of the field for printing but this doesn't work if you want to include more info in the array as you wont be working on the same variable, in the example you'll be working on $tra instead of $traits.pd: i checked your page and it seems you figured out.

Link to comment
Share on other sites

ok im slowly figuring this out.I have this page:

$query[$i]="Select negative, positive from personalitytraits where id='$i'";$result[$i]= mysql_query($query[$i]);while($row = mysql_fetch_array($result[$i]))  { // this calls one row at a time and stores the result as the negative and positive variables  $negative[$i]= $row['negative'];  $positive[$i]= $row['positive'];    } echo '<form id="form1" name="form1" method="post" action="score.php">';  echo '<table width="800" border="1">';        echo 	'<tr>      <td colspan="2">'.$negative[$i].'</td>      <td width="54"> </td>      <td width="54"> </td>      <td width="85"> </td>      <td width="128"><div align="center">Dont know </div></td>      <td width="78"> </td>      <td width="54"> </td>      <td width="54"> </td>      <td colspan="2">'.$positive[$i].' </td>    </tr>    <tr>';      echo '<td width="54" height="57"><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="-5" id="rank_11" />          <br />        -5</div>      </label></td>      <td width="54"><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="-4" id="rank_12" />          <br />        -4</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="-3" id="rank_13" />          <br />        -3</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="-2" id="rank_14" />          <br />        -2</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="-1" id="rank_15" />          <br />        -1</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="0" id="rank_16" />          <br />        0</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="1" id="rank_17" />          <br />        1</div>      </label></td>      <td><label>        <div align="center">          <input type="radio"  name="rank'.$i.'" value="2" id="rank_18" />          <br />        2</div>      </label></td>      <td><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="3" id="rank_19" />          <br />        3</div>      </label></td>      <td width="54"><label>        <div align="center">          <input type="radio" name="rank'.$i.'" value="4" id="rank_20" />          <br />        4</div>      </label></td>      <td width="61"><label>        <div align="center">          <input type="radio"  name="rank'.$i.'" value="5" id="rank_21" />          <br />        5</div>      </label></td>    </tr>  </table>  <p></p>';}		echo '<input type="submit" value="Done Rating">';		$surveyid=$_POST['surveyid'];  echo '<input name="surveyid" type="hidden" value="$surveyid" />';  echo "</form>";          ?>

That goes into this:

 $surveyid=$_POST['surveyid'];$query="Select fname from info where userid='(select userid from surveyid where surveyid='$surveyid')' ";$friend= mysql_query($query);for ($i=1; $i<=10; $i++){$score[$i]= $_POST["rank".$i];$query="insert into personalityscore values ('$surveyid','$i','$score[$i]')";$result = mysql_query($query);}if ($result) {echo "Thank you for finishing the survey. Your results will be added to the database for $friend to view when they next log on.<br>";} else{echo "There was an error entering your data.";}echo '<br><a href="Web Site/index.html" target="_self">Return to Home Page</a>';

well it is displaying the error message and not entering the values of the survey correctly. Im not sure if I am pulling the $score variable correctly. This is actually due tomorrow. I think this is the last thing i really need to do in order for this to work.

Link to comment
Share on other sites

well, im not sure but the values for the scores might be registering but for some reason neither the score page or the create user page is allowing any data to be entered into the database. it is able to pull data from the database but it wont insert into it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...