Results 1 to 11 of 11

Thread: tables PHP - resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Resolved tables PHP - resolved

    Hi all,

    in the below code i create a single column table on the fly using a returned record set, and this works great my question is how can i create a 2 column table as I just cannot work it out.

    PHP Code:
    <?php
    while($row mysql_fetch_array$result1 )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"
    echo 
    $row['name'] .'<br>';
    echo 
    '<a href="detail.php?item=' .$row['stockcode']. '&dept='.$_GET['dept'].'"> <img src="'.$row['img_location'].' " width="88" height="94"><br>';
    echo 
    $row['price']. '<br>';
    echo 
    $row['stockcode']. '<br>';
    echo 
    "</td>";  
    echo 
    "</tr>";

    ?>
    many thanks chris
    Last edited by Chrisio; Mar 22nd, 2005 at 11:14 AM. Reason: resolved

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: tables PHP

    Creating a new column would require another set of <td></td> tags.

    PHP Code:
    <?php
    while($row mysql_fetch_array$result1 )) {
    // Print out the contents of each row into a table
    echo "<tr><td>";
    echo 
    $row['name'] .'<br>';
    echo 
    '<a href="detail.php?item=' .$row['stockcode']. '&dept='.$_GET['dept'].'"> <img src="'.$row['img_location'].' " width="88" height="94"><br>';
    echo 
    $row['price']. '<br>';
    echo 
    $row['stockcode']. '<br>';
    echo 
    "</td><td>"
    echo 
    $row['another_field'] . '<br>';
    echo 
    "</td>";
    echo 
    "</tr>";
    }
    ?>
    Note you could also do this, which I find much cleaner:

    PHP Code:
    <?php
    while($row mysql_fetch_array$result1 )) {
        
    // Print out the contents of each row into a table
        
    echo '<tr>
            <td>' 
    $row['name'] .'<br>
                <a href="detail.php?item=' 
    .$row['stockcode'] . '&dept=' $_GET['dept'] . '"> 
                <img src="' 
    $row['img_location'] . '" width="88" height="94">
                <br>' 
    $row['price']. '<br>' $row['stockcode']. '<br>
            </td>
            <td>' 
    $row['another_field'] . '<br></td>
        </tr>'
    ;
    }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: tables PHP

    Quote Originally Posted by The Hobo
    Note you could also do this, which I find much cleaner:

    PHP Code:
    <?php
    while($row mysql_fetch_array$result1 )) {
        
    // Print out the contents of each row into a table
        
    echo '<tr>
            <td>' 
    $row['name'] .'<br>
                <a href="detail.php?item=' 
    .$row['stockcode'] . '&dept=' $_GET['dept'] . '"> 
                <img src="' 
    $row['img_location'] . '" width="88" height="94">
                <br>' 
    $row['price']. '<br>' $row['stockcode']. '<br>
            </td>
            <td>' 
    $row['another_field'] . '<br></td>
        </tr>'
    ;
    }
    ?>
    And to make it even cleaner:
    PHP Code:
    <?php while($row mysql_fetch_array$result1 )):
        
    // Print out the contents of each row into a table ?>
        <tr>
            <td><?php ecoh($row['name']) ?><br />
                <a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
                    <img src="<?php echo($row['img_location']) ?>" width="88" height="94">
                    <br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
                </a>                    
            </td>
            <td><?php echo($row['another_field']) ?><br></td>
        </tr>
    <?php endwhile; ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: tables PHP

    thanks for the replys, I havent made myself clear, at the moment the code that I have produces 1 long table 1 column wide that is populated

    such as

    item1
    item2
    item3

    and so on


    what i want is

    item1 item2
    item3 item4
    item5 item6

    and so on.

    can you help.

    chris

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: tables PHP

    /you just need to use the mod operator % to do this. @The mod operator returns the remainder after division, which can be used to determine whether or not to print a <tr> tag.
    PHP Code:
    <?php $count 0; while($row mysql_fetch_array$result1 )):
        
    // Print out the contents of each row into a table ?>
        <?php if($count == 0): ?><tr><?php endif; // start the row?>
            <td><?php ecoh($row['name']) ?><br />
                <a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
                    <img src="<?php echo($row['img_location']) ?>" width="88" height="94">
                    <br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
                </a>                    
            </td>
            <td><?php echo($row['another_field']) ?><br></td>
        <?php if($count == 1): ?></tr><?php endif; // end the row?>
    <?php 
    ++$count endwhile; ?>
    Last edited by visualAd; Mar 22nd, 2005 at 10:58 AM. Reason: Error in code.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: tables PHP

    thanks for replying so fast, when i use this code i get
    Parse error: parse error in C:\Program Files\Apache Group\Apache2\htdocs\catalog.php on line 61

    the problem being is there is no line 61 in the code. This is all the code from that page.
    PHP Code:
    <?php
        
    require('db_connect.php');

    $dept $_GET['pid'];
    $isle $_GET['item'];
    //get the cats from the db//
    $result mysql_query("SELECT DISTINCT category FROM shop WHERE toplevel = '".$dept."'") or die(mysql_error()); 
    $result1mysql_query("SELECT stockcode, name, price, description, img_location FROM shop WHERE category = '".$isle."'") or die(mysql_error());
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Catalog</title>
    </head>

    <body>
    <?php

    echo '<div align="left">';
    echo 
    '<font face="Century Gothic">';
    echo 
    '<p align="center">&nbsp;</p>';
    echo 
    "<table border='0'>";
    echo 
    "<tr> <th>Category</th></tr>";
    // keeps getting the next row until there are no more to get
    while($row mysql_fetch_array$result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"
    echo 
    '<a href="catalog.php?item=' $row['category'] . '&dept='.$_GET['pid'].'">' $row['category'] .'</a>' ;
    echo 
    "</td><td>";  
    echo 
    "</td></tr>";
    // Get all the data from the "example" table
    echo '<div align="center"></div>';
    echo 
    '<font face="Century Gothic">';
    echo 
    '<p align="center">&nbsp;</p>';
    echo 
    "<table border='0'>";



    $count 0; while($row mysql_fetch_array$result1 )):
        
    // Print out the contents of each row into a table ?>
        <?php if($count == 0): ?><tr><?php endif; // start the row?>
            <td><?php ecoh($row['name']) ?><br />
                <a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
                    <img src="<?php echo($row['img_location']) ?>" width="88" height="94">
                    <br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
                </a>                    
            </td>
            <td><?php echo($row['another_field']) ?><br></td>
        <?php if($count == 1?></tr><?php endif; // end the row?>
    <?php 
    ++$count endwhile; ?> 


    <p>
    </p>
    </body>
    </html>
    thanks for the help again

    chris

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: tables PHP

    The error was caused becuase you missed the closing curly bracket for the first while statement.
    PHP Code:
    while($row mysql_fetch_array$result )) {
     
    // Print out the contents of each row into a table
     
    echo "<tr><td>";
     echo 
    '<a href="catalog.php?item=' $row['category'] . '&dept='.$_GET['pid'].'">' $row['category'] .'</a>' ;
     echo 
    "</td><td>";  
     echo 
    "</td></tr>";
     
    // Get all the data from the "example" table
     
    echo '<div align="center"></div>';
     echo 
    '<font face="Century Gothic">';
     echo 
    '<p align="center">&nbsp;</p>';
     echo 
    "<table border='0'>";
    /// &lt;---- you forgot this 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: tables PHP

    I dont know what im doing wrong but now im getting parse error on line 51 which is
    PHP Code:
      <?php if($count == 1?></tr><?php endif; // end the row?>
    code now looks like

    PHP Code:
    <?php
        
    require('db_connect.php');

    $dept $_GET['pid'];
    $isle $_GET['item'];
    //get the cats from the db//
    $result mysql_query("SELECT DISTINCT category FROM shop WHERE toplevel = '".$dept."'") or die(mysql_error()); 
    $result1mysql_query("SELECT stockcode, name, price, description, img_location FROM shop WHERE category = '".$isle."'") or die(mysql_error());
    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Catalog</title>
    </head>

    <body>
    <?php

    echo '<div align="left">';
    echo 
    '<font face="Century Gothic">';
    echo 
    '<p align="center">&nbsp;</p>';
    echo 
    "<table border='0'>";
    echo 
    "<tr> <th>Category</th></tr>";
    // keeps getting the next row until there are no more to get
    while($row mysql_fetch_array$result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"
    echo 
    '<a href="catalog.php?item=' $row['category'] . '&dept='.$_GET['pid'].'">' $row['category'] .'</a>' ;
    echo 
    "</td><td>";  
    echo 
    "</td></tr>";
    // Get all the data from the "example" table
    echo '<div align="center"></div>';
    echo 
    '<font face="Century Gothic">';
    echo 
    '<p align="center">&nbsp;</p>';
    echo 
    "<table border='0'>";



    $count 0; while($row mysql_fetch_array$result1 )):
        
    // Print out the contents of each row into a table ?>
        <?php if($count == 0): ?><tr><?php endif; // start the row?>
            <td><?php ecoh($row['name']) ?><br />
                <a href="detail.php?item=<?php echo($row['stockcode'])?>&dept<?php echo($_GET['dept']) ?>">
                    <img src="<?php echo($row['img_location']) ?>" width="88" height="94">
                    <br /><?php echo($row['price']) ?><br><?php echo($row['stockcode']) ?><br />
                </a>                    
            </td>
            <td><?php echo($row['another_field']) ?><br></td>
        <?php if($count == 1?></tr><?php endif; // end the row?>
    <?php 
    ++$count endwhile; ?> 


    <p>
    </p>
    </body>
    </html>
    thanks again

    chrisio

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: tables PHP

    It should be:
    PHP Code:
    <?php if($count == 1): ?></tr><?php endif; // end the row?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    Re: tables PHP

    Quote Originally Posted by visualAd
    It should be:
    PHP Code:
    <?php if($count == 1): ?></tr><?php endif; // end the row?>

    yeps thats it. thanks very much works a treat
    chrisio

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: tables PHP

    If your problem has now been solved, don't forget to edit the first post and mark it as resolved.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width