PHP虽然在Wordpress中控制结构

时间:2012-04-10 23:50:03

标签: php wordpress

我一直在努力使用Word中的PHP While控制结构。我当然知道这是可能的,只是不知道该怎么做。

我正在使用Wordpress数据库存储我的窗口小部件控件表单字段数据,我想用这段代码循环遍历数据。

need to control structure by the 2 numbers defined in my widget control.
$data['houses_row']
$data['shown_rows'] 

这是我正在尝试做的事情:

我在考虑房屋,每个人都在一片土地上作为类比。排序。

每排有多少房子?和要显示多少行?

如果排的房屋数量超过了行可以处理的数量,则需要隐藏这些房屋,这可以自然地完成。我在想很多房子到很多行。

我希望有人能帮我解决问题。

    static $number_posts_shown = 1;
    static $per_row_count = 0;

    while ( have_posts() ) : the_post();        

        if ( $per_row_count <= $data['houses_row'] ): 
        echo "show house: #". $per_row_count ." ";                  

                   endif;
        $per_row_count++;

        // show rows            
        if ( $number_posts_shown <= $data['shown_rows'] ):           
            echo "<hr />";
        endif;
        $number_posts_shown++;                  

    endwhile;

当shown_rows为4且houses_row(每行房屋数)为4时,我希望输出为此。

show house: #0 show house: #1 show house: #2 show house: #3 
--------------------------------------------------------------------------------
blank
--------------------------------------------------------------------------------
blank
--------------------------------------------------------------------------------
blank

当shown_rows为4且houses_row(每行房屋数)为2时,输出为此。

show house: #0 show house: #1 
--------------------------------------------------------------------------------
show house: #2 show house: #3 
--------------------------------------------------------------------------------
blank row
--------------------------------------------------------------------------------
blank row

1 个答案:

答案 0 :(得分:0)

根据您的问题,我认为您希望根据以下格式显示房屋(存储在wordpress帖子中):

$data['houses_row']
$data['shown_rows']

最简单的解决方案是先存储临时数组中已有的内容:

$temporary_data = array();
while ( have_posts() ) : the_post();

    $temporary_data[] = $post;//retrieve current post object

endwhile;

获得所有发布数据后,开始第二次循环以生成它:

$current_house_counter = 0;
$available_slot = 0;
for($shown_row = 0; $shown_row < $data['shown_rows']; $shown_row++)
{
    for($house_row = 0; $house_row < $data['houses_row']; $house_row++)
    {
        if( isset($temporary_data[$current_house_counter]) )
        {
            echo "show house: #". ($current_house_counter + 1) ." ";
        }
        else
        {
            $available_slot++;
            echo "available slot #" . $available_slot . " ";
        }
        $current_house_counter++;
    }
    echo "<hr />";
}

因为我目前没有wordpress安装,所以我会给你这个代码片段来测试它:

$temporary_data[] = "data #1";
$temporary_data[] = "data #2";
$temporary_data[] = "data #3";
$temporary_data[] = "data #4";
$temporary_data[] = "data #5";

$data['houses_row'] = 3;
$data['shown_rows'] = 3;

$current_house_counter = 0;
$available_slot = 0;
for($shown_row = 0; $shown_row < $data['shown_rows']; $shown_row++)
{
    for($house_row = 0; $house_row < $data['houses_row']; $house_row++)
    {
        if( isset($temporary_data[$current_house_counter]) )
        {
            echo "show house: #". ($current_house_counter + 1) ." ";
        }
        else
        {
            $available_slot++;
            echo "available slot #" . $available_slot . " ";
        }
        $current_house_counter++;
    }
    echo "<hr />";
}

上面的代码有以下结果:

show house: #1     show house: #2     show house: #3 <hr />
show house: #4     show house: #5     available slot #1 <hr />
available slot #2  available slot #3  available slot #4 <hr />