mrsrowe Posted October 8, 2008 Share Posted October 8, 2008 Hello I am creating a peer review page. I have 10 users in a group and they need to rate the other nine members in their group. for each member of the group there is a row in a table that has their name and then marks from -3 to 3 for their performance. My query calls the student name id and group from a table in the db. this pulls all the relevant users out and creates a table. However, the radio buttons all belong to the same group so I cannot select scores for each user individually, see below: <tr><td>' . $row['StudentID'] . '</td><td>' . $row['SurName'] . '</td><td>' . $row['FirstName'] . '</td><td><input name="rating" type="radio" value="-3" /></td><td><input name="rating" type="radio" value="-2" /></td><td><input name="rating" type="radio" value="-1" /></td><td><input name="rating" type="radio" value="0" /></td><td><input name="rating" type="radio" value="1" /></td><td><input name="rating" type="radio" value="1" /></td><td><input name="rating" type="radio" value="3" /></td></tr>' I want to change the input name from rating, to something unique to each user, for example to use the StudentID hope this makes sense. all answers appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/ Share on other sites More sharing options...
F1Fan Posted October 8, 2008 Share Posted October 8, 2008 Change the input name to: <input name="rating[' . $row['StudentID'] . ']" type="radio" value="-3" /> Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-659986 Share on other sites More sharing options...
Brandon Jaeger Posted October 8, 2008 Share Posted October 8, 2008 So just do: <input name="rating_' . $row['StudentID'] . '" type="radio" value="-2" /> The format will be like so: rating_123 Edit: F1 beat me to it. His method better anyway. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-659988 Share on other sites More sharing options...
JonnoTheDev Posted October 8, 2008 Share Posted October 8, 2008 Just place the student ID value in the radio value param: echo '<tr><td>'.$row['StudentID'].'</td><td>'. $row['SurName'].'</td><td>'.$row['FirstName']. '</td><td> <input name="'.$row['StudentID'].'" type="radio" value="-3" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="-2" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="-1" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="0" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="1" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="1" /> </td><td> <input name="'.$row['StudentID'].'" type="radio" value="3" /></td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-659989 Share on other sites More sharing options...
mrsrowe Posted October 9, 2008 Author Share Posted October 9, 2008 Thanks all of you. I did that and it works, sort of. How do I grab that value once the form is submitted? If I use this bit of code: if(isset($_GET['submitted'])) { //echo $StudentID; //echo $StudentID; $name = $_GET['rating']; //this is causing a fatal error. echo $name; //echo $FirstName; } else I get the response: Notice: Undefined index: rating How do it get it to append the value that is passed with 'rating'? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-660831 Share on other sites More sharing options...
Andy-H Posted October 9, 2008 Share Posted October 9, 2008 $rating = $_POST[{$row['StudentID']}]; Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-660836 Share on other sites More sharing options...
F1Fan Posted October 9, 2008 Share Posted October 9, 2008 If you used name="{$row['StudentID']}", use $_POST[$row['StudentID']]. If you used name="rating[{$row['StudentID']}]", use $POST['rating'][$row['StudentID']]. If you used name="rating_{$row['StudentID']}", use $_POST['rating_'.$row['StudentID']]. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-660914 Share on other sites More sharing options...
F1Fan Posted October 9, 2008 Share Posted October 9, 2008 One more thing. If you want to cycle through all of them, use this: <?php foreach ($_POST['rating'] as $StudentID=>$Rating){ //do whatever here... } ?> This will only work if you use name="rating[{$row['StudentID']}]" as your name. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-660918 Share on other sites More sharing options...
mrsrowe Posted October 10, 2008 Author Share Posted October 10, 2008 If you used name="{$row['StudentID']}", use $_POST[$row['StudentID']]. If you used name="rating[{$row['StudentID']}]", use $POST['rating'][$row['StudentID']]. If you used name="rating_{$row['StudentID']}", use $_POST['rating_'.$row['StudentID']]. none of these are the same as the one your first suggested: <input name="rating[' . $row['StudentID'] . ']" type="radio" value="-3" /> so I'm a bit confused, I'm also getting lots of error messages :[ Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-661831 Share on other sites More sharing options...
mrsrowe Posted October 10, 2008 Author Share Posted October 10, 2008 I've changed the action to get in order to see what is being sent though the browser: viewStudentsSetVs.php?rating[1]=-3&Submit=go&submitted=TRUE so it's picking up the variable and the value. But when the button is submitted it tells me this: Notice: Undefined variable: row in /data/www/sites/.../viewStudentsSetVs.php on line 24 Notice: Undefined index: in /data/www/sites/.../viewStudentsSetVs.php on line 24 why isn't it picking up the information? any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-661867 Share on other sites More sharing options...
F1Fan Posted October 10, 2008 Share Posted October 10, 2008 First, name="rating[{$row['StudentID']}]" is the same as name="rating[' . $row['StudentID'] . ']". Second, you're not listing all of your code, but read your errors. "Undefined variable: row..." You haven't defined $row by line 24. Then you have an undefined index that is blank on the same line. That usually happens when you use an undefined variable as an array key. List ALL of your code. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-661956 Share on other sites More sharing options...
F1Fan Posted October 10, 2008 Share Posted October 10, 2008 Sorry, messed something up... echo "name=\"rating[{$row['StudentID']}]\""; is the same as echo 'name="rating[' . $row['StudentID'] . ']"'; Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-661958 Share on other sites More sharing options...
mrsrowe Posted October 10, 2008 Author Share Posted October 10, 2008 sorry, i wanted to be quick. You are right, $row is defined after the data comes from the query, I thought this would persist on the button click, it appears in the location bar in the browser, so to my brain, it must still be available to use. i'm not a natural with this stuff. here is my code <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $selected = 1; $page_title = 'hello chips'; //page header echo '<h2>students</h2>'; require_once('ilikechips.php'); if(isset($_GET['submitted'])) { echo 'submitted!'; $rating = $_GET['rating'][$row['StudentID']]; } else { echo 'no button pushed here weakling'; } //query $query = "SELECT StudentID, SurName,FirstName FROM tblStudent WHERE GroupID= ' . $Selected . '"; $result = mysql_query($query); if($result) { //table header echo '<form action="viewStudentsSetVs.php" method="get">'; echo '<table border=1> <tr><td>ID</td> <td>Surname</td> <td>First Name</td> <td>-3</td> <td>-2</td> <td>-1</td> <td>0</td> <td>1</td> <td>2</td> <td>3</td> </tr>'; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $SurName = $row['SurName']; $FirstName = $row['FirstName']; $StudentID = $row['StudentID']; echo '<tr>'; echo '<td>' . $StudentID . '</td>'; echo '<td>' . $FirstName . '</td>'; echo '<td>' . $SurName . '</td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="-3" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="-2" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="-1" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="0" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="1" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="1" /></td>'; echo '<td><input name="rating[' . $StudentID . ']" type="radio" value="3" /></td></tr>'; } echo '</table>'; echo '<input name="Submit" type="submit" value="go tiger" />'; //echo '<input type="hidden" name="submitted" value="TRUE" />'; echo '<input type="hidden" name="submitted" value="TRUE" />'; echo '</form>'; mysql_free_result($result); } else { echo ' the current users could not be retrieved.'; } //mysql_close() ?> this is the location bar contents: http://chipsarefantastic.com/viewStudentsSetVs.php?rating[1]=-3&Submit=go+tiger&submitted=TRUE Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-662036 Share on other sites More sharing options...
F1Fan Posted October 10, 2008 Share Posted October 10, 2008 First, PLEASE post your code in the code tags. It is painful to read without. Now, you have found the problem yourself; you are defining $row after you are using it. You can't do that. The only way that a variable can span multiple pages is by using $_SESSION variables. Also, you say it's being passed in the location bar, but I don't see the $row variable in your listed URL. To fix all of it, declare (define) $row before you use it. It is as simple as that. It's like trying to drive a car before you put gas in it. You try to start it, it won't start. Then you put gas in it and ask yourself why it wouldn't start before. Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-662053 Share on other sites More sharing options...
mrsrowe Posted October 13, 2008 Author Share Posted October 13, 2008 hello thanks. I don't know what you mean by a code tag, which is why I didn't use one. sorry. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-663738 Share on other sites More sharing options...
F1Fan Posted October 13, 2008 Share Posted October 13, 2008 Click the # button and it will add [ code ][ /code ] around your code (without the extra spaces). Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-664046 Share on other sites More sharing options...
mrsrowe Posted October 14, 2008 Author Share Posted October 14, 2008 I didn't need to define $row because this works. and this is what I was looking for. $rating = $_GET["rating"][0]; AND i used the code tag. see learning all the time. Quote Link to comment https://forums.phpfreaks.com/topic/127561-solved-radio-button-array/#findComment-664897 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.