digi duck Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/ Share on other sites More sharing options...
scootstah Posted February 7, 2012 Share Posted February 7, 2012 Instead of $check1="checked"; Do $check1='checked="checked"'; In some browsers, simply having the "checked" attribute (even with no value) will still check it. As for the rounding, either use floor() or ceil(). Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315495 Share on other sites More sharing options...
digi duck Posted February 7, 2012 Author Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315502 Share on other sites More sharing options...
scootstah Posted February 7, 2012 Share Posted February 7, 2012 It should be $rows['AVG(overall)'] You can make this a little prettier by changing AVG(overall) in your query to AVG(overall) AS overall And then you can just do $rows['overall'] Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315504 Share on other sites More sharing options...
digi duck Posted February 7, 2012 Author Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315516 Share on other sites More sharing options...
digi duck Posted February 7, 2012 Author Share Posted February 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315531 Share on other sites More sharing options...
GingerRobot Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315535 Share on other sites More sharing options...
digi duck Posted February 7, 2012 Author Share Posted February 7, 2012 That was it. Works like a treat. Thank you so much for all your help!! Quote Link to comment https://forums.phpfreaks.com/topic/256613-check-radio-buttons-depending-on-value/#findComment-1315536 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.