whynot Posted December 30, 2011 Share Posted December 30, 2011 Hi, My output in html is : <uL><li id="B1"></li> <li id="B2"></li> <li id="B3"></li> <li id="B4"></li> <li id="B5"></li> <li id="B6"></li> <li id="B7"></li> <li id="B8"></li> <li id="B9"></li> <li id="B10"></li> <li id="B11"></li> <li id="B12" class="active"></li> <li id="B13" class="no"></li> <li id="B14" class="no"></li> <li id="B15" class="no"></li> <li id="B16" class="no"></li> <li id="B17" class="no"></li> <li id="B18" class="no"></li> <li id="B19" class="no"></li> <li id="B20" class="no"></li> </ul> If MySQL query result is equal to `6`, then `<li>` tag with `id` equal to "`B12`" should have class "`active`". All the `<li>` elements occuring after this active element should have class "`no`". This shows images of horizontal rating between `0` and `10` and between 0.5 Example : 0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10 In the example above elements from `0` to `5` will be blue, element `6` will be white and elements from `7` to `10` are black. How could I generate this using PHP and/or MySQL? Thanks, XXXX chirstmas XXXX Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/ Share on other sites More sharing options...
erdem Posted December 30, 2011 Share Posted December 30, 2011 <?php $sql = mysql_query("SELECT * FROM table"); while($ROW = mysql_fetch_array($sql)){ if($ROW['li_number'] == 12) $class = 'class="active"'; elseif($ROW['li_number'] >= 13) $class = 'class="no"'; else $class = ''; echo '<li id="b'.$ROW['li_number'].'" '.$class.'></li>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302519 Share on other sites More sharing options...
whynot Posted December 30, 2011 Author Share Posted December 30, 2011 <?php $sql = mysql_query("SELECT * FROM table"); while($ROW = mysql_fetch_array($sql)){ if($ROW['li_number'] == 12) $class = 'class="active"'; elseif($ROW['li_number'] >= 13) $class = 'class="no"'; else $class = ''; echo '<li id="b'.$ROW['li_number'].'" '.$class.'></li>'; } ?> This Not worked ! your code worked for static li_number ( only 13 ) ! $ROW=['li_number'] Any value can be . example : 2 - 2.5 - 8.5 - 10 - 7 Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302521 Share on other sites More sharing options...
erdem Posted December 30, 2011 Share Posted December 30, 2011 this code was an idea. if you have a value and you want to control it you can do like this. just google and find how you can control those strings Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302522 Share on other sites More sharing options...
erdem Posted December 30, 2011 Share Posted December 30, 2011 http://php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302524 Share on other sites More sharing options...
AyKay47 Posted December 30, 2011 Share Posted December 30, 2011 <?php $sql = mysql_query("SELECT * FROM table"); while($ROW = mysql_fetch_array($sql)){ if($ROW['li_number'] == 12) $class = 'class="active"'; elseif($ROW['li_number'] >= 13) $class = 'class="no"'; else $class = ''; echo '<li id="b'.$ROW['li_number'].'" '.$class.'></li>'; } ?> This Not worked ! your code worked for static li_number ( only 13 ) ! $ROW=['li_number'] Any value can be . example : 2 - 2.5 - 8.5 - 10 - 7 the code provided loooks to be a basic example to get you started in the right direction, if you are expectin float values as well, you can use ceil to round the values up and then compare them. However I did spot an error, if you want to call the columns by name, use mysql_fetch_assoc in stead of mysql_fetch_array. $sql = mysql_query("SELECT * FROM table"); while($ROW = mysql_fetch_assoc($sql)){ $ROW['li_number'] = ceil($ROW['li_number']); if($ROW['li_number'] == 12) $class = 'class="active"'; elseif($ROW['li_number'] >= 13) $class = 'class="no"'; else $class = ''; echo '<li id="b'.$ROW['li_number'].'" '.$class.'></li>'; } again, this is just an example to point you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302527 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 The data value (6 in the example) determines where to change the output as you are looping to produce that output - <?php $data = 6; // assigned from your query/fetch logic (0-10 in .5 steps) $value = 2*$data; //(0-20 in whole steps) echo "<ul>"; for($i=1;$i <=20;$i++){ echo "<li id='B$i'"; if($i == $value){echo " class='active'";} if($i > $value){echo " class='no'";} echo "></li>\n"; } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302530 Share on other sites More sharing options...
whynot Posted December 30, 2011 Author Share Posted December 30, 2011 The data value (6 in the example) determines where to change the output as you are looping to produce that output - <?php $data = 6; // assigned from your query/fetch logic (0-10 in .5 steps) $value = 2*$data; //(0-20 in whole steps) echo "<ul>"; for($i=1;$i <=20;$i++){ echo "<li id='B$i'"; if($i == $value){echo " class='active'";} if($i > $value){echo " class='no'";} echo "></li>\n"; } echo "</ul>"; Thanks, This Nicely worked. I had previously worked with this method. My Problems when $data = 0 ; using 0 - 20 not show li 0 active. i change to this : $i=1;$i <=21 . Actually B1 = 0 B2 = 1 .............. . i need to show active class for 0 if $data = 0. How to Fix This Big Problem ?! Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302552 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 You would need elements for B0 - B20. B0 is active when the data is 0, B1 is active when the data is .5, ... B20 is active when the data is 10. You would just start the for() loop at zero - <?php $data = ...; $value = 2*$data; //(0-20 in whole steps) echo "<ul>"; for($i=0;$i <=20;$i++){ echo "<li id='B$i'"; if($i == $value){echo " class='active'";} if($i > $value){echo " class='no'";} echo "></li>\n"; } echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/254075-retrieve-and-selected-advanced-using-php-mysql-html/#findComment-1302555 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.