Jump to content

Need help joining 3 tables and displaying


Sephiroth_ronin

Recommended Posts

Hello everyone, I need your help !
This is what I have as my 3 tables and their variables:
TABLE NAME: room_type
- ROOMTYPE_id
- ROOMTYPE_hostel
- ROOMTYPE_beds
- ROOMTYPE_bathroom

TABLE NAME: season
- SEASON_id
- SEASON_name
- SEASON_start
- SEASON_end

TABLE NAME: prices
- PRICE_id
- SEASON_id
- ROOMTYPE_id
- PRICE

Now I would like to have the results displayed in a PHP table like so:

                            SEASON_name1    SEASON_name2 ...
ROOMTYPE_beds1    PRICE                  PRICE
ROOMTYPE_beds2    PRICE                  PRICE
...

Can someone help me !?
Link to comment
Share on other sites

Ok I got the data from the query. The only problem I have is that I want to show the information in a table with SEASON_name as rows, ROOMTYPE_name as colums and in the cell joining the row and column : the PRICE (if there is one, if not = NULL !?!).

It should look like this :


                            SEASON_name1          SEASON_name2            ...
ROOMTYPE_name1    PRICE                        PRICE
ROOMTYPE_name2    PRICE2                        PRICE
...
Link to comment
Share on other sites

To output like that you will need to store the data in an array as you read from the db then output the array contents. Try
[code]
<?php
include 'db.php';  // connect to database

$sql = "SELECT r.roomtype_beds, s.season_name, p.price
        FROM room_type r
        INNER JOIN prices p ON r.roomtype_id=p.roomtype_id
        INNER JOIN season s ON p.season_id = s.season_id
        ORDER BY r.roomtype_beds, s.season_name";
$res=mysql_query($sql) or die(mysql_error());

$prices = $seasons = array();
while (list($beds, $season, $price)=mysql_fetch_row($res)) {
    $prices[$beds][$season] = $price;
    //
    //  get season names for headings
    //
    if (!in_array($season, $seasons)) {
      $seasons[] = $season;
    }
}
echo "<table border='1' cellpadding='2' cellspacing='0'>";
    // headings
echo '<TR><TH>Beds</TH>';
foreach ($seasons as $s) {
    echo "<TH>$s</TH>";
}
echo '<TR>';
    // prices
foreach ($prices as $beds => $sdata) {
    echo "<TR><TH>$beds</TH>";
    foreach ($sdata as $price) {
        echo "<TD>$price</TD>";
    }
    echo '<TR>';
}
echo '</table>';

?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.