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 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Displaying mysql select records in html css table and calculate total price

    hello guys i have created the below codes. the first one is the mysql select statement which works fine when i echo. the second one is an html/css table i created. i am stuck to the point where i can merge both of them so that i get a nice display and the total price of the products are calculated in each column.

    the first code selects product name, retailer name and product price from 5 tables. using the echo codes i can display the records in columns.

    code for mysql select statement

    PHP Code:
    <?php

    session_start
    ();

    include(
    'db_connect.php');

    $username $_SESSION['username'];

    $user mysql_fetch_assoc(mysql_query("select user_id from tbllogin where username = '{$username}'"));

    $query mysql_query
    ("SELECT * FROM tblfav_ret a, tblretailer b, tblretprod c, tblproduct d, tblfavourites e
    WHERE a.ret_id = b.ret_id
    AND b.user_id = c.user_id
    AND c.prod_id = d.prod_id
    AND d.prod_id = e.prod_id
    AND a.user_id = '{$user['user_id']}'"
    );

    $num mysql_num_rows($query);

    if(
    $num>0){

    echo 
    "<center><table bgcolor='grey' width='80%' border=0>";

    echo 
    "<tr bgcolor='#CCCCCC'>";
    echo 
    "<td><b><center>Name</td>";
    echo 
    "<td><b><center>Retailer</td>";
    echo 
    "<td><b><center>Price</td>";
    echo 
    "</tr>";

    while(
    $row mysql_fetch_assoc($query)){
         
    extract($row); 

         echo 
    "<tr>";
         echo 
    "<td style='text-align: center;'>".$row['prod_name']."</td>";
         echo 
    "<td style='text-align: center;'>".$row['ret_name']."</td>";
    echo 
    "<td style='text-align: center;'>".$row['prod_price']."</td>";
    echo 
    "</tr>";
         
    }
    echo 
    "</table>";
    }

    ?>
    i further created this html/css table which displays smthng like this

    product name <retailer_name> <retailer_name> <retailer_name> <retailer_name>
    prod_1 price1 price2 price3 price4
    prod_2 price1 price2 price3 price4
    total <total_price> <total_price> <total_price> <total_price>

    Code:
    <html>
    <body>
    
    <div id="myfavourites">
    
    <table class="fav_tbl">
    
    <thead>
                <tr>
                            <th scope="col">Product</th>
                            <th scope="col">Retailer 1</th>
                            <th scope="col">Retailer 2</th>
                            <th scope="col">Retailer 3</th>
                            <th scope="col">Retailer 4</th>
               </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th scope="row">Total per month</th>
                            <td>Total 1</td>
                            <td>Total 2</td>
                            <td>Total 3</td>
                            <td>Total 4</td>
                        </tr>
                    </tfoot>
    
    </table>
    
    </div>
    
    </body>
    </html>
    the help i need is to make the display in the html/css table and calculation of total price of products listed

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    462
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Just create a variable for each column and add the next item price to it during the loop.

    col1_total = 0.00;

    while($row = mysql_fetch_assoc($query)){
    extract($row);

    col1_total = col1_total + $row['prod_price'];

    echo "<tr>";
    echo "<td style='text-align: center;'>".$row['prod_name']."</td>";
    echo "<td style='text-align: center;'>".$row['ret_name']."</td>";
    echo "<td style='text-align: center;'>".$row['prod_price']."</td>";
    echo "</tr>";

    }

    You might have to verify the math...
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can also cheat and just use the += syntax in PHP.
    Also, remove the extract(). It serves no purpose, and has a potential to overwrite variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    462
    Thanks
    0
    Thanked 20 Times in 20 Posts
    I didn't know the += sytax would work with decimals. Seems a lot easier.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.


  •  

    Posting Permissions

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