0

I'm able to display id and tags in the foreach loop no problem. But I'm having trouble showing title and height. I'm not sure how to call them into the foreach loop.

I can call them in the while loop so I know that they are working.

$persons = array();
$tags = array();

while( ($row = mysqli_fetch_array( $rs, MYSQLI_ASSOC)))
{
    if(!isset( $persons[$row['id']]))
    {
        $persons[$row['id']]['title'] = $row['title'];
        $persons[$row['id']]['height'] = $row['height'];
        $persons[ $row['id'] ] = array( 'id' => $row['id'], 'tag' => $row['tag']);
        $tags[ $row['id'] ] = array();
    }
    $tags[ $row['id'] ][] = $row['tag'];
}

foreach( $persons as $pid => $p)
{
    echo 'id: # ' . $p['id'] ;
    echo 'title: ' . $p['title'];
    echo 'height: ' . $p['height'];
    echo '<a href="#">' . implode( '</a>, <a href="#">', $tags[ $p['id'] ])  . '</a>';
    echo '<br /><br />';
}

1 Answer 1

2

You're overwriting $persons[ $row['id'] ] when you set the tags, so you lost the other data.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.