Jump to content

retrieve and selected advanced <UL><LI> using php / mysql / html


Recommended Posts

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


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


<?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


<?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.

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

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 ?!

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

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.