Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7767853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:46:36+00:00 2026-06-01T15:46:36+00:00

I’ve been struggling with the PHP While control structure in WordPress. I definitely know

  • 0

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.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:46:37+00:00Added an answer on June 1, 2026 at 3:46 pm

    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 />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.