计算三的倍数并使用替代内容填充空插槽

时间:2017-04-18 16:12:07

标签: php arrays grouping

我在网站上显示电视节目列表,每行有三列。当我没有足够多的三个节目时,我想用空格填充空插槽。

我用来显示列表的代码是:

<?php $counter = 1; ?>
<?php foreach ($show_listingsRecords as $record): ?>
    <?php if($counter % 3 == 0) : ?>
        <!-- ROW 3 -->
        <div> ... display name + description + link ...</div>
    <?php elseif ($counter % 3 == 2) : ?>
        <!-- ROW 2 -->  
        <div> ... display name + description + link ...</div>
    <?php elseif  ($counter % 3 == 1): ?>
        <!-- ROW 1 -->  
        <div> ... display name + description + link ...</div>
    <?php endif; ?>       
    <?php $counter++; ?>
<?php endforeach ?>

我试图插入以下条件语句,但它不起作用,因为foreach()循环决定了要显示的记录数。如果插槽2或2和2,我如何实现显示替代内容? 3是空的?

<?php if(!$show_listingsRecords): ?>
    Display my alternate content
<?php endif ?>

2 个答案:

答案 0 :(得分:0)

我建议先进行所有处理,然后只使用foreach()进行显示。

第一步是检查准备$show_listingsRecords数组需要哪种修改。

  • 如果没有子阵列,那么您将需要使用所有三个$alternates子阵列。
  • 如果子数组的数量不均匀地除以$chunk_size(每批子数组),则只合并$alternates中必要数量的子数组以完成“批处理”。

代码:(Demo

$show_listingsRecords=array(
    array("name"=>1,"description"=>2,"link"=>3),
    array("name"=>4,"description"=>5,"link"=>6),
    array("name"=>7,"description"=>8,"link"=>9),
    array("name"=>10,"description"=>11,"link"=>12),
    array("name"=>13,"description"=>14,"link"=>15)
);

$alternates=array(
    array("name"=>"backup1A","description"=>"backup1B","link"=>"backup1C"),
    array("name"=>"backup2A","description"=>"backup2B","link"=>"backup2C"),
    array("name"=>"backup3A","description"=>"backup3B","link"=>"backup3C")  // needed if no listings at all
);

$chunk_size=3;  // 3 is used in this case

// conditionally prepare array
if(!$size=sizeof($show_listingsRecords)){  // if no listings at all (empty array)
    $show_listingsRecords=$alternates;
}elseif($over=$size%$chunk_size){  // $over is how many extra elements beyond "even" (0,1,2 in this case)
    $show_listingsRecords=array_merge($show_listingsRecords,array_slice($alternates,0,$chunk_size-$over));
}

// display
foreach(array_chunk($show_listingsRecords,$chunk_size) as $batch){  // iterate in batches
    echo "<div>\n";
        foreach($batch as $listing){  // iterate the arrays in the batch
            echo "\t<div>",implode(' + ',$listing),"</div>\n";
        }
    echo "</div>\n";
}

输出:

<div>
    <div>1 + 2 + 3</div>
    <div>4 + 5 + 6</div>
    <div>7 + 8 + 9</div>
</div>
<div>
    <div>10 + 11 + 12</div>
    <div>13 + 14 + 15</div>
    <div>backup1A + backup1B + backup1C</div>
</div>

答案 1 :(得分:-1)

您可以在代码后添加另一个循环:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>testApp</display-name>

    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
    </servlet>

    <listener>
        <listener-class>
            or.tc.pack.context.listener.TestWebContextListener
        </listener-class>
    </listener>

    <!-- The master configuration file for this Spring web application -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/context/testway/webApplication-config-local.xml
        </param-value>
    </context-param>

    <!-- Loads the Spring web application context -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/services/eng/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/services/fra/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>480</session-timeout>
    </session-config>

    <resource-ref id="ResourceRef_1288099140976">
        <description>
        Business Update queue</description>
        <res-ref-name>jms/ZZZZ_ZZ_ZZZZ_BUS_UPD</res-ref-name>
        <res-type>javax.jms.Queue</res-type>
        <res-auth>Application</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

    <resource-ref>
        <description>
        </description>
        <res-ref-name>java:comp/env/tm/TimerManager</res-ref-name>
        <res-type>commonj.timers.TimerManager</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Unshareable</res-sharing-scope>
    </resource-ref>

</web-app>

A还建议您使用常量/变量而不是幻数3.如果您决定将来使用4列,您需要做的就是更改变量并为第四列添加条件,而不是手动检查和重写“正确”数字3(查找和替换可能会更改您不想更改的代码的其他部分中的数字3。)