#1
  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    14
    i'm trying to write a script that dynamically generates HTML tables depending on the results from an SQL query. So far, I have managed to set the HTML table headers to be the same as those in the MySQL table. [thanks to rod k -cheers !]

    Now i'm trying to put the results in the table. Basically, I need a MySQL function to retrieve data and put it into the table until there is no data left....

    (At the moment I am using "mysql_fetch_array()", but with this it is necessary to know the names of the columns to retrieve the information for each result set. Since my table is dynamic and should be able to build itself from different MySQL tables, the column names aren't fixed and so i can't use this to put the data in the tables...)

    any ideas ?

    -nick
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    At the moment I am using "mysql_fetch_array()", but with this it is necessary to know the names of the columns to retrieve the information for each result set. Since my table is dynamic and should be able to build itself from different MySQL tables, the column names aren't fixed and so i can't use this to put the data in the tables...)


    You don't have to remember the column names.Instead of that you can use coulmn index numbers..

    let us say you have the columns sno,name,address in a table..

    the coulmn index will start from 0.so you can retrive the coulmn values using index 0,1 and 2.

    eg:

    <?
    $row=mysql_fetch_array($result);

    $sno=$row[0];
    $name=$row[1];
    $address=$row[2];
    ?>





    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,640
    Rep Power
    4476
    There is also a function that will fetch the names of the columns also, if you want to display them since they are dynamic. check the manual under Mysql functions...

    ---John Holmes...
  6. #4
  7. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    14
    thanks Shiju... I had in fact came quite close to using a similar system using mysql_fetch_row instead of mysql_fetch_array....

    however, in terms of my table, I still have the same problem: I do not have a way of putting the data into the table unless i know how many elements to put in before creating a new <tr>[html table row].

    at the moment i have:

    while(mysql_fetch_row ($this->Query_ID)):
    $i = 0;
    echo "<tr><td>$db->Rowdata[$i]</td>
    <td>",
    $i++;


    This is far from working. Is there a way of findin the amount of elements in an array ?

    Then I could do something like:

    while($array=mysql_fetch_row ($this->Query_ID)):
    $i = 0;
    $j = [amount of elements in $array];
    echo "<tr>";
    while ($i < $j):
    echo "<tr><td>$db->Rowdata[$i]</td>
    <td>"; $i++;
    endwhile;
    echo "</tr>";

    it's all a bit stream-of-thought so apologies if i'm not clear.... thanks a lot though for all help from everyone so far

    -nick


    [This message has been edited by zcrar70 (edited July 11, 2000).]

    [This message has been edited by zcrar70 (edited July 11, 2000).]
  8. #5
  9. No Profile Picture
    ledjon
    Guest
    Devshed Newbie (0 - 499 posts)
    In perl the # of times in an array can be found by just saying "$var = @array;"... I'd assume there's something similar in PHP... though I'm not sure that will solver your problem.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    32
    Rep Power
    14
    since the result set is in array form

    can't you just do

    echo count($result);

    or am i missing the point
  12. #7
  13. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    14
    hi all,

    thanks a lot for your responses... i finally managed to do it ! this is how


    $db = new DBconnect; //this class is v similar to the one in the "accessing db with classes article

    $query = "SELECT * FROM table WHERE column like '%'";
    $db->dbquery($query);

    #first build table

    echo "<table><table border=1 bgcolor='#eeeeee'>";

    #build table headlines;
    echo "<tr>";
    while ($column3= $db->fetch_fields()): //this is just a mysql_fetch_fields function in the class

    echo "<th>$column3[name]</th>";
    endwhile;

    #enter results into database

    while($db->next_record()): //same func as in tutorial

    $i = 0;
    $j = sizeof($db->Record); //this is the magic function !! finds the amount of elements in an array... pretty much exactly the same as count (cheers Simpson...)
    echo "<tr>";

    while ($i < $j) {
    echo "<td>", $db->Record[$i], "</td>";
    $i++;}

    echo "<td>";
    endwhile;


    et voila ! it's all easy when you know what you're doing, it's just that that's the hard bit !
    thanks a lot for all your help....

    -nick

Similar Threads

  1. League table problem
    By silverduck in forum MySQL Help
    Replies: 8
    Last Post: February 10th, 2004, 12:38 PM
  2. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 06:19 PM
  3. Listing results in a 3 or 4 column table
    By muffty in forum PHP Development
    Replies: 8
    Last Post: January 14th, 2004, 06:39 AM
  4. trying to create and populate table with query results
    By alexmasters in forum PHP Development
    Replies: 20
    Last Post: January 5th, 2004, 07:59 PM
  5. trying to create and populate table with query results
    By alexmasters in forum MySQL Help
    Replies: 1
    Last Post: January 5th, 2004, 10:37 AM

IMN logo majestic logo threadwatch logo seochat tools logo