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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    California
    Posts
    42
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Post Blank Cell in table with MySQL DB table

    I have this code that gets all the products off the DB and shows them in a color organized table.

    It will load all the products, but if the user entered a product to be displayed in row 1 col 1 and another in row 1 col 3, the cell 1-3 will just move over to 1-2.

    How can I change it so it leaves 1-2 empty rather than moving it mover?

    PHP Code:
    echo "<table>";
    for (
    $i 1$i <= 9$i++)
    {
        
    $sql "SELECT products.pname, product_categories.color, products.pid, products.clmn 
        FROM products, product_categories 
        WHERE products.cat=product_categories.pname 
        AND products.row={$i} 
        ORDER BY products.clmn;"
    ;
        
    $query mysql_query($sql);
        echo 
    "<tr>";
        while ( 
    $show mysql_fetch_array($query) )
        {
            echo 
    "<td bgcolor='#{$show['color']}' width='80' height='70'><a href='http://{$config['domain']}/transaction.php?pid={$show['pid']}&action={$action}&quant={$quant}'><b><div class=\"charge\">{$show['pname']}</div></b></a></td>";
        }
        echo 
    "</tr>";
    }

    echo 
    "</table>"
    Last edited by Puppet Master; 04-25-2011 at 01:13 AM. Reason: Changed Prefix to Resolved.
    Puppet Master + Programming = Eternal Bliss

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Instead of a while ( $show = mysql_fetch_array($query) ) loop, use another for() loop, and with each iteration, compare the value of your column "clmn" to the iterator of the for() loop. If they match, great, echo the query results. If the iterator is less than "clmn", you know you need to echo an empty <td> and go through the loop again.

    Your loop ends not when the query results end, but when the last column is compared (whatever number that is).

  • Users who have thanked Fumigator for this post:

    Puppet Master (04-25-2011)

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    California
    Posts
    42
    Thanks
    6
    Thanked 2 Times in 2 Posts
    [QUOTE=Fumigator;1082250]Instead of a while ( $show = mysql_fetch_array($query) ) loop, use another for() loop, and with each iteration, compare the value of your column "clmn" to the iterator of the for() loop. If they match, great, echo the query results. If the iterator is less than "clmn", you know you need to echo an empty <td> and go through the loop again.

    Okay, I wrote it the way you told me. It works, but is only showing me the first column of the table.

    Is there a part I did wrong?

    PHP Code:
    echo "<table>";
    /***********get the rows**************************/
    $getlimit mysql_query('SELECT * FROM homescreen_layout');
    $limit mysql_fetch_array($getlimit);
    $limit_num $limit['max_rows'];
    /**********show the rows********************************/
    for ($i 1$i <= $limit_num$i++)
    {
        
    $sql "SELECT products.pname, product_categories.color, products.pid, products.clmn 
        FROM products, product_categories 
        WHERE products.cat=product_categories.pname 
        AND products.row={$i}   
        ORDER BY products.clmn;"
    ;
        
    $query mysql_query($sql);
        
    $show mysql_fetch_array($query);
        
    /*******get the cols*************************************/
        
    $getcols mysql_query('SELECT * FROM homescreen_layout');
        
    $cols_array mysql_fetch_array($getcols);
        
    $cols $cols_array['max_cols'];
        
    /*******************show the cols************************/
        
    echo "<tr>";
        for (
    $c=1$c <= $cols$c++)
        {
            
    //while ($show = mysql_fetch_array($query))
            //{
                
    if($c == $show['clmn'])
                {
                
                    echo 
    "<td bgcolor='#{$show['color']}' width='80' height='70'><a href='http://{$config['domain']}/transaction.php?pid={$show['pid']}&action={$action}&quant={$quant}'><b><div class=\"charge\">{$show['pname']}</div></b></a></td>";
                }
                else
                {
                    echo 
    "<td bgcolor='#FFFFFF' width='80' height='70'>&nbsp;</td>";
                }
            
    //}
        
    }
        echo 
    "</tr>";
    }

    echo 
    "</table>"
    Last edited by Puppet Master; 04-24-2011 at 08:04 AM. Reason: Retried code
    Puppet Master + Programming = Eternal Bliss

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    As part of your for() loop you need to fetch from the products query.

  • #5
    New Coder
    Join Date
    Nov 2010
    Location
    California
    Posts
    42
    Thanks
    6
    Thanked 2 Times in 2 Posts
    It worked perfectly, Thanks for your help Fumigator
    Puppet Master + Programming = Eternal Bliss


  •  

    Tags for this Thread

    Posting Permissions

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