We help IT Professionals succeed at work.

Fancybox/Overlay data grid

Medium Priority
31 Views
Last Modified: 2020-03-09
I have a large table (1000 records) that is generated from a SQL database and PHP.

I have a button on each row that opens a fancybox screen overlay that contains information from another table, based on the id of the row that contains the button. This overlay table could contain 1000 records.

Therefore, the whole page could contain 1,000,000 (1000 X 1000 records), which would take too long and kill the server etc.

My code displays the 'master' row and the 'slave' rows as follows:

<?php
//master table header
echo "<table id='download' class='tablesorter'>";
echo "<thead><tr>";
echo "<th>Id</th>\n";
echo "<th>...</th>\n";
echo "<th>Order Ref</th>\n";
echo "</tr></thead><tbody>";

//master table sql
$sql= "select  [id], [orderref],.... from table";
$result=sqlsrv_query($conn, $sql);
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$n=$n+1;

//master table content
echo "<tr>";
echo "<td>".$row['id']."</td>\n";
echo "<td>...</td>\n";


//button to show overlay with slace table
echo "<td>";
echo "<a data-fancybox data-src='#hidden-content".$n."' href='javascript:;'>".$row['orderref']."</a>";
echo "<div style='display: none;' id='hidden-content".$n."'>";

        //overlay content
        echo "<h1>Slave Table for Order ".$row['orderref']."</h1>";
        echo "<table id='slave' class='tablesorter'>";
        echo "<thead><tr>";
        echo "<th>Id</th>\n";
        echo "<th>...</th>\n";
        echo "<th>Order Ref</th>\n";
        echo "</tr></thead><tbody>";

        //slave table sql
        $sql= "select  [id], [content],.... from subtable where orderref='$orderref'";
        $result=sqlsrv_query($conn, $sql);
        while($rowslave=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {

        //slave table content
        echo "<tr>";
        echo "<td>".$rowslave['id']."</td>\n";
        echo "<td>...</td>\n";
        echo "<td>".$rowslave['content']."</td>";
        echo "</tr>";}
        echo "</tbody>";
        echo "</table>";

        //end of overlay
echo  "</div>";
echo "</td>";

echo "</tr>";}

//endof master table
echo "</tbody>";
echo "</table>";

Open in new window


I need to find a way to display all the master rows and then only perform the SQL to retrieve the slave rows when the relevant button is clicked, 

I am not sure if this needs to be done through Ajax, or I can just run the specific  SQL query for the slave row when the button is clicked.

Any help would be appreciated
Comment
Watch Question

Developer & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013
Commented:
You would use ajax.  Instead of outputing your slave data on this page, you would create a new page that accepts a get or post of the orderref and outputs the data either as json or html.

Your ajax would look something like https://api.jquery.com/jquery.ajax/ 
$.ajax({
  method: "POST",
  url: "ajaxprocessingpage.php",
  data: { orderref: "123" }
})
  .done(function( data) {
    alert( data);
  });
You would have to first get the orderref and then place the returned data where you need it.

The end result may look like
$('.submitbutton').on('click',function(){

     //assume button looks like 
     //<button class="submitbutton" data-orderref="123">Submit</button>
     var orderref=$(this).data("orderref");

     $.ajax({
       method: "POST",
       url: "ajaxprocessingpage.php",
       data: { orderref: orderref }
     })
      .done(function( data) {
         //alert( data);
         //assume data is formated in html
         //assume data is goint to #results
         $("#results").html(data);
  
      })

});

Explore More ContentExplore courses, solutions, and other research materials related to this topic.