Jump to content

Check radio buttons depending on value


digi duck

Recommended Posts

Hi. Im pretty sure this is easy but ive been trying for hours and looked all over the web but cant seem to do it.

 

I have information submitted to my database which I then retrieve on another page. I retrieve the landlord name and group the same values together plus retrieve an overall rating which I average and sort by. This works and is done using:

 

$sql="SELECT landlord_name, AVG(overall) FROM $tbl_name GROUP BY landlord_name ORDER BY AVG(overall) DESC";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){

 

Where Im finding it difficult though is I have 5 radio buttons with values from 1-5 and want one of them to be checked if the value matches the averaged overall rating. What I've tried is:

 

if (AVG(overall)==1){
$check1="checked";}
if (AVG(overall)==2){
$check2="checked";}
if (AVG(overall)==3){
$check3="checked";}
if (AVG(overall)==4){
$check4="checked";}
if (AVG(overall)==5){
$check5="checked";}

 

then in the form:

 

<table width="600" border="1" cellspacing="0" cellpadding="3">
<tr>
<td><? echo $rows['landlord_name']; ?></td>
<td><div class="avg"><form>
<input type="radio" value="1" disabled="disabled" checked= "<? $check1 ?>" />
<input type="radio" value="2" disabled="disabled" checked= "<? $check2 ?>" />
<input type="radio" value="3" disabled="disabled" checked= "<? $check3 ?>" />
<input type="radio" value="4" disabled="disabled" checked= "<? $check4 ?>" />
<input type="radio" value="5" disabled="disabled" checked= "<? $check5 ?>"/>
</form></div>
</td>
</tr>
</table>

 

To make it a bit more difficult, the AVG(overall) value will not always be an integer from 1-5 and will need to be rounded to the closest one. Think i can use  ROUND for this but dont know how to put it in.

 

Thanks for your help in advance. As you can probably tell im new to php and still learning the basics.

Link to comment
Share on other sites

Thanks for the reply however it is giving me an error of:

 

Fatal error: Call to undefined function AVG() in /srv/disk2/959619/www/studentlandlordreviews.co.uk/landlord-reviews.php on line 58

 

which refers to the:

 

if (AVG(overall)==1){

 

I guess this means it doesnt know what AVG(overall) is so i tried this instead but still no luck:

 

	$avg =AVG(overall);

if ($avg==1){
$check1='checked="checked"';}

 

Thanks for your help.

Link to comment
Share on other sites

Thank you it has stopped throwing that error at me however now it seems as though it is always saying that 5 is checked (or possibly all of them checked) regardless of the value. I looked in my database and the overall value is 1 yet it checks number 5. Any ideas? Thanks for your help so far.

Link to comment
Share on other sites

Managed to work it out. I had checked= $variable in the form so instead replaced the whole thing with echo $variable. Thanks a lot!!

 

Got the problem now in that it checks the buttons regardless of landlord name i.e:

 

NAME    Value

Bob        2

John        4

 

It would check both the 2nd and 4th radio buttons. Anyone know how to make it only check the value that corresponds to the name??

 

Thanks

Link to comment
Share on other sites

It's a little difficult to tell without seeing all of the code, but my suspicion is that you're not resetting your $checked variables as the beginning of each loop iteration. This means that the second time around the loop, one of the $checked variables is already set. For example, try adding

 

$checked1= "";
//repeat

 

To the beginning of the loop.

 

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.