整理我的php循环

时间:2017-12-11 12:27:57

标签: php

我创建了一个foreach循环,它似乎正在做两个不同的循环,这会弄乱输出。

如何将它们组合在一起,以便它们在同一个if语句中并且只为所有这些语句打印bootstrap div结束标记?

https://webdesignbelfast.com/psychic/

<div class="container home-section padding-top">
    <div class="row">
        <div class="col-sm-12">
            <h2>Available Readers Online Now!</h2>
        </div>
    </div>
    <div class="row text-center">
        <?php

        $psychiclist = simplexml_load_file('https://www.digitalselect-uk.com/psychic/xml/profiles');

        foreach ($psychiclist as $psychicinfo):
            $picture = $psychicinfo->picture;
            $status = $psychicinfo->status;
            $name = $psychicinfo->name;
            $pin = $psychicinfo->pin;

            if ($status == "Available") {
                $available = $status;
                echo "
        <div class='col-sm-2 col-xs-4'>
            <img height='100%' width='100%' src='", $picture, "'>
            <p class='status available'>", $available, "</p>
            <p class='name'>", $name, "</p>
            <p class='pin'>Pin: ", $pin, "</p>
        </div>
        ";
            }

            if ($status == "Available") {
                $counter++;
                if ($counter % 6 == 0) {
                    echo '</div><div class="row text-center">';
                }
            }

            if ($status == "Busy") {
                $busy = $status;
                echo "
        <div class='col-sm-2 col-xs-4'>
            <img height='100%' width='100%' src='", $picture, "'>
            <p class='status busy'>", $busy, "</p>
            <p class='name'>", $name, "</p>
            <p class='pin'>Pin: ", $pin, "</p>
        </div>
        ";
            }

            if ($status == "busy") {
                $counter++;
                if ($counter % 6 == 0) {
                    echo '</div><div class="row text-center">';
                }
            }
        endforeach;
        ?>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

因此,这应该为您提供所需的结果。

首先,唯一的区别是读者的状态,并且你正在做一个不同的if语句,因为它们很忙,因为这会影响css类。

你可以通过为css类创建一个变量(基本上是小写的状态)

来防止这种情况

他们还为行实现了逻辑。你不需要这样做。 bootstrap(你正在使用的css库)会响应地为你做这件事。如果你强行在它之间行,它不能使它具有良好的响应性,这将作为强硬的换行符 Bootstrap会在需要时自动换行到下一行。 col-xs上每3个图像,col-md上每6个图像与当前列宽分配。

此外,为了使代码更易于管理,并在编辑器中启用html突出显示,您只需添加'stop php'标记?>即可输出html,然后使用短回显标记只是回显变量的值 <?= $somevariable ?>

通过此设置,您可以使用基本的MVC(模型视图控制器)设置。模型是simplexml对象,控制器是循环,其中变量被分配,解析和正确,以及视图,html代码与php短回声。

实现我们最终的所有目标:

<div class="container home-section padding-top">
    <div class="row">
        <div class="col-sm-12">
            <h2>Available Readers Online Now!</h2>
        </div>
    </div>
    <div class="row text-center">
        <?php

        $psychiclist = simplexml_load_file('https://www.digitalselect-uk.com/psychic/xml/profiles');

        foreach ($psychiclist as $info):

            $picture = $info->picture;
            $status = $info->status;
            $name = $info->name;
            $pin = $info->pin;
            $cssclass = strtolower($status);
            ?>
            <div class="col-sm-2 col-xs-4">
                <img height="100%" width="100%" src="<?= $picture ?>">
                <p class="status <?= $cssclass ?>"><?= $available ?></p>
                <p class="name"><?= $name ?></p>
                <p class="pin">Pin: <?= $pin ?></p>
            </div>
        <?php endforeach;?>
    </div>
</div>