SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo - two columns

    Hi chaps, i don't know if its more of a mysql solution or php...

    i have a list of products and want to echo 2 columns containing 50/50 of the data, so for example if i had 20 products, there would be two lists of 10. The number of products will change over time, therefore does anyone know of a solution?

    thanks in advance

    b

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Select 20 products from the database, then use PHP to split them into columns with 10 in each.

    The PHP for this would go something like...

    PHP Code:
    <div id="products">
      <?php foreach(array_chunk($products10) as $product_list): ?>
        <div class="list">
          <?php foreach($product_list as $product): ?>
            <?php echo $product['name']; ?>
          <?php endforeach; ?>
        </div>
      <?php endforeach; ?>
    </div>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony....im a little confused

    Using the query:

    $query_Products = "SELECT * FROM items ORDER BY prodtitle ASC";
    $Products = mysql_query($query_Products, $faboc) or die(mysql_error());
    $row_Products = mysql_fetch_assoc($Products);
    $totalRows_Products = mysql_num_rows($Products);

    Could you change the values ..sorry product/products references lost me

    thanks

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Certainly.

    I've added some comments that may help too.

    PHP Code:
    <?php
    # get 20 items from the database
    $sql 'SELECT * FROM items ORDER BY prodtitle ASC LIMIT 20;';
    # execute the query
    $res mysql_query($sql);
    # create an array to store the 20 items
    $items = array();
    # loop through the items from the database
    while($item mysql_fetch_assoc($res)){
      
    # save the item to our array
      
    array_push($items$item);
    }
    ?>
    <html>
      <head>
        <title>Deom</title>
      </head>
      <body>
        <div id="products">
          <?php foreach(array_chunk($items10) as $item_list): ?>
            <div class="list">
              <?php foreach($item_list as $item): ?>
                <?php echo $item['prodtitle']; ?>
              <?php endforeach; ?>
            </div>
          <?php endforeach; ?>
        </div>
      </body>
    </html>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hum i get the error

    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given (line 10)

    which is this section
    while($item = mysql_fetch_assoc($res)){

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    have you created a database connection prior to this point?

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That's because the query failed, for some reason.

    Replace with this bit and we'll go from there.

    PHP Code:
    # execute the query
    $res mysql_query($sql) or die(mysql_error()); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, my error, i referred to an old localhost connection

    Many thanks, works like a dream!

    b


Bookmarks

Posting Permissions

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