split array value in table by php - 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

Related

print invoice data blank php foreach loop error

I want to print invoice data. but some error only one image print. how to print all data in an invoice I am using PHP 7.2 and loop foreach please help me...
error->Warning: Invalid argument supplied for foreach() in D:\xammp\htdocs\tam\admin\invoice.php on line 133
PHP GET id wise invoice data script
<?php
require 'setting/config.php';
$id=$_GET['id'];
$query="select * from orders where id='$id'";
$galrun=mysqli_query($conn, $query);
$result=mysqli_fetch_assoc($galrun);
$total=$result['total'];
$array[0]=$result['image'];
$array[1]=$result['productName'];
$array[2]=$result['product_qty'];
$array[3]=$result['salseprice'];
$resultdata= implode(",", $array);
?>
HTML invoice
<table class="table">
<thead>
<tr>
<th>Sr. No.</th>
<th>image</th>
<th>Description</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultdata as $item){
?>
<tr>
<td> ? </td>
<td><img src="image/product/<?php echo $item[0] ; ?>" style="width:100px;height:50px;"></td>
<td><?php echo $item[1] ; ?></td>
<td><?php echo $item[2] ; ?></td>
<td><?php echo $item[3] ; ?></td>
<td><?php echo $total ; ?</td>
</tr>
<?php }?>
</tbody>
</table>
You try to loop through $resultdata, but it is a string, because of this part:
$resultdata= implode(",", $array);
PHP implode() returns a string.

Foreach returns a column and I have to turn it into a table

I have the following JSON table
[
["TITLE", "CONTENT", "DATE"],
["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]
I use a foreach to get the json contents
$days = json_decode(file_get_contents('json_file'));
foreach($days as $item){
if(isset($item)){
$firstColumn = $item;
}
echo $firstColumn[0];
};
$firstColumn[0] returns "Title", "Monday", "Friday"
How can I add it in a table?
<table class="table">
<thead>
<tr>
<th> Title </th>
<th>Content</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Use the below to render the table from any given json obj
<?php
if (sizeof($days) > 0) { //If json is not empty
$firstColumn = $days[0] ; //First Column (<thead> values)
?>
<table>
<thead>
<tr>
<?php foreach ($firstColumn AS $k => $v) { ?>
<th><?php echo $v?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
foreach($days as $k => $v) {
if ($k != 0) { ?>
<tr>
<?php
foreach ($v AS $k1 => $v1) { ?>
<td><?php echo $v1 ?></td>
<?php
} ?>
</tr>
<?php
}
} ?>
</tbody>
</table>
<?php
} ?>
Do it like below:-
<?php
$days = [
["TITLE", "CONTENT", "DATE"],
["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]; // as you said that you got this array already
unset($days[0]);
?>
<table class="table">
<thead>
<tr>
<th> Title </th>
<th>Content</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach($days as $day){?>
<tr>
<td><?php echo $day[0];?></td>
<td><?php echo $day[1];?></td>
<td><?php echo $day[2];?></td>
</tr>
<?php }?>
</tbody>
</table>
Output at my local end:- https://prnt.sc/h8nit6
Regarding your question:- but how can I add it into a return inside a function?. I think you want like below:-
function returnFullHtml(){
$days = json_decode(file_get_contents('json_file'));// now you got the array what you shhown in your question input
unset($days[0]);
$html = '<table class="table">
<thead>
<tr>
<th> Title </th>
<th>Content</th>
<th>Date</th>
</tr>
</thead>
<tbody>';
foreach($days as $day){
$html .= '<tr><td>'. $day[0] .'</td><td>' . $day[1] .'</td><td>' . $day[2] .'</td></tr>';
}
$html .= '</tbody></table>';
return $html;
}
You can try it like this
echo '<table class="table">';
$items = json_decode(file_get_contents('json_file'));
$headers = array_shift($items);
echo '<thead>';
foreach($headers as $value){
echo '<th>'.$value.'</th>';
}
echo '</thead>';
echo '<tbody>';
foreach($items as $item){
echo '<tr>';
foreach($item as $value){
echo '<td>'.$value.'</td>';
}
echo '</tr>';
};
echo '</tbody>';
echo '</table>';

Repeat Foreach limit

I have this code
<?php foreach ($column as $k => $v): ?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>
And i get this output
<tr>
<td>id</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>address</td>
</tr>
<tr>
<td>birth</td>
</tr>
<tr>
<td>foto</td>
</tr>
but i don't want to include the foto, how can i do this ?
you can use if condition inside foreach
<?php foreach ($column as $k => $v):
if($v=='foto'){ // skip iteration if value is `foto`
continue;
}
?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>
You can do the following before starting the loop-
If foto is the key -
if(isset($column['foto']))
unset($column['foto']);
If foto is value -
if(in_array('foto', $column))
unset($column[array_search('foto', $column)]);
After that looping starts -
<?php foreach ($column as $k => $v): ?>
<tr>
<td><?php echo $v; ?></td>
<td><?php echo $k; ?></td>
</tr>
<?php endforeach ?>

Two loops in one table

I need to insert two loops in one table, but I have a problem.
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
}
?>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
</table>
But the result is:
http://prntscr.com/6m9v25
One name is in the wrong position.
Just put while loop into for loop.
// Code goes here
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
<?php
}
?>
</table>
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
$x = 1;
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<tr>
<td><?php echo $x ?></td>
<td><?php echo $name ?></td>
</tr>
<?php
$x++
}
?>
</table>
Try This Code!! (Edited Again!)

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

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>

Categories