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
</div>
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>";
}
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>
<td>";
Echo $parts[0];
Echo "</td>
<td>";
Echo $parts[1];
Echo "</td>
</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:
For an example, see: