1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Help with PHP / MySQL

Discussion in 'BBS Hangout' started by ClutchCityReturns, Nov 20, 2009.

  1. ClutchCityReturns

    Joined:
    Apr 26, 2005
    Messages:
    13,427
    Likes Received:
    2,666
    I've been doing web design for a little over a year now, but I still consider myself a novice when it comes to server-side coding. So, I was wondering if anyone could help me out with this issue I'm having.

    First, go to this site: http://kurtrankin.aisites.com/IMD331/eap/index.php

    Click on the gallery page, then select any of the 5 galleries. On the subsequent page you'll notice that the images and their descriptions (all being pulled from a MySQL database) are in one long vertical line along the left of the page. That's not what I want.

    I need to know how to format my results so that they display in rows of three, like in this screen shot.

    [​IMG]

    Anybody know how to do this? Any help is appreciated.
     
  2. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    table
    php foreach result
    write tr td php if total result mod 3 equals zero, write in html an ending /tr , else write me another td and continue
    end foreach write a /tr just to be safe
    close table

    table
    tr 1, 2, 3, tr!
    tr 4, 5, 6, tr!
    tr 7... so on so forth... tr
    ...
    tr 100, 101, 102 tr!
    tr 103, 104 tr!
    close table

    EDIT: You should be able to do this REALLY FAST with Dreamweaver that's what she said.

    ;) Shoot you an email later, sir. I love doing this MOD and PHP and MATH overall.
     
    #2 SwoLy-D, Nov 20, 2009
    Last edited: Nov 20, 2009
  3. ClutchCityReturns

    Joined:
    Apr 26, 2005
    Messages:
    13,427
    Likes Received:
    2,666
    Yeah, you might need to be a little more specific. Remember, I'm a PHP novice, so reading it in PHP is hard enough...I'm not ready to translate short hand.
     
  4. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    Check your email. :eek:
     
  5. Depressio

    Depressio Member

    Joined:
    Mar 3, 2009
    Messages:
    6,416
    Likes Received:
    366
    Usually you'd build a table using two for loops, one for rows, one for columns. Something like (for a 5 row, 3 column table):

    HTML:
    <?php 
    
      $rows = 5;
      $cols = 3;
      echo "<table>";
      for ($r = 0; $r < $rows; $r += 1)
      {
        echo "<tr>";
        for ($c = 0; $c < $cols; $c += 1)
        {
          echo "<td>";
          echo "other stuff you want in table";
          echo "</td>";
        }
        echo "</tr>";
      }
      echo "</table>";
    
    ?>
    I suppose the tricky part would be to only make it the size of your results from the database. Say you want a 3-column table and your query result is a size of 5. Then, $rows = ceil($result_size/3) (in this case, 2). You may have to do a little handling in the inner loop for that 6th cell (3rd cell of the second row).

    There are probably fancier solutions, but this is generally how I've seen tables built in PHP. Then again, I haven't used PHP in years.
     
  6. Kyakko

    Kyakko Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39
    use (div) for layouts instead. it's a lot easier and more wc3 compliant. here's a simple code: replace parens with angle brackets

    (html)
    (head)
    ...
    (style type="text/css")
    .rowBlock{float:left}
    (/style)

    (/head)

    (?php


    $res=mysql_query("SELECT img_url FROM some_table WHERE some_condition='condition' ORDER BY ASC");

    $i=1;

    while($arrInfo=mysql_fetch_array($res)){
    echo "(div class=\"rowBlock\")(img src=\"$arrInfo[img_url]\"/)(/div)";

    if(($i%3)==0)
    echo "(br style=\"clear:both\"/)";

    ++$i;
    }
    ?)
    (/body)
    (/html)
     
    #6 Kyakko, Nov 20, 2009
    Last edited: Nov 20, 2009
  7. Depressio

    Depressio Member

    Joined:
    Mar 3, 2009
    Messages:
    6,416
    Likes Received:
    366
    You and your fancy css.
     
  8. DrLudicrous

    DrLudicrous Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
  9. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    Did you read my email? :confused:

    If you have Dreamweaver, which I assume you do since you're a web design guru, it's easy with recordsets and repeating regions. What you do is that first you create your recordset (it will be named rs_PhotoGallery based on a hard-coded galleryID or from a query string for when peeps want different galleries and you want to use the same script for all galleries).

    After you have it defined, you create a table with headers and one row. From the recordset, you drag each record's data (ex: photo_id, photo_name, photo_description) onto each cell. Make sure this runs with your data first by testing it.

    Once you have a return for one record in Dreamweaver, select the entire row, right-click it, and select Insert->Data Objects->Repeat Region. DW will then ask you which recordset (hopefully you only have one on the page) and it will ask you how many records you want.

    The entire above procedure is here. :eek: But that's only for when you have one record per row. If you want MANY records in one row and then skip to the next row, then just figure out the recordID and insert a < TR > if the ID of the record divided by three (or however many records you want) returns a reminder 0 (check if total mod 3 = 0 is true).

    Otherwise if you don't have DW, you will have to know how to get totals for your records, figure out on what record item your cursor or counter is, and then you have to do a "mod" to determine where to stop the record row. You have a total of records. Whether you want to do this with DIVs or TABLEs, you will have to figure out a few things before you let PHP know where to go next as far as record or break the line.

    :cool: Good stuff!
     

Share This Page