Web Analytics Made Easy -
StatCounter PHP/MYSQL table looping problem - CodingForum

Announcement

Collapse
No announcement yet.

PHP/MYSQL table looping problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      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?

      Comment


      • #4
        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.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎