Jump to content

[SOLVED] radio button array


mrsrowe

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']].

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :[

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.