1

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']);
}

?>
5
  • What have you tried? What's not working? Commented May 28, 2018 at 17:30
  • Can we have the php part? Commented May 28, 2018 at 17:30
  • Charlie, I haven't tried anything. I have know idea how to do it. Commented May 28, 2018 at 18:01
  • @Tobia, I included the php code in the post now. thanks. Commented May 28, 2018 at 18:01
  • @Tobia, do you know how i can get this result? can you help me? Commented May 29, 2018 at 7:03

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

8 Comments

thanks, I will check your code. here is a visual example of the result I try to achieve. oi68.tinypic.com/2j5h2me.jpg
Did you check it?
yes, what I did is this according to your code and instructions. jsfiddle.net/c5eca23L I couldn't make it work. but if I am not mistaken about your instructions, your solution requires manual editing of javascript for every value to create js objects? in the example i have three tables, tables will be all same, it won't change. but values to be highlighted change regularly.
My solution is to set a js object with default values to be highlighted. In your code you're missing the defaultValue object, it is not defined. In my filled it was in the html part. You have to create a php variable with default values, convert it to json string and then output it as javascript object.
I edit my answer editing the php code with a more verbose solution with a DB query to compile a "$out" object to be print to javascript.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.