how can i echo contents of 2 associative arrays into one table? - php

this is my code snippets and the result is not the thing that i want.I have 2 associative arrays and want to echo their contents into one table like before the html tag.how can i do that?
team A: team B:
player_one player_five
player_two player_six
player_three player_seven
player_four player_eight
<html>
<head>
<title>Untitled</title>
</head>
<body>
<table widht='100%' border="1">
<tr>
<td>team A</td>
<td>team B</td>
</tr>
<?php
$team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
$team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");
foreach($team_a as $index1 => $value1 ){
foreach($team_b as $index2 => $value2 )
echo "
<tr>
<td>$value1</td>
<td>$value2</td>
</tr>
";
}
?>
</table>
</body>
</html>

// drop indexes because you don't use them anyway
$a = array_values($team_a);
$b = array_values($team_b);
// process arrays
max = max(count($a), count($b));
for ($i=0; $i < $max; $i++) {
echo '
<tr>
<td>' . (isset($a[$i] ? $a[$i] : '-')) . '</td>
<td>' . (isset($b[$i] ? $b[$i] : '-')) . '</td>
</tr>
';
}

Alternatively, you could combine the arrays first and line them up accordingly, them print them:
<?php
$team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
$team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");
$teams = array();
$keys = array_keys($team_a);
foreach ($keys as $key) {
$teams[$key] = array($team_a[$key], $team_b[$key]);
}
?>
<table widht='100%' border="1">
<tr>
<td>team A</td>
<td>team B</td>
</tr>
<?php foreach($teams as $players): ?>
<tr>
<?php foreach($players as $player): ?><td><?php echo $player; ?></td><?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Output
Or if you do not want the original arrays touched or using a new array, then just loop them accordingly:
<?php
$team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
$team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");
?>
<table widht='100%' border="1">
<tr>
<td>team A</td>
<td>team B</td>
</tr>
<?php foreach($team_a as $key => $value): ?>
<tr>
<td><?php echo $value; ?></td><td><?php echo $team_b[$key]; ?></td>
</tr>
<?php endforeach; ?>
</table>

Related

How to Create Table on Html with PHP data Looping

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>

Showing data in table row wise by using codeigniter php

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>

split array value in table by php

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

Print nested associative array keys as row numbers

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>

Last loop in CodeIgniter foreach

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.

Categories