SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting HTML tables with PHP

    Hi,

    I've got a basic php script that queries a MySQL db and places a recordset limited to 4 into an array then loops through and echos out a distinct html table row per db row.

    How do I go about echoing the table so it has one row with two cells, each cell containing a different array value (ie. $row["id"] goes in first cell, $row2["id"] goes in second cell, this row loops and row3["id"] goes in first cell in second table row and row4["id"]goes in second cell in second table row).

    I've attached a picture of how I need it to look if I've totally muddled the issue (note the picture shows 4 cells, I just need to loop the one row)....

    Thanks,
    Ian

  2. #2
    SitePoint Enthusiast Fuseblown's Avatar
    Join Date
    Jul 2001
    Location
    State College, PA
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a shot...this is just an example...change it to whatever you want/need..

    PHP Code:
    echo "<table>";
    mysql_connect($dbhost$dbuser$dbpass) or die("Unable to connect to database server!");

    mysql_select_db($dbname) or die("Unable to select database!");

    $query "SELECT * FROM table_name ORDER BY id ASC";

    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());

    if (
    mysql_num_rows($result) > 0) {
      while(
    $row mysql_fetch_object($result)) {
      echo 
    "
        <tr>
             <td>
    $row->id</td>
             <td>
    $row->field2</td>
            </tr>
      "
    ;

      }
    }

    else {
      echo 
    "<i>No values in the database!</i>";
    }

    mysql_close($connection);

    echo 
    "</table>"
    Now that's off the top of my head, using variables and such that I normally use in my code...

    I'm new to PHP/MySQL so it's probably not the best code, but hopefully it helps, hehe.
    Fuseblown

  3. #3
    SitePoint Enthusiast Fuseblown's Avatar
    Join Date
    Jul 2001
    Location
    State College, PA
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also there is a check in there...if you do not have any values in the database for that table, it'll give a message that there are no values present.
    Fuseblown

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the reply but this (below) will place field2 of the first record into the second cell. I need to have the id field (for example) of the second record in the second cell.

    PHP Code:
    if (mysql_num_rows($result) > 0) {
      while(
    $row mysql_fetch_object($result)) {
      echo 
    "
        <tr>
             <td>
    $row->id</td>
             <td>
    $row->field2</td>
            </tr>
      "
    ;

      }


  5. #5
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kinda strange...well, try this:

    PHP Code:
    $i 1;
    if (
    mysql_num_rows($result) > 0) {
      while(
    $row mysql_fetch_object($result)) {
        if (
    $i == 1){  
          echo 
    "<tr><td>$row->id</td>";
        }else{
          echo 
    "<td>$row->id</td></tr>\n";
          
    $i 0;
        }

        
    $i++;
      }
      
    // Close table row if still open
      
    if ($i == 1){
        echo 
    "<td>&nbsp;</td></tr>\n";
      }

    Hope this helps...


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
  •