简体   繁体   中英

How can I make a dynamic loop?

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?

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM