Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP/MYSQL table looping problem

    I've been having a lot of trouble recently fixing a problem i have encountered in a script i've written for a website i am creating. My problem is: I can loop a table row to display a filler (groups) for a given length up and down, but i cant make it stop after x rows down, start a new column, and then keep going with more info. I have a code which seems to be able to do this (code#2) but i cant integrate it with my code (code# 1) and make it work. What should i do?


    EX: WHAT I HAVE
    (CODE # 1)
    A
    B
    C
    D
    E
    F
    G
    H
    I

    WHAT I WANT:
    (CODE 1 + 2)
    A D G
    B E H
    C F I

    ------CODE # 1-------
    <?

    $sql="SELECT * FROM $tbl_name WHERE example='cool' AND DATE(datetime) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) ORDER BY number DESC LIMIT 4";
    // ORDER BY id DESC is order result by descending
    $result=mysql_query($sql);
    ?>
    <table>


    <?php
    while($rows=mysql_fetch_array($res… // Start looping table row
    ?>
    <tr>
    <td bgcolor="#FFFFFF"><a href="fillerexample"><? echo fillerexample; ?></a></td>
    </tr>
    <?
    }
    ?>
    </table>

    --------CODE # 2------------

    //the sorting function
    function vert_htmltable($table){
    $NbrLines = 3; // number of horizontal lines of the final table
    $NbreData = count($table);
    $NbrCol = 0;
    if ($NbreData != 0) {
    $k = 0;
    $return.="<table><tr>";
    while ($k < $NbreData){
    $return.="<td><table>";
    for ($i=1; $i<=$NbrLines; $i++){
    if ($k < $NbreData){
    $return.="<tr><td>";
    $return.=$table[$k]."</td></tr>";
    $k++;
    }
    if ($i == $NbrLines){
    $return.="</table></td>";
    }
    }
    $NbrCol++;
    }
    $return.="</tr></table>";
    }
    return $return;
    }
    //we process your "results" vars table ( keep it as a table as you did first )
    $vert_table=vert_htmltable($result);
    //showing result
    echo $vert_table;

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    <?php

    $sql
    ="SELECT * FROM $tbl_name WHERE example='cool' AND DATE(datetime) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) ORDER BY number DESC LIMIT 4";
    // ORDER BY id DESC is order result by descending
    $result=mysql_query($sql);
    ?>
    <table>
    <tr>

    <?php
    $i 
    0;
    $cols 3;
    while(
    $rows=mysql_fetch_array($result))
    {
        if (++
    $i $cols == 0)
        {
            print 
    '</tr><tr>';
        }
    ?>
    <td bgcolor="#FFFFFF"><a href="fillerexample"><? echo fillerexample?></a></td>
    <?
    }
    ?>
    </tr>
    </table>
    Try that, make sure its tested with data that doesn't conform to %3 to see how it treats the remaining td's that are missing.

  • #3
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this works sort of , but it wont display more than 4 results, this is what it looks like


    fillerexample 1 fillerexample 3 fillerexample 4

    fillerexample 2

    i would like it to go three rows down, and four columns across for a total of 12 displayed results... how would i go about this?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That is because you have set 4 as a LIMIT in your SQL. Also, I didn't notice this fillerexample in there, but I assume you've replaced it with something.


  •  

    Tags for this Thread

    Posting Permissions

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