I just want to create a table on HTML with a PHP loop. So, I try to do this:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php endfor; ?>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
</tr>
</tbody>
</table>
But, why the output becomes this?
Assuming you have an array like this:
[0] => Array
(
[stud_id] => 1234
[name] => John Doe
[class] => Class 1
)
[1] => Array
(
[stud_id] => 2345
[name] => Jane Doe
[class] => Class 2
)
My table loop will be look like this:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
foreach($array as $data) {
?>
<tr>
<td><?=$data['stud_id']?></td>
<td><?=$data['name']?></td>
<td><?=$data['class']?></td>
</tr>
<?php } ?>
</tbody>
</table>
You must put the tbody contents (tr, td) inside the loop
It's because you've got a loop inside a loop.
Try this instead:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<td><?php echo "Class ". 10-$i . "\n" ;?></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
Assuming that you really need two nested loops.
You need to move the endfor to the end of the loop, otherwise there will be <tr> before the 2nd loop
So
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
<?php endfor; ?>
</tr>
</tbody>
</table>
How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>
I hava a string and its
$value ='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
And I want to show this value in a table like
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
<tr>
<th>Category</th>
<th>Code</th>
</tr>
<tr>
<td>XYZ</td>
<td>101</td>
</tr>
<tr>
<td>XYZ</td>
<td>102</td>
</tr>
<tr>
<td>XYZ</td>
<td>103</td>
</tr>
<tr>
<td>XYZ</td>
<td>104</td>
</tr>
<tr>
<td>XYZ</td>
<td>105</td>
</tr>
<tr>
<td>ABC</td>
<td>201</td>
</tr><tr>
<td>ABC</td>
<td>202</td>
</tr>
<tr>
<td>ABC</td>
<td>203</td>
</tr>
<tr>
<td>ABC</td>
<td>204</td>
</tr>
<tr>
<td>ABC</td>
<td>205</td>
</tr>
</table>
can anybody help me for this
Explode the values on comma and colon and you get an array with the values. Then output them accordingly.
$value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
$arr = explode(",", $value);
Foreach($arr as $pair){
$parts =explode(":", $pair);
Echo "<tr>\n<td>";
Echo $parts[0];
Echo "</td>\n<td>";
Echo $parts[1];
Echo "</td>\n</tr>";
}
https://3v4l.org/TbtbX
Please check below mentioned solution. This will help you.
$str = 'xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
$array = explode(',', $str);
$temp = array();
foreach ($array as $i => $j):
$temp[$i] = explode(':', $j);
endforeach;
?>
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
<tr>
<th>Category</th>
<th>Code</th>
</tr>
<?php foreach ($temp as $value): ?>
<tr>
<td><?= $value[0] ?></td>
<td><?= $value[1] ?></td>
</tr>
<?php endforeach; ?>
</table>
Let me know if it not works.
Even if I don't like spoonfeeding, there you go:
<?php
$value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
$array = explode(",", $value);
?>
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
<tr>
<th>Category</th>
<th>Code</th>
</tr>
<?php foreach($array as $value) :
$exploded = explode(":", $value);
$key = $exploded[0];
$value = $exploded[1];
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value; ?></td>
</tr>
<?php endforeach; ?>
</table>
IF it is key value pair the use foreach :
foreach ($value as $key => $v) {
echo "<tr>";
echo "<td>".$key."</td>";
echo "<td>".$v."</td>";
echo "</tr>";
}
If value='' is a part of your string; get quotations content by this code
$string = "value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205'";
$matches = array();
preg_match( '/value=\'([^\']*)\'/i', $string, $matches ) ;
$content = $matches[0];
and use this code to generate table :
echo "<table width='50%' border='1' cellpadding='10' cellspacing='10'>";
$array = explode(',', $content);
foreach ($array as $item) {
$data = explode(':', $item);
echo "<tr>";
echo " <th>$data[0]</th>";
echo " <th>$data[1]</th>";
echo "</tr>";
}
echo "</table>";
Use explode() and foreach:
<?php
$value = 'xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
$rows = explode(',', $value);
?>
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
<tr>
<th>Category</th>
<th>Code</th>
</tr>
<?php
foreach ($rows as $row) {
$values = explode(':', $row);
?>
<tr>
<td><?php echo $values[0]; ?></td>
<td><?php echo $values[1]; ?></td>
</tr>
<?php
}
?>
</table>
For reference, see:
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/control-structures.foreach.php
For an example, see:
https://3v4l.org/tJcrc
I have a nested associative array that prints out users data in a table. Here is the code:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Prenume</th>
<th>Nume de familie</th>
<th>Email</th>
<th>Telefon</th>
<th>Oras</th>
<th>Adresa</th>
</tr>
</thead>
<tbody>
<?php foreach ($user_data as $arr){ ?>
<tr>
<td>
row number nedded here
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
I need to display the row number instead at the most left, instead of "row number nedded here"
You could just add a variable as a counter and display that:
<tbody>
<?php $counter=0; foreach ($user_data as $arr){ ?>
<tr>
<td>
<?php echo ++$counter; ?>
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
You can do it like this, if you have not any key you have index number starting from 0.
<tbody>
<?php foreach ($user_data as $key=>$arr){ ?>
<tr>
<td>
<?php echo $key+1 ;?>
</td>
<td>
<?php echo $arr["prenume"];?>
</td>
<td>
<?php echo $arr["nume"];?>
</td>
<td>
<?php echo $arr["email"];?>
</td>
...............
</tr>
<?php }?>
</tbody>
I'm trying to catch the last loop in my CodeIgniter for-loop. I'm trying to generate a < hr /> under every news item but the last one. This is my code:
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><hr /></td>
</tr>
<?php endforeach; ?>
</table>
Уou can try something like this:
<?php for($i = 0, $lastIDX = count($news)-1; $i<=$lastIDX; $i++): ?>
<!-- html code ... $news_item is $news[$i] now -->
<? if ($i !== $lastIDX) : ?>
<tr>
<td><hr /></td>
</tr>
<?php endif; ?>
<?php endfor; ?>
But it would be better to use CSS :last-child selector.
You can use this one as a solution:
$last = end($news);
<table class="news">
<?php foreach($news as $news_item): ?>
<tr>
<td class="headline"><?php echo $news_item['title']; ?></td>
</tr>
<tr>
<td class="text"><?php echo $news_item['text']; ?></td>
</tr>
<tr>
<td><?=(($last == $news_item)?"":"<hr />")?></td>
</tr>
<?php endforeach; ?>
</table>
Where the $last is the last array of your array XD.
Get the last key of the array beforehand and compare it the current one while looping:
$keys = array_keys($news);
$last_key = end($keys);
foreach ($news as $news_key => $news_item) {
if ($news_key == $last_key) {
// at the last item
} else {
// not at the last item
}
}
Also you could count the elements in your array beforehand, and have a counter inside the loop that increments on every round, so you can tell if you are at the end or not.