0

I have this PHP array:

echo "<pre>";
print_r($notifications);

/* output:

Array
(
    [0] => Array
        (
            [score] => 120
            [type] => 5
            [post_id] => 1
            [subject] => a subject
            [range_day] => today
        )

    [1] => Array
        (
            [score] => 6
            [type] => 4
            [post_id] => 2
            [subject] => a subject
            [range_day] => today
        )

    [2] => Array
        (
            [score] => 2
            [type] => 4
            [post_id] => 3
            [subject] => a subject
            [range_day] => yesterday
        )
)
*/

And this is expected output:

<ul>
    <li>
        <p class="subject">a subject<span> | type:5</span></p>
        <div class="score">Your score: 120</div>
        <a href="/1/a-subject"> 
        <span class="date">today</span>
    </li>
    <li>
        <p class="subject">a subject<span> | type:4</span></p>
        <div class="score">Your score: 6</div>
        <a href="/2/a-subject"> 
        <span class="date">today</span>
    </li>
    <li>
        <p class="subject">a subject<span> | type:4</span></p>
        <div class="score">Your score: 2</div>
        <a href="/3/a-subject"> 
        <span class="date">yesterday</span>
    </li>
</ul>

Also here is my current code:

$html = "<ul>";
foreach ( $notification as $item1 ) {

    $html .= "<li>";
    foreach ( $item as $item2 ) {

        // in here I need to put different tags for different $item2. How?
    }
    $html .= "</li>";

}
$html = "</ul>";

As you see, I need to use multiple different tags for each key .. How can I manage that? I mean I need to surround first key into <div> tag, second key into <span> tag and etc .. How can I do that?

4 Answers 4

1

This might help you

<ul>
 <?php 
   foreach ( $notifications as $item ) { ?>
    <li>
     <p class="subject"><?php echo $item['subject']; ?><span> | type:    <?php echo $item['type']; ?></span></p>
    <div class="score">Your score: <?php echo $item['score']; ?></div>
    <a href="/<?php echo $item['post_id']; ?>/<?php echo $item['subject']; ?>"> 
    <span class="date"><?php echo $item['range_day']; ?></span>
</li>
<?php
 }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

The href in this result is incorrect. Note that the OP replaced spaces with - in his href
1

Do the simple following:

$html = "<ul>";
foreach ( $notification as $item1 ) {
foreach ( $item as $item2 ) {
    $html .= "<li>";
    $html .= "<p class=subject> ${item1['subject']} </p>";
    $html .= "<div class=score>Your score: ${item1['score']}</div>"
    ............//others logic
    $html .= "</li>";
}
$html .= "</li>";
$html .= "</ul>";

1 Comment

This answer skips one tricky part of the OP's problem. How to build the href correctly (the OP needs spaces replaced with -). It's also not clear what $item is.
1

Remove one loop and treat element manually :

$html = "<ul>";
foreach ( $notification as $item1 ) {

    $html .= "<li>";
    $html .= "<p class='subject'>".$item1["subject"]."</p>";
    $html .= "<div class='score'>your score ".$item1["score"]."</div>";
// error on the OP post, the a tag on the expected value is not closed
    $html .= "<a href='/".$item1["postid"]."/.".str_replace(" ","-",$item1["subject"])."'>The subject : ".$item1["subject"]."</a>";
    ...
    $html .= "</li>";
}
$html = "</ul>";

1 Comment

This answer skips one tricky part of the OP's problem. How to build the href correctly (the OP needs spaces replaced with -)
0

My preferred approach is to separate the PHP logic from the HTML as much as possible. It makes for much simpler html:

$template = <<< 'TXT'
<li>
   <p class="subject">{{subject}}<span> | type:{{type}}</span></p>
   <div class="score">Your score: {{score}}</div>
   <a href="/{{post_id}}/{{no_space_subject}}"> 
   <span class="date">{{range_day}}</span>
</li>
TXT;
$links = '';

foreach ($notifications as $n){
    $n['no_space_subject'] = str_replace(' ','-',$n['subject']);
    //replace {{VALUE}} with $n[VALUE] in the template
    $links .= preg_replace_callback('/{{([^}]+)}}/',
            function($m) use ($n){return $n[$m[1]];},
            $template
        );
}

Then later in your html, you can just output the results:

<ul><?=$links?></ul>

Live demo

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.