how can show 5 millons of data datatable in codeigniter - php

I am new in php. I want to show 5 Millions of record from my database in php in codeignter. I am using select star sql query to select table. It's working good but takes long time in loading data into datatable.
Any help would be appreciated.
Thanks in Advance.
<?php
include('layout/header.php');
include('layout/sidebar.php');
?>
<div data-widget-group="group1">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Data Tables</h2>
<div class="panel-ctrls"></div>
</div>
<div class="panel-body no-padding table-responsive">
<form action="<?= base_url(); ?>signin/sendemailstudent" method="post">
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th style="padding-right:100px;">S.No</th>
<th>School</th>
<th>Date Of Admission</th>
<th>Register Serial</th>
<th>Student Name</th>
<th>Date Of Birth</th>
<th>Father Name </th>
<th>Phone </th>
<th>Email</th>
<th>Pincode</th>
<th>Class To Which Admitted</th>
<th>Class From Which Withdrawn</th>
<th>Date Of Withdrawl</th>
<th>Upload By</th>
<th>Notify<input type="checkbox" onclick="check();" id="select_all" name="select_all" /></th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($data as $value) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $value['school_name']; ?></td>
<td><?php echo $value['doa']; ?></td>
<td><?php echo $value['register_serial']; ?></td>
<td><a href='<?php echo base_url('signin/editstudent').'/'.$value['id']; ?>'><?php echo $value['name']; ?></a></td>
<td><?php echo $value['dob']; ?></td>
<td><?php echo $value['father_name']; ?></td>
<td><?php echo $value['phone']; ?></td>
<td><?php echo $value['email_address']; ?></td>
</tr>
<?php
$i++;
} ?>
<div class="clearfix pt-md">
</div>
</tbody>
</table>
</form>
</div>
<div class="panel-footer"></div>
</div>
</div>
</div>
</div>
<?php include('layout/footer.php'); ?>

As someone already mentioned:
paginate
filter
'using select star', dont use the star selector if you wont use all the fields.
codeignitter is using Active Record, something that uses DataMapper could be faster, not sure if you can change that though, most probably you can (havent use codeignitter). or using pure php pdo to deal with db.
have a look on how to use generators, they're much faster for processing big sets as they dont load everything in memory at once (that's what foreach would do)
not sure which version of php you're running, but if you're working on php <5.6 then you should definitely upgrade as new versions use much less memory/cpu for dealing with db.
install sql profiler to check if your queries are optimal.

Related

How to sort the output of sql data in php

I am trying to sort the output of the echo data that i am trying to bring from mysql database. There are different rows in the table and i want to sort by one of the rows named city. But it is showing data has last in first out. The code is here:
<?=APP::getElement()->triggerMessage(); ?>
<div class="panel">
<?php include_once(APP_HTML.'sub_menu.php');
$data=$model->data['data'];
// echo"<pre>";
// print_r($data);
?>
</div>
<?php include_once('search.php'); ?>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">All County</div>
<form name="List" id="List" class="form-horizontal" role="form" method="post" action="<?php echo $base_url.'delete';?>">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th><input type="checkbox" id="checkAll" name="checkAll" class="selectAll" /></th>
<th>County Name</th>
<!--<th> Parent County</th>
<th> County Status</th>-->
<!--<th> Product Type</th>
<th> Model No.</th>
<th> Image</th>
<th> Description</th>-->
<!-- <th>Country Name</th> -->
<th>Added Date</th>
<th>Updated Date</th>
<!-- <th>Weightage</th> -->
<th>Status</th>
<th width="12%">Action</th>
</tr>
</thead>
<tbody>
<?php
$pagination=$model->data['pagination'];
foreach($data as $row){
// echo $row['project_name'];
?>
<tr>
<td><input type="checkbox" id="id" name="id[]" class="checkID" value="<?php echo $row['id']; ?>"/></td>
<td><?php echo $row['city'] ?></td>
<td><?php echo $row['added_date']; ?></td>
<td><?php echo $update = empty($row['updated_date'])?'N/A':$row['updated_date']; ?></td>
<td><?php echo APP::getElement()->getStatus($row['status']); ?></td>
<td>
<?php $fields=array('update','delete','status'); ?>
<?=APP::getElement()->getActionButton($fields,$row);?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
</div>
<?php echo $pagination; ?>
please help.
Try to put ORDER BY in your SQL query.
Documentation

Passing Form Data to separate PHP Page

I have a table with multiple columns (index.php). One column is a checkbox. Whenever the checkbox is checked, it displays another row where you can select a quantity. You can then hit a button called "Add to Order" and it will take you to a confirmation page (index-order.php) where I want it to display each row along with all of the data in that specified row that has the checkbox checked. Currently, I am getting no errors in my console, but no data is being displayed at all.
What do I need to change to make this happen? Here is what I have so far.
Index.php code:
<form name="form1" method="POST" action="index-order.php">
<section id="addToOrder">
<button type="submit" class="order" id="order" name="order" value="AddToOrder">Add to Order</button>
</section>
<br>
<div id="my-div2" class="ui-widget">
<div class="ui-widget">
<table id="merchTable" cellspacing="5" class="sortable">
<thead>
<tr class="ui-widget-header">
<th class="sorttable_nosort"></th>
<th class="sorttable_nosort">Loc</th>
<th class="merchRow">Report Code</th>
<th class="merchRow">SKU</th>
<th class="merchRow">Special ID</th>
<th class="merchRow">Description</th>
<th class="merchRow">Quantity</th>
<th class="sorttable_nosort">Unit</th>
<th style="display: none;" class="num">Quantity #</th>
</tr>
</thead>
<tbody>
<?php foreach ($dbh->query($query) as $row) {?>
<tr>
<td class="ui-widget-content"><input type="checkbox" class="check" name="check"></td>
<td name="rows[0][0][loc]" class="loc ui-widget-content" id="loc-<?php echo intval ($row['Loc'])?>"><?php echo $row['Loc'];?></td>
<td name="rows[0][0][rp-code]" class="rp-code ui-widget-content" align="center" id="rp-code-<?php echo intval ($row['Rp-Code'])?>"><?php echo $row['Rp-Code'];?></td>
<td name="rows[0][0][sku]" class="sku ui-widget-content" id="sku-<?php echo intval ($row['SKU'])?>"><?php echo $row['SKU'];?></td>
<td name="rows[0][0][special-id]" class="special-id ui-widget-content" align="center" id="special-id-<?php echo intval ($row['Special-ID'])?>"><?php echo $row['Special-ID'];?></td>
<td name="rows[0][0][description]" class="description ui-widget-content" id="description-<?php echo intval ($row['Description'])?>"><?php echo $row['Description'];?></td>
<td name="rows[0][0][quantity]" class="quantity ui-widget-content" data-quantity="<?php echo $row['Quantity'] ?>" align="center" id="quantity-<?php echo intval ($row['Quantity'])?>"><?php echo $row['Quantity'];?></td>
<td name="rows[0][0][unit]" class="unit ui-widget-content" id="unit-<?php echo intval ($row['Unit'])?>"><?php echo $row['Unit'];?></td>
<td name="rows[0][0][quant]" style="display: none;" class="quantity_num ui-widget-content"><input type="textbox" style="width: 100px;" class="spinner" name="value" id="test"></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</form>
Index-order.php:
<?php if(isset($_POST['rows'])): ?>
<table cellspacing="20">
<tr align="center">
<th>Loc</th>
<th>Report Code</th>
<th>SKU</th>
<th>Special ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Unit</th>
<th>Quantity #</th>
</tr>
<?php
foreach($_POST['rows'][0] as $row):
?>
<tr align="center">
<td><?php echo $row['loc']; ?></td>
<td><?php echo $row['rp-code']; ?></td>
<td><?php echo $row['sku']; ?></td>
<td><?php echo $row['special-id']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['unit']; ?></td>
<td><?php echo $row['quant']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
I am okay with the precedent answer, I never heard about this kind of method with PHP and it doesn't seems to be the right solution. Anyway, the following post would maybe help you : How to get value from td's via $_POST.
You cannot transfer datas through POST by using td ; but an alternative would be to use the "hidden" type of forms element :
<form action="script.php" method="post">
<td class=".."><input type="hidden" name="td1" value="...">value</td>
...
</form>
In PHP, you'll grab the data with the $_POST array and the td1 name :
<?php var_dump($_POST); ?>
Itwould in my opinion be the easier way to get what you want in a proper way ; the link I gave upper is also talking about DOMDocument, but it looks more complex to manage with.

how to create link for one user id from datatable and view only that user id detail in next page?

I have a function manage_child which it is in a datatables that show all children details. My problem is I want to view only one child details at another pages. I have tried to implement this example of code in my function but it didnt work since I call it like this:
`<td><a href="?pages=child_view"</a><?php_e($child_id); ?></td>`
I have link the page where the details should go but it didn't work. I am still in my basic using php and I use mysqli for the database.
This is the example that I tried to implement in my code but still doesn't work.
echo "<td>".$row[id]."</td>";
echo "<td>".$row[l_name]."</td>";
echo "<td>".$row[f_name]."</td>";
echo "<td>".$row[ssn]."</td>";
this is my code
function manage_child(){
$query = query("SELECT * FROM register_child");
confirm($query);
?>
<div class='container-fluid'>
<div class='row'>
<h1 class="page-header">Children
<small>Information</small>
</h1>
<div class="panel-body">
<div class="dataTable_wrapper">
<table width="50%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>CHild ID</th>
<th>Child IC</th>
<th>Name</th>
<th>Address</th>
<th>Mother name</th>
</tr>
</thead>
<tbody>
<?php
$bil = 0;
while($row = fetch_array($query)) {
$password = $row['ic_no'];
$child_id = $row['child_id'];
$child_name = $row['child_name'];
$address = $row['address'];
$mother_name = $row['mother_name'];
$bil++;
?>
<tr class="odd gradeX">
<td><?php echo $bil; ?></td>
<td><?php _e($password);?></td>
<td><a href="?pages=child_view"</a><?php _e($child_id); ?></td>
<td><?php _e($child_name); ?></td>
<td><?php _e($address); ?></td>
<td><?php _e($mother_name); ?></td>
<?php
}// End of while
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php
}

Active/inactive users using checkbox in codeigniter

i need to update my database as soon as i check the checkbox with the value of 0(inactive) and 1(active). if i uncheck the checkbox the user would be unable to access the page means he/she is blocked from user panel. and if i check the box again the user can be enable to access the whole web means he/she would be unblocked.
i wonder if i can do this in codeigniter. i have searched for it but i cannot get them. Advance sorry it there is silly one.
Thankyou for any help.
This is my view file :
<div class="col-main">
<h1 class="page-title">All Users</h1>
<div class="admin_panel">
<ul class="form-fields">
<li class="full-row">
<table class="table demo">
<tr>
<td colspan="3">Add New</td>
</tr>
<tr>
<td colspan="3"><label>Quick Search : </label>
<input id="filter" type="text"/></td>
</tr>
</table>
</li>
</ul>
<table class="table demo table-bordered product_grid" data-filter="#filter">
<thead>
<tr>
<th>S.No</th>
<th>First Name</th>
<th>last Name</th>
<th>Password</th>
<th>Username</th>
<th>Password</th>
<th>Status</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody> <tr>
<?php if(isset($pro_data) && $pro_data != false){ ;
foreach ($pro_data as $da) { ?>
<td><?php echo $da['reg_id']; ?></td>
<td><?php echo $da['reg_fname']; ?></td>
<td><?php echo $da['reg_lname']; ?></td>
<td><?php echo $da['reg_pass']; ?></td>
<td><?php echo $da['reg_email']; ?></td>
<td><?php echo $da['reg_pass']; ?></td>
<td><?php $data = array(
'name' => 'reg_fname',
'id' => 'reg_id',
'value' => 'accept',
'checked' => TRUE,
'style' => 'margin:10px',
);
echo form_checkbox($data);?></td>
<td class="edit"><a title="Edit" href="edit-user.php"><img title="Edit" alt="Edit" src="images/edit_icon.png"></a></td>
<td class="del"><a title="Remove" href="#"><img title="Remove" alt="Remove" src="images/delete_item_btn.png"></a></td>
</tr>
<?php }?>
<?php }?>
</tbody>
You can send a AJAX Request to the CodeIgniter Controller. The Controller updates the database. As soon as a User wants to visit the site, you check if he has the right to do.
If a user should be immediately get the new right (for example if he is on the forbidden site), you need Websockets.
I hope this helps. Your Question only gives little information about your Problem. Do you want immediately set rights (-> AJAX)? Why not a Form? Maybe I'm wrong and you ask for a completely different causing.

Database info not showing from my loop

I new to php and learning and need some advice on shoing my database info within a loop which includes html, My page is blank so am wondering why my php isnt valid:
<?php include 'incudes/connect_db.php'; ?>
<div class="topbar">
<div class="topbarblock1">Dashboard</div>
<div class="topbarblock2">Username</div>
</div>
<br><br>
<div class="middle">
<div class="middleblock1">Dashboard</div>
<div class="middleblock2">
<div class="container">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
?>
<h2>
User Management
<hr />
</h2>
</div>
<div class="containermain">
<table>
<tr>
<th class="th1">User Id</th>
<th class="th2">Username</th>
<th class="th3">Email Address</th>
<th class="th4">Details</th>
</tr>
<tr>
<td class="td1">#</td>
<td class="td2"><?php echo $row['username']; ?></td>
<td class="td3"><?php $row['email']; ?></td>
<th class="td4">Edit</th>
</tr>
</table>
</div>
</div>
</div>
<?php
}
?>
I'm not looking for a straight out answer I am looking for some advice on how this should be laid out and learn best practices.
Change the line that says
<td class="td3"><?php $row['email']; ?></td>
to
<td class="td3"><?php echo $row['email']; ?></td>
PHP will try to interpret the variable as some sort of command/statement otherwise and subsequently fail.
Also rethink where you wrap the loop as I previously mentioned, or else your HTML is going to end up wonky (you'll see this after you've fixed the code.)
You should also program by using error_reporting(E_ALL); at the start of your code so you can fix simple mistakes on your own

Categories