SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having Array Issues

    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; ?>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Hi,
    You would need to set up a simple counter that increased each time the loop was run.
    As a basic example:

    PHP Code:
    $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;

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You might want to re-check that code @spikeZ ; , as it stands it will output "Sbv"

    Spike ebiwari dave

    PHP Code:
    $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 Code:
    <?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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot u are a genius.. It does the job
    Quote Originally Posted by ScallioXTX View Post
    You might want to re-check that code @spikeZ ; , as it stands it will output "Sbv"

    Spike ebiwari dave

    PHP Code:
    $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 Code:
    <?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.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Good spot Remon, I was thinking of a different loop
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •