WordPress.org

Forums

[resolved] Need PHP help for sorting an array (18 posts)

  1. alexleonard
    Member
    Posted 7 years ago #

    As I'm not very strong on PHP, I thought I'd ask here for some help.

    I'm trying to sort out a way of displaying my links from one category, but in three columns. As this list is going to be expanding I need to code this in a future proof way.

    I'm thinking that the best way to do this would be to use the get_bookmarks function to return an array of my bookmarks. I could then analyse the array, work out how many bookmarks are in it, divide by 3, and then display that number of bookmarks in each column, being careful to avoid any duplication.

    Problem is I haven't the first notion of how I would go about doing this!

    Here's my english/code demo:

    <?php
    $array = get_bookmarks('category=1');
    $columnlength = $array/3;
    ?>
    
    <div class="col1">
    wp_list_bookmarks('limit=$columnlength');
    </div>
    
    <!-- As there's no offset function so I don't know what to do for columns 2 and 3 -->
    <div class="col2">
    wp_list_bookmarks('limit=$columnlength');
    </div>
    
    <div class="col3">
    wp_list_bookmarks('limit=$columnlength');
    </div>

    I am assuming that I'm going to need to do something a little more sophisticated and outside of the get_bookmarks/wp_list_bookmarks functions..?

  2. haochi
    Member
    Posted 7 years ago #

    Well, you can just pull out all the bookmarks in category 1 using just wp_list_bookmarks (set echo=0) and separate them into three pieces.

  3. The idea of a limit incorporates an offset into it, in this particular case. Because it's using mySql's LIMIT function, limit can actually be LIMIT offset,limit.

    php
    $array = get_bookmarks('category=1');
    $columnlength = (int)count($array)/3;
    ?>
    <div class="col1"><?php wp_list_bookmarks(array(
    'limit'=>'0,'.$columnlength,
    'category'=>1,
    ) ); ?></div>
    
    <div class="col2"><?php wp_list_bookmarks(array(
    'limit'=>$columnlength.','.$columnlength,
    'category'=>1,
    ) ); ?></div>
    
    <div class="col3"><?php wp_list_bookmarks(array(
    'limit'=>$columnlength*2.','.$columnlength+2,
    'category'=>1,
    ) ); ?></div>

    Might work. You might need to throw some +1's in there, I didn't think the logic entirely through.

  4. FreeKill
    Member
    Posted 7 years ago #

    Could you do it entirely with HTML and CSS? Wrap each bookmark in a floating div with 33% width, and your bookmarks should start to form 3 columns on their own...

    something like:

    <div id="bookmarks">
         <div style="width: 30%; float: left" class="float-mark">Bookmark</div>
         ... (continued for all bookmarks)
     </div>
  5. Floating all the bookmarks left would work, the problem is then they would not order up vertically but horizontally. Not a problem if you don't care about the order though.

  6. alexleonard
    Member
    Posted 7 years ago #

    Wow. Thanks everyone for the quick responses there :)

    I'm going these out this morning and see what sorts it out. I'll report back here later :)

    @FreeKill Thanks for the suggestion, but Otto42 had it right about them needing to order vertically, and what you suggest could probably be achieved in a less html code intensive way with:

    <ul id="bookmarks">
    <li>bookmark 1</li>
    <li>bookmark 2</li>
    <!-- etc -->
    </ul>

    With a CSS rules of:

    #bookmarks {
      width: 500px; /* or relevant width */
    }
    
    #bookmarks li {
      float: left;
      width: 30%;
      padding-right: 3%;
    }

    Potentially you probably already knew this!

  7. alexleonard
    Member
    Posted 7 years ago #

    Ok. I'm encountering some trouble, but I'm assuming it's my lack of basic PHP knowledge here!

    I have a few different links pages around the site in question, and as a result have created one links-page template which is called for each of these pages. Within the links page template I'm stating if is_page 22, display links in this manner, if is_page 45, display this way, etc.

    So I've decided that I only need to go with two columns, as 3 cols was two crowded. I'm using this variation of the code Otto42 put up, and while the page in question loads, the only code that's rendering is my div's - so there's an issue with the wp_list_bookmarks..

    // previous if statements for other pages
    
    } elseif (is_page('67')) {
      $array = get_bookmarks('category=12');
      $columnlength = (int)count($array)/2;
      ?>
      <div class="support-col1">
       <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12, ) ); ?>
      </div>
      <div class="support-col2">
       <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength+1, 'category'=>12, ) ); ?>
      </div>
      <?php
    	} else { // final fallback for other links pages }; ?>
  8. Hmmm.. That looks like it should work to me.

    Try setting 'categorize'=>0 in the list bookmarks call.

  9. alexleonard
    Member
    Posted 7 years ago #

    Thanks for sticking with me on this one! Still no joy with categorize. Here's the full code that I'm using, in case something else is throwing things out. I think the else statements are fine as it's sending out the <div class="support-col1"> but nothing in between..

    <?php if (is_page('53'))  {
    $cats = get_categories("type=link&hierarchical=0&include=4");
     foreach ($cats as $cat) {
      echo '<h2>'.$cat->cat_name.'</h2>';
      echo '<ul>';
      $books = get_bookmarks("category=$cat->cat_ID");
      foreach ($books as $book) {
       echo '<li><p><a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
       echo '<p>'.$book->link_description.'</p>';
       echo '</li>';
      } // end books loop
     echo '</ul>';
     } // end cats loop;
     } elseif (is_page('67')) {
      $array = get_bookmarks('category=12');
      $columnlength = (int)count($array)/2;
      ?>
      <div class="support-col1">
      <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
      </div>
      <div class="support-col2">
      <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength+1, 'category'=>12,'categorize'=>0 ) ); ?>
      </div>
      <?php
     } else {
      $cats = get_categories("type=link&hierarchical=0&exclude=4,12");
      foreach ($cats as $cat) {
      echo '<h2>'.$cat->cat_name.'</h2>';
      echo '<ul>';
      $books = get_bookmarks("category=$cat->cat_ID");
      foreach ($books as $book) {
      echo '<li><p><a href="'.$book->link_url.'">'.$book->link_name.'</a></p>';
      echo '<p>'.$book->link_description.'</p>';
      echo '</li>';
     } // end books loop
     echo '</ul>';
     } // end cats loop;
    };?>
  10. This line is a bit confusing...
    $columnlength = (int)count($array)/2;

    Try this:
    $columnlength = (int)((count($array))/2);

    Ensure that it's an int and not some kind of float or something.

  11. alexleonard
    Member
    Posted 7 years ago #

    Ok. That partially worked! It's now displaying two unordered lists with half the number of bookmarks listed!

    Only problem that remains... it's showing the same items in each list! So it's sussed out that there are 68 bookmarks in that category, and is displaying the first 34 from each list.

    Thanks for sticking at this for me (I think you're going to deserve a beer at the end of this ;)

  12. alexleonard
    Member
    Posted 7 years ago #

    I had a tinker and got it working.. I realised that I was too close and it had to be something simple. Turns out that I had a +1 in there that wasn't necessary...

    The correct code has ended up at:

    <?php
    $array = get_bookmarks('category=12');
    $columnlength = (int)((count($array))/2);
    ?>
    <div class="support-col1">
     <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
    </div>
    <div class="support-col2">
     <?php wp_list_bookmarks(array( 'limit'=>$columnlength.','.$columnlength, 'category'=>12,'categorize'=>0 ) ); ?>
    </div>

    Thanks for the help Otto! Once the site is live I'll send on a wee link so you can see what I was doing :)

  13. alexleonard
    Member
    Posted 7 years ago #

    And a wee beer should be flying towards you too :)

  14. bcreighton
    Member
    Posted 7 years ago #

    I used this my three columns - and I have just one quick problem. How do I get rid of the h2 header? ~thanks

  15. bcreighton
    Member
    Posted 7 years ago #

    Ooo, got it! For those who need it:

    <?php wp_list_bookmarks(array( 'limit'=>'0,'.$columnlength, 'categorize'=>0, 'title_li' =>0) ); ?>

    Thanks for everyone's hard work on this thread! Hopefully WP will add an offset parameter to make this a wee easier.

  16. peterfrom
    Member
    Posted 6 years ago #

    Great reading... One question, though... How would I achieve the same thing but with the category loop intact? I need to show the list in exactly the same way as above but with the categories on top of the links.

    That is, I need to fetch all categories and all links (bookmarks) and then list them in three columns. If I change the categorize to 1, then all the links (bookmarks) show up in the first column only but with the categories on top of the links. Using categorize with the value zero gives me the columns but with no category names.

    Help would be appreciated.

  17. webmatter
    Member
    Posted 6 years ago #

    Thanks for this useful thread! I have tried the 3-column solution. Somehow it doesn't accept $columnlength*2
    in

    <div class="col3"><?php wp_list_bookmarks(array(
    'limit'=>$columnlength*2.','.$columnlength,
    'category'=>1,
    ) ); ?></div>

    I am receiving an parse error "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' .." caused by $columnlength*2.

    Does anybody know what the error in the syntax is? Thanks in advance ...

  18. jimmiejo
    Member
    Posted 6 years ago #

    Could someone please post how they managed to get it to work with 3 or more columns?

Topic Closed

This topic has been closed to new replies.

About this Topic