Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:38:07+00:00 2026-06-16T16:38:07+00:00

First I’ll mention what I am trying to achieve. I am using CodeIgniter framework

  • 0

First I’ll mention what I am trying to achieve. I am using CodeIgniter framework of PHP. I have 5 tables in my database and I want to display them in Datatables format on a button click on the display page. I am using server side processing php as data source. So at first I made the code for displaying only one table in Datatable format and was successful in it. Now I want to display one table at a time out of 5 on button click event. But $aColumns length should be equal to number of columns defined in HTML table. Now considering marks tabe, it has 4 columns student_id, exam_id, subject_id and marks_achieved. Now another table is branch and has 2 columns only branch_id and branch_name. So I cannot increase or decrease tags in HTML dynamically so I am confused.
Also I am using this source to create datatables.
You can check my getTable() function here.

jQuery:

$(document).ready(function()
{
$('#datatable').dataTable({
    "sPaginationType":"full_numbers",
    "bJQueryUI":true,
    "bProcessing": true,
    "bServerSide": true,
    "sServerMethod": "GET",
    "sAjaxSource": "datatable/getTable",
    "iDisplayStart": 0,
    "iDisplayLength": 10,
    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
    "aaSorting": [[0, 'asc']],
    "aoColumns": [
        { "bVisible": true, "bSearchable": true, "bSortable": true },
        { "bVisible": true, "bSearchable": true, "bSortable": true },
        { "bVisible": true, "bSearchable": true, "bSortable": true },
        { "bVisible": true, "bSearchable": true, "bSortable": true }
        ]
})

$('input[type=button]').bind('click', function(){
        var param = $(this).attr('id');
            data = param + '=1';

        $.ajax({
            type: 'POST',
            url: 'datatable', 
            data: data                
        }).done(function( data ) { 
            console.log(data);
            $('#display_area').html(data); 
        });
    })
    });

HTML:

<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/javascript.js"></script>
<script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
</head>
<body id="dt_example">
<form action="" method="post">
<input type="button" id="display_branch" name="display_branch" value="Display Branch Table" >
<input type="button" id="display_marks" name="display_marks" value="Display Marks Table" >
</form>   
<div id="container">
<div id="demo">
<table id="datatable" cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
    <tr>
        <th>Student ID</th>
        <th>Exam ID</th>
        <th>Subject ID</th>
        <th>Marks Achieved</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</tbody>
<tfoot></tfoot>
</table>
</div>
<div class="spacer"></div>
</div>
</body>
</html>

To get the columns dynamically I have made this change as shown below in datatable.php but it is not working. What is wrong here or I should try some other approach ?

   if(isset($_POST['display_marks']))
    {
        $aColumns = array('student_id', 'exam_id', 'subject_id', 'marks_achieved');
        $sTable = 'marks';
    }
    if(isset($_POST['display_branch']))
    {
        $aColumns = array('branch_id', 'branch_name');
        $sTable = 'branch';
    }

EDIT:
The solution posted by user1190992 works but the whole approach is changed. And in that I want to sanitize the headers of the columns. “branch_id” is displayed instead I want to display Branch ID. How can I perform this sanitization ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:38:08+00:00Added an answer on June 16, 2026 at 4:38 pm

    This is a very simple way for creating HTML from JSON data dynamically. It doesn’t use server side processing though.

    JavaScript

    $(document).ready(function() {
        $(".abutton").click(function() {
            $('#myDatatable_wrapper').detach(); //Remove existing table
            var table = '<table id="myDatatable" class="table"><thead><tr>';
            $.ajax({
                url: 'dt.php',
                data: "table_id="+$(this).attr("id"),
                type: "POST",
                success: function (data) {
                    $.each(data.aoColumns, function(key, value) {
                        table += "<th>"+value+"</th>";
                    });
                    table += "</tr></thead><tbody>";
                    $.each(data.aaData, function(key, row) {
                        table += "<tr>";
                        $.each(row, function(key, fieldValue) {
                            table += "<td>"+fieldValue+"</td>";
                        });
                        table += "</tr>";
                    });
                    table += '<tbody></table>';
                    $('.container').html(table);
                    $('#myDatatable').dataTable();
                },
                dataType: "json"
            });
        });
    });
    

    PHP

    $table_id = filter_input(INPUT_POST, "table_id", FILTER_SANITIZE_STRING);
    $dbconn = mysqli_connect("localhost", "username", "password");
    
    if($table_id == "table1") {
        $sql_query = mysqli_query($dbconn, 'SELECT * FROM display_branch');
    }
    else {
        $sql_query = mysqli_query($dbconn, 'SELECT * FROM display_marks');
    }
    
    if(mysqli_num_rows($sql_query) == 0) {
        echo "Check your ID";
        exit(1);
    }
    $data = array();
    $data['aaData'] = array();
    while($row = mysqli_fetch_assoc($sql_query)) {
        $data['aaData'][] = $row;
    }
    
    $data['aoColumns'] = array();
    while($finfo = mysqli_fetch_field($sql_query)) {
        $data['aoColumns'][] = $finfo->name;
    }
    echo json_encode($data);
    

    HTML

    <button id="table1" class="abutton">Table 1</button><br /><button id="table2" class="abutton">Table 2</button>
    <div class="container"></div>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First you should know I have looked into many questions and none of them
First of all, I'm using ARC. If I have a class that, for example,
First of all, I have only worked with scaffolding in rails. What I want
First the IDE that i am using is the visual C# with .net framework.
first of all, I'm not good with jQuery and coding. I'm trying to achieve
First, I want to mention that I read many stackoverflow posts about NoClassDefFoundError ,
first take a look on this picture from localScope app : i have 2
first off I'm a noob to PHP but here is my problem. I am
First, some context: I'm a Python developer who has written a medium-sized application using
First sorry for my english. I am currently trying to develop a JAVA webservice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.