dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: for loop

  1. #1
    Join Date
    Nov 2008
    Posts
    78

    for loop

    Hi!
    I need help with for.
    I built a web page structured in this way: I have a div that contains three columns.
    I have an array that contains a list of items that I place inside of my columns.
    what I can not do is build a for loop to place the elements as in the example below.

    <div>
    <div id="column1">
    item 1
    item 4
    </div>
    <div id="column2">
    item 2
    item 5
    </div>
    <div id="column3">
    itemt 3
    item 6
    </div>
    </div>

    Thanks for help..

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,443
    PHP Code:
    $output = array();
    $counter 0;
    foreach(
    $data as $value) {
      
    $output[$counter++ % 3][] = $value;
    }
    foreach(
    $output as $column) {
      echo 
    "<div>";
      foreach(
    $column as $value) {
        echo 
    "<p>$value</p>";
      }
      echo 
    "<div>";

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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