www.webdeveloper.com
Results 1 to 8 of 8

Thread: Help w/ Dividing Array Into Columns?

Hybrid View

  1. #1
    Join Date
    Jan 2003
    Posts
    216

    Question Help w/ Dividing Array Into Columns?

    I'm using the following PHP script on an e-commerce site to display retail locations out of a database:
    Code:
    <?
    $state_ids = array_map('break_down_by_id', Db_DbHelper::queryArray('select distinct state_id as id from foxylocator_locations where country_id = ?', array($country->id)));	
    $states = Shop_CountryState::create()->where('id in (?)', array($state_ids))->find_all();
    ?>
    <? foreach($states as $state): ?>
    <h5><?= $state->name ?></h5>
    <? endforeach ?>
    The issue is that I now need to display the locations in four columns displaying from top to bottom in each one in ABC order. But not every state has a location. So the way it's divided up needs to be based on the number of results in the $states array.

    How can I take the array and divide it up into four equal (or close to it) <div> columns?

    Thank you for your help!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    PHP Code:
    $chunkSize ceil(count($states) / 4);
    $chunks array_chunk($states$chunkSize);
    foreach(
    $chunks as $chunk) {
      echo 
    "<div class='column'>\n";
      foreach(
    $chunk as $state) {
        echo 
    "<h5>".$state->name."</h5>\n";
      }
      echo 
    "</div>\n";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2003
    Posts
    216
    Thank you! Yes. Using array_chunk makes sense now. I am getting an error though:

    Array_chunk() expects parameter 1 to be array, object given

    Any ideas? $states is definitely an array. $chunkSize is returning a number. Any suggestions?

    Thank you again for your help.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    I would tend to assume that PHP is not being totally stupid, so first I'd check for is typos and so forth, and make absolutely sure it is, in fact, an array. You can use var_export() as a quick-and-dirty check just before the place where it's breaking:
    PHP Code:
    echo "<pre>".var_export($statestrue)."</pre>\n"
    If it's still unclear after that, then we may need to see some of the current code.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jan 2003
    Posts
    216
    It turns out that $states is actually an object -- not an array. Can this easily be reworked with an object? Or can it be easily converted to an array? Sorry. This is above my PHP knowledge level. Thank you for any help you can provide.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    It's hard to say without knowing the details of the relevant class. If by some chance it already implements the Iterator interface, then you could loop through it via a foreach() to build an array of states.
    PHP Code:
    $stateArray = array();
    foreach(
    $states as $state) {
        
    $stateArray[] = $state;
    }
    // now you have an array to work with 
    If not (and you don't feel like making it iterative), if it's simply a case that the $states object has a class variable which is an array that contains the desired state objects, then you could just grab that array directly (if it's public) or via a "getter" method that simply returns that array.
    PHP Code:
    class States
    {
        private 
    $states = array();

        public function 
    __construct()
        {
            
    // includes whatever it is that populates $this->states
        
    }

        public function 
    getStates()
        {
            return 
    $this->states;
        }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jan 2003
    Posts
    216
    Great idea. I just looped through and "converted" it to an array. Worked great. Thanks, NogDog!

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Sweet.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles