I want to view data from database in my website using CodeIgniter 3. There are 2 types of ways to view my data in this website. First is without looping, I used this code :
<?php $s=$sched_stud->row();?>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<th scope="row">Full Name</th>
<td><?php echo $s->name;?></td>
</tr>
<tr>
<th scope="row">Level Enrolled</th>
<td><?php echo $s->level;?></td>
</tr>
</tbody>
</table>
and the second one is using foreach, this is my code :
<tbody>
<?php
$a=1;
foreach ($sched_stud as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
but there are errors saying :
Undefined property: mysqli::$room
how can i fix this?
In first way you are getting only one record with row()
SO in your second way for loop you need result() to get multiple records
<tbody>
<?php
$a=1;
foreach ($sched_stud->result() as $key) { ?>
<tr>
<th scope="row"><?php echo $a; $a++; ?>.</th>
<td>Tue, 8 Jan 2019</td>
<td>17.00 - 18.30</td>
<td><?php echo $key->room;?></td>
<td>Mrs. Adinda</td>
<td>Upcoming</td>
</tr>
<?php } ?>
</tbody>
Related
i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>
I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.
I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to have a link to another page, with particular subject, How can I pass the id ?
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($subjects as $sub):?>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
You still need to echo it out.
<?php foreach($subjects as $sub): ?>
<tr>
<td><?php echo $sub->subject ?></td>
<td>Add Topic</td>
</tr>
<?php endforeach; ?>
Please try the following:
<table class="table table-hover">
<thead>
<tr>
<th>Subject</th>
<th>Add Topic</th>
</tr>
</thead>
<tbody>
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
and then on the page : approve.php
<?php
$subjectId = $_GET['id'];
?>
$subjectId will give you the corresponding subject id with which you can move forward with the functionality.
Note: foreach should start either outside <tr> and end outside </tr> or it can be inside <tr> </tr>
You need to enclose your variable in a PHP tag:
<?php foreach($subjects as $sub):?>
<tr>
<td><?php echo $sub->subject;?></td>
<td>Add Topic</td>
</tr>
<?php endforeach;?>
There is also a short form echo tag enabled on most PHP servers <?= $variable ?>
On the subsequent page you retrieve the parameter from the GET array:
$subject = $_GET['id'];
If you're passing this value to the database you should do some validation:
if ($_GET['id']) { // check parameter was passed
$subject = int_val($_GET['id']) // cast whatever was passed to integer
} else {
// handle no subject case
}
Yes you can, it will be considered as GET. as for how to pass it.
Edit the following:
<td>Add Topic</td>
This is the part:
<?=$sub->id?>
You closed php tag when u started adding html therefore open it again to echo php vars.
I'm trying to use this chart generator from http://htmldrive.net/items/show/792/Rare-Accessible-charts-using-jQuery-and-HTML5.html
Here's the code which loads data from mysql database:
The query works, but I guess my interpretation of the example provided in the site was wrong.
I get an output if I do it this way(predefined data):
<tr>
<th scope="row">Profit</th>
<td>5</td>
<td>5</td>
</tr>
But when I do it this way I get a blank output:
?>
<table>
<caption> Reports</caption>
<thead>
<tr>
<td></td>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<th scope="col"><?php echo $row['Cust_Name']; ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Subtotal</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTAL_PUR']; ?></td>
<?php } ?>
</tr>
<tr>
<th scope="row">Profit</th>
<?php while($row=mysql_fetch_assoc($query)){ ?>
<td><?php echo $row['TOTALPROFIT']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
Here's what I'm getting:
After the first iteration through the rows, when you display the customer names, the fetch data pointer is at the end of the dataset... you're trying to fetch the set again without resetting the pointer.
Try issuing
mysql_data_seek($query, 0);
before the while loops to display total and profit