droid fiji droid fiji - 3 years ago 82
Ajax Question

change color of specific div dynamically

i would like to change the

divs
color accordingly, dynamically if possible this is what i have so far:

My database: shape

id is_success
id1 0
id2 1
id3 0
id4 1

<div class="container" style="background: black; width: 500px; height: 500px;">

<div style ="id="id1" width: 35%; height: 14%; margin:20%"> div1 </div>
<div style ="id="id1" width: 35%; height: 14%; margin:80%"> div2 </div>
<div style ="id="id1" width: 35%; height: 14%; margin:10%"> div3 </div>
<div style ="id="id1" width: 35%; height: 14%; margin:60%"> div4 </div>

</div>

<?php
$sql = "SELECT * FROM shape ";

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$id = $row['id'];
$is_success = $row['is_success'];
if ($is_success == 1)
{
//change the divs background color to green
}else {
//change the divs background color to red
}
}
}


this is what the end result should look like this:

enter image description here

so when ever a value changes from the database the background color of the div should be set is this possible
Updated: Please Note that Each div are located at different positions on the html page

Answer Source

Parse your dynamic id to the respective div with background color.

$id = $row['id'];
$is_success = $row['is_success'];
if ($is_success == 1)
{
      // IF div id's and database id's are same try this line
      ?><script>$("#<?php echo $id;?>").css("background","green");</script><?php
      // Create new div with color dynamically try this line
      echo "<div id='".$id."' style='background:green;width: 35%;height: 14%; '> ".$id." </div>";
}else {
      ?><script>$("#<?php echo $id;?>").css("background","red");</script><?php
      // Create new div with color dynamically try this line
      echo "<div id='".$id."' style='background:red;width: 35%;height: 14%; '> ".$id." </div>";
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download