简体   繁体   English

PHP函数创建div,将被调用x次

[英]PHP Function to create div which will be called x amount of times

I am attempting to make an html element that contains x amount of other div s created by a function where x is dependent on the number of elements $allposts contains. 我正在尝试制作一个HTML元素,其中包含x由函数创建的其他div数量,其中x取决于$allposts包含的元素数量。

<div id="postings">
    <?php
    foreach($allposts as $key => $value){
        createPost($key, $value);
    }
    ?>
 </div>

This html and php code above is my attempt to create a "posting" for each item that is found in the $allposts array. 上面的html和php代码是我为$allposts数组中找到的每个项目创建“发布”的尝试。

<?php
    function createPost($author, $input)
    {
        $data = [];
        $i = 0;
        foreach($input as $key => $value){
            $data[$i] = $value;
            $i++;
        }
        echo "<div>
                <span class='post-title'>$data[0]</span>
                <span class='post-author'>$author</span>
                <span class='post-date'>$data[1]</span>
                <p class='post-desc'>$data[2]</span>
            </div>";
     }
?>

This is the php document that contains the function createPost($author, $input) . 这是包含功能createPost($author, $input)的php文档。

The function works but it seems that each time the function is being called, the previous post is overridden leaving only one post visible which ends up being the last one in the array. 该函数有效,但似乎每次调用该函数时,前一个帖子都会被覆盖,仅留下一个可见的帖子,最终成为数组中的最后一个帖子。

<p class='post-desc'>$data[2]</span>;

does not have the correct closing tag, maybe it is interfering with the formatting resulting in missing data upon rendering to the browser. 没有正确的结束标记,可能是干扰了格式设置,导致在呈现给浏览器时丢失了数据。 Do you have a sample of the html source it puts out? 您是否有其发布的html源示例? Indentation really helps with readability. 缩进确实有助于提高可读性。

<?php

function createPost($author, $input)
{
    $data = [];
    $i = 0;

    foreach($input as $key => $value) {
        $data[$i] = $value;
        $i++;
    }

    echo "<div>
            <span class='post-title'>$data[0]</span>
            <span class='post-author'>$author</span>
            <span class='post-date'>$data[1]</span>
            <p class='post-desc'>$data[2]</p>
          </div>";
}

?>

Get the $data array declaration outside of foreach and the other change needs to be done is, do not use $i as key of $data array because everytime, it will be declared with 0 (zero) and so, all new values replace the previos one. foreach之外获取$data数组声明,另一个需要做的更改是,不要将$ i用作$ data数组的键,因为每次它将用0(零)声明,因此,所有新值都将替换previos之一。 So instead of using $i keep it $data[] so it will automatically add numeric indexes. 因此,不要使用$i保留它$data[]这样它将自动添加数字索引。

<?php
    $data = [];    // I took it out of the loop.
    function createPost($author, $input)
    {
        foreach($input as $key => $value){
        $data[] = $value;
    }
    echo "<div>
                <span class='post-title'>$data[0]</span>
                <span class='post-author'>$author</span>
                <span class='post-date'>$data[1]</span>
                <p class='post-desc'>$data[2]</span>
           </div>";
     }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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