2
$rows = getChallengeList();
error_log(print_r($rows, 1));
?>
<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <?php foreach ($rows as $row): ?>
        <?php foreach($row as $chal): ?>
        <tr>
            <td><?= $chal['loyalty_challenges_id']; ?></td>
            <td><?= $chal['title']; ?></td>
            <td><?= $chal['description']; ?></td>
        </tr>    
        <?php endforeach; ?>
    <?php endforeach; ?>
</table>

The error_log returns this:

[11-Jan-2013 10:44:27] Array
(
    [0] => Array
        (
            [loyalty_challenges_id] => 1
            [title] => New Customer Special
            [description] => Reward new customers with a free order of breadsticks after placing their second order during their first 30 days 
        )

    [1] => Array
        (
            [loyalty_challenges_id] => 2
            [title] => Frequent Flyer Special
            [description] => Reward long-time customers who order often with a free pizza
        )

)

But the values rendered by the loop look like this:

<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
    </tr>    
    <tr>
        <td>N</td>
        <td>N</td>
        <td>N</td>
    </tr>    
    <tr>
        <td>R</td>
        <td>R</td>
        <td>R</td>
    </tr>    
    <tr>
        <td>2</td>
        <td>2</td>
        <td>2</td>
    </tr>    
    <tr>
        <td>F</td>
        <td>F</td>
        <td>F</td>
    </tr>    
    <tr>
        <td>R</td>
        <td>R</td>
        <td>R</td>
    </tr>    
</table>

Any idea what would cause that? It's been a while since I've worked with straight php, rather than CMS's that have their own array processing functions.

6
  • 2
    Please stop using short tags. Commented Jan 11, 2013 at 16:51
  • 3
    You need just one foreach. Commented Jan 11, 2013 at 16:51
  • @njk - I was taught that it was preferable to do that if you were going to have a lot of HTML with php values interspersed than to echo out hundreds of lines of html just so you can stay inside a single php tag. Commented Jan 11, 2013 at 16:59
  • @EmmyS Check this out: Are PHP short tags acceptable to use? Commented Jan 11, 2013 at 17:02
  • 1
    @Asok - according to that answer, "As ThiefMaster mentions in the comments, as of PHP 5.4, <?= ... ?> tags are supported everywhere, regardless of shorttags settings. This should mean they're safe to use in portable code but that does mean there's then a dependency on PHP 5.4+". Not an issue - we own all our servers, so we have complete control over them. Commented Jan 11, 2013 at 17:13

4 Answers 4

5

You have one loop too much:

$rows = getChallengeList();
error_log(print_r($rows, 1));
?>
<table border="1">
    <tr><th>ID</th><th>Title</th><th>Description</th></tr>
    <?php foreach ($rows as $chal): ?>
        <tr>
            <td><?= $chal['loyalty_challenges_id']; ?></td>
            <td><?= $chal['title']; ?></td>
            <td><?= $chal['description']; ?></td>
        </tr>    
    <?php endforeach; ?>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Explanation: when using $chal['loyalty_challenges_id']; against the $chal string value, the 'loyalty_challenges_id' value is cast to a numeric, giving 0, so it displays the character at offset 0 of the string, the first character in the string
Thanks. I could swear I already tried that and got empty strings back, but I guess not.
1

You need just one foreach:

<?php foreach ($rows as $row): ?>
    <tr>
        <td><?php echo $row['loyalty_challenges_id']; ?></td>
        <td><?php echo $row['title']; ?></td>
        <td><?php echo $row['description']; ?></td>
    </tr>    
<?php endforeach; ?>

What's happening ?

When you loop thrue $chal you loop thrue each key, and when you try to access $chal['loyalty_challenges_id'];

is the same of

$rows[0]['loyalty_challenges_id']['loyalty_challenges_id']

which is translated to

$rows[0]['loyalty_challenges_id'][0]

That's why you get the first letter on each row.

Comments

0

Try :

<?php foreach ($rows as $row): ?>
    <tr>
        <td><?php echo $row['loyalty_challenges_id']; ?></td>
        <td><?php echo $row['title']; ?></td>
        <td><?php echo $row['description']; ?></td>
    </tr>    
<?php endforeach; ?>

Comments

0
    <?php foreach($row as $chal): ?>
    <tr>
        <td><?= $chal['loyalty_challenges_id']; ?></td>
        <td><?= $chal['title']; ?></td>
        <td><?= $chal['description']; ?></td>
    </tr>    
    <?php endforeach; ?>

This foreach is unneeded. Remove it, and use $row['loyalty_challenges_id'] etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.