PHP While control structure in WordPress

I’ve been struggling with the PHP While control structure in WordPress. I definitely know this is possible, just don’t know exactly how to do it.

I’m using the WordPress database to store my widget controls form fields data, and I want to loop through the data with this code.

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

Here’s what I’m trying to do:

I’m thinking of Houses and each one is on a plot of land as an analogy. Sort of.

How many houses to show per row? and How many rows to show?

If the row has more houses than the row can handle then those houses need to be hidden, which this can do naturally. I am thinking many houses to many rows.

I hope somebody can help me with my loop.

    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;

when shown_rows is 4 and houses_row(houses per row) is 4 I wanted the output to be this.

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

when shown_rows is 4 and houses_row(houses per row) is 2 the output is this.

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

Related posts

Leave a Reply

1 comment

  1. from your question, I get the idea that you want to display houses (stored in wordpress post) based on the format of:

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

    The simplest solution is to store what you already have in temporary array first:

    $temporary_data = array();
    while ( have_posts() ) : the_post();
    
        $temporary_data[] = $post;//retrieve current post object
    
    endwhile;
    

    After you have all post data, begin the second loop to generate it:

    $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 />";
    }
    

    Because I don’t have a wordpress installation at the moment, I’ll give you this code snippet to test it out:

    $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 />";
    }
    

    The code above has the following result:

    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 />