Max
Max

Reputation: 940

Auto highlighting using values from DB with php

I have five (sometimes 7,8 etc.) php generated small HTML tables on the same page, each having a different <table id="">. and I have data on mysql table with unique IDs which is the same as the html table ids.

What I need is each html table matching values to be highlighted with the values of the mysql table row with the same table id.

So instead of using a checkbox with multiple selector values, I need it to be done automatically without clicking any checkbox. Normally with checkboxes, I use something like this: https://jsfiddle.net/zt54jqtL/ Thanks!!

<div>

<form id="form1" name="form1" method="post" action="">
    <label>
        <input type="checkbox" name="SelectAll" class="all" />All</label>
    <label>
        <input type="checkbox" name="2" class="selector" />2</label>
    <label>
        <input type="checkbox" name="7" class="selector" />7</label>
    <label>
        <input type="checkbox" name="7" class="selector" />7</label>

</form>

PHP code:

 <?php

$conn=mysqli_connect("localhost","root","","Mdata");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


function print_tableX ($conn, $id) {
    $sql = "SELECT Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8 FROM tableA";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo "<table id='$id'>";
        while($row = $result->fetch_assoc()) {
            echo '<tr><td>' . join('</td><td>', $row) . "</td></tr>\n" ;
        }
        echo "</table>";
    }
}  


$result = $conn->query("SELECT sID from tableB");
while ($row = $result->fetch_assoc()) {
    print_tableX ($conn, $row['sID']);
}

?>

Upvotes: 1

Views: 171

Answers (1)

Tobia
Tobia

Reputation: 9520

I am not sure I have understood correctly the question. Moreover from your code I cannot understand which rows have to be highlighted.

Let's say that this is the html generated:

<table id="tab1">
    <tr data-dbval="3"><td>Value</td><td>3</td></tr>
    <tr data-dbval="5"><td>Value</td><td>5</td></tr>
    <tr data-dbval="8"><td>Value</td><td>8</td></tr>
 </table>

 <table id="tab2">
    <tr data-dbval="2"><td>Value</td><td>2</td></tr>
    <tr data-dbval="7"><td>Value</td><td>7</td></tr>
    <tr data-dbval="8"><td>Value</td><td>8</td></tr>
 </table>

and this a javascript object with row references to be highlighted:

<script>
    var defaultValue={"tab1":["3","8"],"tab2":["7"]}
</script>

This jquery code gets values to be highlighted per table and adds a "highlight" class to tr element:

$(document).ready(function() {

  $.each(defaultValue,function(k,v){
    var $tab=$("table#"+k);
    if($tab.length>0){
        $.each(v,function(k1,v1){
        $tab.find("tr[data-dbval='"+v1+"']").addClass("highlight");
      })
    }
  })

});

https://jsfiddle.net/moc5sq4w/

To generate JS object from PHP you can use this (this is ugly embedded php). The SQL is just for example, I did not understand your DB data structure:

<script>
var defaultValue=<?php
$result = $conn->query("SELECT sID from tableB");
$out=array();
while ($row = $result->fetch_assoc()) {
    $id=$row['sID'];
    $out[$id]=array();
    $sql = "SELECT Value1, Value2, Value3, Value4, Value5 FROM tableA WHERE sID='$id'";
    $result1 = $conn->query($sql);
    if ($result1->num_rows > 0) {
        while($row1 = $result1->fetch_assoc()) {
           $out[$id][]=$row;
        }
    }
}
echo json_encode($out);

?>;</script>

Upvotes: 1

Related Questions