Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示3个博客条目,然后显示3张照片,然后显示3个博客条目,最后显示3张照片_Php_While Loop - Fatal编程技术网

Php 显示3个博客条目,然后显示3张照片,然后显示3个博客条目,最后显示3张照片

Php 显示3个博客条目,然后显示3张照片,然后显示3个博客条目,最后显示3张照片,php,while-loop,Php,While Loop,对PHP来说很陌生,我正在尝试让我的页面显示3个博客条目,然后是3张照片,然后是另外3个博客条目,等等 有人建议我使用do-while循环,但我很难让它工作,甚至我的大脑也无法控制它,我更习惯于在CMS中使用foreach循环 这是我的原始代码,只有在我手动显式添加每个循环时才能工作 <?php // Show 3 blog entries $entries = $page->children("sort=-sort, limit=3"); $count = 0; fore

对PHP来说很陌生,我正在尝试让我的页面显示3个博客条目,然后是3张照片,然后是另外3个博客条目,等等

有人建议我使用do-while循环,但我很难让它工作,甚至我的大脑也无法控制它,我更习惯于在CMS中使用foreach循环

这是我的原始代码,只有在我手动显式添加每个循环时才能工作

   <?php  // Show 3 blog entries
$entries = $page->children("sort=-sort, limit=3");
$count = 0;

foreach ($entries as $entry) { 

$count++; 
$class = "blog_box"; 
if ($entry == $entries->last()) {$class .= " blog_box_last"; }
if ($entry == $entries->first()) {$class .= " blog_box_first"; }
if (0 == $count % 2) { $class .= " blog_box_even"; }
?>

<div class="<?php echo $class; ?>">

<div class="blog_text">

<h3><?php echo $entry->title; ?></h3>
<h6><?php echo $entry->entry_date; ?></h6>
<p><?php echo $entry->body; ?></p>

</div><!-- /.blog_text -->

<?php if ($entry->image) { 

$image = $entry->image->width(350);

?>

<img src="<?php echo $image->url; ?>" width="<?php echo $image->width; ?>" alt="<?php echo $entry->title; ?>" />

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /.blog_box -->

<?php }

?>

// Show 3 blog images

<?php $blog_images = $page->get("image_uploads")->find("limit=3");
foreach ($blog_images as $img) { 
$b_img = $img->size(200,140); ?>
<img src="<?php echo $b_img->url; ?>" width="<?php echo $b_img->width; ?>" height="<?php echo $b_img->height; ?>" alt="<?php echo $b_img->description; ?>" class="small_frame" />
<?php } ?>


让我们看看你的代码不好用哪种CMS?任何代码都有助于诊断。你的问题非常模糊。哇,太快了,只是添加我的代码给你一个更好的想法,谢谢。上传了我的代码,有什么想法吗?干杯
    $entries = $page->children("sort=-sort");
$images = $page->get("/image_uploads/"); 
$cnt = 0;

do {
    $cnt++; 
    $entry = $entries->shift();
    if($entry) {
        // output 1 entry
    }

    if($cnt == 3) {
        while(++$cnt <= 6) {
            $image = $images->shift();
            if($image) {
                // output 1 image
            }
        }
        $cnt = 0;
    }

} while(count($entries) && count($images));