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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Tables in php, rows/columns confusion

    Hi everyone!

    I am hoping someone can help me with a simple php table?
    I am a total beginner with php and am a little lost in this...

    I have a db table with the fields 'date' (primary key), 'heading' and 'text' and I need these to be displayed so that the date and the heading are in 2 columns in the same row and the 'text' in the same column as 'heading' but in a row beneath it. This is a news table so content will be added all the time and should be displayed by date.

    I am using this now:
    Code:
    <? 
    	
    $link=mysql_connect("", "", "") 
    or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db('Mydatabase') or die('Could not select database');
    
    $query = 'SELECT * FROM news';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    
    
    // Printing results in HTML
    echo "<table>\n";
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
            echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
    }
    echo "</table>\n";
    
    // Free resultset
    mysql_free_result($result);
    
    // Closing connection
    mysql_close($link);
    
     ?>
    But it shows everything in the same row. I realize I need to call the variables separately and put then into the tables but I am confused as to how I would do that.

    sorry if this is a crazy simple question, but help would be appreciated

    best!
    Figurine

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Try something like
    PHP Code:
    while ($line mysql_fetch_array($resultMYSQL_ASSOC)) {
      echo  
    "<tr>
                   <td>{$line['date']}</td>
               <td>{$line['heading']}</td>     
               </tr>
           <tr>
                   <td colspan=\"2\">{$line['text']}</td>
           </tr>"
    ;

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Figurine (08-15-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    19
    Thanked 0 Times in 0 Posts
    thank you once again!

    You're the best Abduraooft


  •  

    Posting Permissions

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