0

Ok, so I've read loads of articles and I think I'm at risk of duplicating, but cant work this one out.

I have an array that is being returned by a PHP function, I've called it getLeague();

The structure of the array is:

body[0]->position

body[0]->teamname

body[0]->points

and obviously the results increment from 0 -16 as that's the amount of teams in my league.

I'm trying to tabulate the array by calling getLeague() and iterating over the returned array to print into a table.

I'm trying at the minute to work out the basic for each loop, after that I'll shoehorn it into a table. Can you help me with the foreach? I've tried:

<table class="table table-striped">

    <thead>
        <tr>
            <th>Position</th>
            <th>Team</th>
            <th>Points</th>
        </tr>
    </thead>

    <tbody> 

        <?php
        $rows = getLeague();
        foreach ($rows as $row):
            ?>
            <tr>
                <td><?= $row->body->position; ?></td>
                <td><?= $row->body->teamname; ?></td>
                <td><?= $row->body->points; ?></td>
            </tr>
        <? endforeach ?>

    </tbody>

</table>

Any help appreciated.

5
  • Did you encounter an error? What didn't work? Commented Jan 1, 2014 at 15:00
  • 1
    You're using object access syntax, but are calling these items an array. Seems like the first is an array. Are the items actually objects or are they arrays? Which it is depends on how you're getting them from the DB Commented Jan 1, 2014 at 15:02
  • 2
    Could you add the output of var_dump($rows); to your question? Commented Jan 1, 2014 at 15:04
  • You haven't shown an array at any point or anything "obvious" or clear. It would be more helpful to show php code for a php question than HTML. Commented Jan 1, 2014 at 15:04
  • with all due respect there's plenty of php in my question, its just that i use it in a html context. Commented Jan 1, 2014 at 16:38

1 Answer 1

3

Without seeing more on that data structure, I can't say for certain, but I think you want:

foreach ($rows->body as $row):

And:

$row->position
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.