Reputation: 33
My php code throws values but mixes up both arrays data into one another. how to fix the code to sort it out! thanks in advance!
here $Array1=Array ( [0] => Array ( [0] => Abdul Razak, Ahmed Deedat [name] => Abdul Razak, Ahmed Deedat [1] => 11 [standard] => 11 [2] => 12/08/16 [dated] => 12/08/16)) $Array2=Array ( [0] => Array ( [0] => lali, raj [name] => lalit, raj [1] => 12 [standard] => 12 [2] => 12/08/16 [dated] => 12/08/16))
the above arrays are just incomplete samples, rows are over 10 I want the output like this:
left wing | right wing
array 1, key [0] values | array 2 [0] key values
array1, key [1] values | array 2 [1] key values
and so on till both arrays doesn't end
code looks like this:
<?php
<table id="fire">
<caption><?php echo "Achievements by the selected two are:";?> </caption>
<tr>
<th>Wing 1</th>
<th>Wing 2</th>
</tr>
<tr>
<?php
if(isset($raws1)) {
foreach($raws1 as $raw1){ ?>
<tr>
<?php
if($raw1==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw1['name'] . ", " . $raw1['standard'] .", on ".$raw1['dated']." , ".$raw1['achieved'].", at ".$raw1['at']. ", ".$raw1['on'].", (". $raw1['position'] ."</td>";
}
if(isset($raws2)) {
foreach($raws2 as $raw2){ ?>
<tr>
<?php
if($raw2==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw2['name'] . ", " . $raw2['standard'] .", on ".$raw2['dated']." , ".$raw2['achieved'].", at ".$raw2['at']. ", ".$raw2['on'].", (". $raw2['position'] ."</td></tr>";
} }}}}?>
</table>
?>
Upvotes: 0
Views: 131
Reputation: 41810
Outputting the beginning part of your table with headers looks like it's already fine.
<table id="fire">
<caption><?php echo "Achievements by the selected two are:";?> </caption>
<tr>
<th>Wing 1</th>
<th>Wing 2</th>
</tr>
<?php
Here's one way to do the PHP part. Put both of your arrays into one array. This will help to prevent repeated code later because you can foreach
over the outer array to do the same things to both inner arrays.
$both = [$raw1, $raw2];
Get the maximum count of the two arrays.
$count = max(array_map('count', $both));
Then you can use a for
loop to loop up to that maximum count.
for ($i=0; $i < $count; $i++) {
echo '<tr>';
// This foreach loop will loop over the two arrays ($raw1 and $raw2)
foreach ($both as $array) {
// check the $i element of both the arrays and output its value if it's set
if (empty($array[$i])) {
echo "<td>---</td>";
} else {
$x = $array[$i];
echo "<td>$x[name], $x[standard], etc. ...";
}
}
echo '</tr>';
} ?>
</table>
Upvotes: 1
Reputation: 1277
if your keys are the same just do a foreach loop on all keys to get data that you need.
foreach($array1 as $key=>$value)
{
echo "<td>{$array1[$key]}</td><td>{$array2[$key]}</td>"
}
Upvotes: 0
Reputation: 1061
I only created two new table inside, It simplified stuff alot.
It is still posible to do it in only one table. But I don't think its a requirement in your case.
Try that.
<table id="fire">
<caption><?php echo "Achievements by the selected two are:";?> </caption>
<tr>
<th>Students 1</th>
<th>student 2</th>
</tr>
<tr>
<td>
<?php
if(isset($raws1)) {
?><table><?php
foreach($raws1 as $raw1)
{ ?>
<tr>
<?php
if($raw1==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw1['name'] . ", " . $raw1['standard'] .", on ".$raw1['dated']." , ".$raw1['achieved'].", at ".$raw1['at']. ", ".$raw1['on'].", (". $raw1['position'] ."</td>";
}
?></tr><?php
}
?></table><?php
}
?></td><td><?php
if(isset($raws2)) {
?>
<table>
<?php
foreach($raws2 as $raw2){
?>
<tr>
<?php
if($raw2==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw2['name'] . ", " . $raw2['standard'] .", on ".$raw2['dated']." , ".$raw2['achieved'].", at ".$raw2['at']. ", ".$raw2['on'].", (". $raw2['position'] ."</td></tr>";
}
?></tr><?php
}
?></table><?php
}?>
</td>
</table>
Upvotes: 0