How to make dynamic table on php codeigniter - php

i'm new on using codeigniter, i want to ask how to make dynamic table so when i select data from anytable form database it can be fit with the table, even the field is different.
so, in normally i show table like this :
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#Number</th>
<th scope="col">Field</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($data as $row) {?>
<tr>
<th scope="row"><?php echo $no++?></th>
<td><?php echo $row->COLUMN_NAME ?></td>
</tr>
<?php } ?>
</tbody>
</table>
but the problem when i using 3 field or more field it can't be fit, so any suggestion for it?

Your problem:
You are fetching data from database.
And want to display it in table but, not sure how many columns are there.
Solution:
Say, you have a multi-dimensional array with n records.
First get the first element (which is a database row, a table row)
Get count of it.
Now loop over the array.
Use foreach() language construct.
It will take care of every thing.
Note: This solution assumes that the individual array (database records) are having same number of columns.
<?php
if (! empty($arr)) {
foreach ($arr as $elem) {
?>
<tr>
<?php
if (! emtpy($elem)) {
foreach($elem as $td) {
?>
<td><?php echo $td;?></td>
<?
}
}
</tr>
<?
}
}

Related

How to user fetch_assoc() twice in a single php file? [duplicate]

This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed 1 year ago.
I have two table. One is an article list and the other one is a category list. In the category table, I have use fetch_assoc() to get the data from my DB and list in the table. But I want to fetch the article's data in the article table. How do I use fetch_assoc() twice in the same php file?
<table>
<caption class="table_title">Category Management</caption>
<thead>
<tr class="table_header">
<th>ID</th>
<th>Ttile</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while($row = $categoryResult->fetch_assoc()) {?>
<tr class="table_content">
<td><?php echo escape($row['id'])?></td>
<td><?php echo escape($row['title'])?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
</table>
articles table
<tbody>
<?php while($row = $result->fetch_assoc()) {?>
<tr class="table_content">
<td></td>
<td></td>
<td></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
Replace
while($row = $categoryResult->fetch_assoc()) {
with
foreach($categoryResult as $row)
It does the same but the foreach approach uses an automatic iterator that will always start from the beginning of the result set.
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
If for some reason, you must use while loop and the manual approach then you need to ensure that you always reset the internal pointer to the first records before starting your loop. However, manual looping is not recommended. It is easier to make more errors when doing this manually with while
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}

Loop in table row & display data which is available in array using PHP

I'm trying to loop in a table row 16 times with 2 elements of array. I know I can't loop 16 times with 2 elements of data but I want to display a table which has 16 row and only first 2 row will have data and others will leave blank.
Same as the image below
I have tried this code but its not what I want
<th>No</th>
<th>Id</th>
<th>Name</th>
for($i=1; $i<=16; $i++)
{
<tr>
<td> echo $i </td>
foreach($array as $a){
<td> $a->id </td>
<td> $a->name </td>
}
</tr>
}
You're looping through your entire array, 16 times. I assume that your array only has 2 elements in it. I'm going to assume it may not always have 2 elements in it, and if it has, say, 8 elements in it, you want the first 8 rows populated with data, but you always want 16 rows to display?
Additionally, I'm assuming that your $array variable is numerically indexed.
If all of that is true, then what you want is to eliminate your foreach, and just access the array element by index:
<thead>
<tr>
<th>No</th>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php for ($i=0; $i<=15; $i++) {
if (!empty($array[$i])) { ?>
<tr>
<td><?= $i+1; ?></td>
<td><?= $array[$i]->id; ?></td>
<td><?= $array[$i]->name; ?></td>
</tr>
<?php }
else { ?>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
Note that PHP arrays are zero-based, hence why I did 0-15 instead of 1-16.

How to run two foreach loop inside one html table in Laravel?

I want to show two foreach loop data dynamically inside one table, but the design of the table break after dynamic loop data.
My blade.php code is given below:
<table>
<tr>
<th>Name</th>
<th>Buy Rate</th>
<th>Sell Rate</th>
</tr>
<tr>
<?php foreach($datav as $data){ ?>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->buy_rate; ?></td>
<?php } ?>
<?php foreach($sells as $sell){ ?>
<td><?php echo $sell->rate; ?></td>
</tr>
<?php } ?>
</table>
My route is:
Route::get('/','WelcomeController#test');
My Controller Code:
public function test(){
$datav= DB::table('localcurrency')
->where('status',1)
->get();
$sells= DB::table('localcurrency2')
->where('status',1)
->distinct()
->get();
return view('home.home_content')
->with('datav',$datav)
->with('sells',$sells);
}
How can I run this?
It is better if you merge both $datav and $sells in one array.
In this above scenario firstly your $datav array will complete then it goes to second array.
So it will not create a complete table. Or the table alignment will be wrong.
Rest depends on what you proceed in the array.
public function test(){
$datav= DB::table('localcurrency')
->where('status',1)
->get();
$sells= DB::table('localcurrency2')
->where('status',1)
->distinct()
->get();
$sells=$sells->toArray();
$results=array();
foreach($datav as $key=>$data)
{
$newarr=array();
$newarr['name']=$data->name;
$newarr['buy_rate']=$data->buy_rate;
$newarr['sell_rate']=$sells[$key]->rate;
$results[]=$newarr;
}
return view('home.home_content')
->with('results',$result);
}
Your views looks like this
<table>
<tr>
<th>Name</th>
<th>Buy Rate</th>
<th>Sell Rate</th>
</tr>
#foreach($results as $result)
<tr>
<td>{{$result['name']}}</td>
<td>{{$result['buy_rate']}}</td>
<td>{{$result['sell_rate']}}</td>
</tr>
#endforeach
you better to use Blade template engine syntax #foreach($values as $value) {{$value}} #endforeach
you better understand what structure of output you want to get
your task is not resolved in this case because if you want to fill
one table row by row by some data, this data should be interconnected
one to one relationship, so you should do it inside of one cycle
you can't fill first every values of first two columns of table and
then another column's values. Table should be filled row by row

How should I change this scraping PHP script to work with this table?

I'm using this code to scrape some info from a table on a website. One example I have works, because it has a row of th, followed by tr, td (the th is the first row above the other rows horizontally).
$dom = new \simple_html_dom($html);
$rows = $dom->find('table.table-bordered tbody tr');
$header = [];
foreach ($rows as $row) {
if(!empty($header)) break;
foreach ($row->find('th') as $key=>$th) {
$header[] = trim(html_entity_decode($th->plaintext));
}
}
$cells = [];
foreach ($rows as $row) {
$cell = [];
foreach ($row->find('td') as $key=>$td) {
$cell[$header[$key]] = trim(html_entity_decode($td->plaintext));
}
if(!empty($cell)) {
$cells[] = $cell;
}
}
The problem is that another example table I have has a different structure and I'm unsure how to change the code to reflect it. The th is on each row vertically as the first column of the table. Thus the first th gets repeated in the output as the key for all rows.
<table class="table table-bordered">
<tbody>
<tr>
<th> Sender </th>
<td> Test </td>
</tr>
<tr>
<th> Number </th>
<td> 1234 </td>
</tr>
<tr>
</tbody>
</table>
There is also a second table with no class nor id, which I would like to get separately. Is there a way to skip the first table?
<table class="table">
<tbody>
<tr>
<th> Table 2 cell 1 </th>
<td> Test table 2 </td>
</tr>
<tr>
<th> Number something </th>
<td> 1234 table 2 </td>
</tr>
<tr>
</tbody>
</table>
The output looks like this (json encoded):
[{"Sender":"Test"},{"Sender":"1234"},{"Sender":"Test table 2"},{"Sender":"1234 table 2"}]
Should be:
[{"Sender":"Test"},{"Number":"1234"},{"Table 2 cell 1":"Test table 2"},{"Number something":"1234 table 2"}]
Or ignoring the first table table table-bordered:
[{"Table 2 cell 1":"Test table 2"},{"Number something":"1234 table 2"}]
Sender should not be the key for each row. What should be changed in the PHP code to read this table correctly? I don't think the $dom->find is actually finding single rows and then looking for th and td inside.
I think the following line of code will let you scrape the second table only. When you write like this [class='table'] then the selector will ignore all the compound classes having the same portion within, meaning it will look for the class only having table.
Replace the following line with the existing one being used within your script:
$rows = $dom->find("[class='table'] tbody tr");

How to display static values in td column in codeigniter

Hi all i have one table there i need to display the course report so every course has one stage so that i have 16 types of stages ex: On Hold,Asset Incomplete,SME Discussion..etc..Like 16 types i have So i would like to display 16 types in mt td column
<table class="table table-bordered">
<thead>
<tr><th class="center">#</th>
<th class="center">PHY</th>
<th class="center">CHE</th>
<th class="center">ZOO</th>
<th class="center">BOT</th>
<th class="center">MATH</th>
<th class="center">Total</th>
</TR>
</thead>
<tbody>
<tr>
<td>On Hold</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
Remaining td's i have to get count of every course and to display
Can anyone help me how can i do that.
Thanks in advance.
You can do it by taking all your static string as an associative array for example
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion');
and your td should be
<?php $i = 0;
foreach($data as $dt){ ?>
<td><?php echo $arr[$i]; ?></td>
<?php $i++; } ?>
If you want to pass a variable from your controller to your view, you do so in the $data variable.
//Controller
$this->load->model('mymodel);
$data['var1'] = "Some value";
$data['var2'] = "Some value";
$data['query'] = $this->mymodel->thefunction();
//Model = mymodel
//function = thefunction(), you have to obviously create the class and function, if you don't know how to do that, let me know
$sql = "SELECT Count(courseid) as 'statusCount'";
$query = $this->db->query($sql);
When you now want to display those variables in your view template, call the variable by it's name, without the "$data part"
//In your view
<?php
foreach($query->result() as $row) {
echo "<td>" . $row->statusCount . "</td>";
}
?>
Please set the array with two fields.
One for Id,another for course name.
Then use forech() for write <td> in view.html.
For example:
forech($array as $item){
echo('<td><a href="ProjectView.php?course_id=".$item["id"]>$item["name"]'</a></td>') ;
}

Categories