#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    219
    Rep Power
    11

    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
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    Actually, if I'm reading this right, every state you display does have a location. Because you're getting the list of states from the locations.

    If the question is as simple as "I have an array that I need to split into four mostly equal parts" then array_chunk.

IMN logo majestic logo threadwatch logo seochat tools logo