please can any help me with this code:
I have two array having different data and i want to output them in a table row.
first array $age = array(‘date’ => ‘date’ …);
second array $name = {‘ebiwari’,‘douebo’…}
how do i output this for name to always take is column in the table:
This what i did. but the name remain the same…
<?php foreach($age as $age): ?>
<tr>
<?php echo "<td>".$name[0]."</td>"; ?>
<td><?php echo "NID".$age['userId']."2012"; ?></td>
<td><?php echo $age['placeOfBirth']; ?></td>
<td><?php echo $age['date']; ?></td>
<td><?php echo $age['sex']; ?></td>
<td><?php echo $age['state']; ?></td>
<td><?php echo $age['lga']; ?></td>
<td><?php echo $age['community']; ?></td>
<td><?php echo $age['villageHead']; ?></td>
<td><?php echo $age['occupation']; ?></td>
<td><?php echo $age['fatherName']; ?></td>
<td><?php echo $age['age']; ?></td>
<?php
?>
</tr>
<?php endforeach; ?>
SpikeZ
March 16, 2012, 11:42pm
2
Hi,
You would need to set up a simple counter that increased each time the loop was run.
As a basic example:
$i=0;
$name = array("Spike", "ebiwari", "dave");
foreach($name as $data) {
echo $data[$i]; // print out an example data value
$i++; //increment the value of $i;
}
rpkamp
March 16, 2012, 11:51pm
3
You might want to re-check that code @spikeZ ; , as it stands it will output “Sbv”
$names = array("Spike", "ebiwari", "dave");
foreach($names as $name) {
echo $name; // print out an example data value
}
@OP you should probably use something like this
<?php foreach($age as $i => $age): ?>
<tr>
<?php echo "<td>".$name[$i]."</td>"; ?>
(added $i)
Not sure, because I don’t know what $name
is, or what it’s relation to $age
is.
Thanks alot u are a genius… It does the job
rpkamp:
You might want to re-check that code @spikeZ ; , as it stands it will output “Sbv”
$names = array("Spike", "ebiwari", "dave");
foreach($names as $name) {
echo $name; // print out an example data value
}
@OP you should probably use something like this
<?php foreach($age as $i => $age): ?>
<tr>
<?php echo "<td>".$name[$i]."</td>"; ?>
(added $i)
Not sure, because I don’t know what $name
is, or what it’s relation to $age
is.
SpikeZ
March 17, 2012, 12:07am
5
Good spot Remon, I was thinking of a different loop