1

I have a database that is to be used as an online scheduling of resources. The database is set up with the following tables (note: table names are the first bullet points, and column names are indented under each table name):

  • consultant
    • consultant_id
    • f_name
    • l_name
    • user_name
    • password
  • client
    • client_id
    • client_name
  • event_type
    • event_id
    • event_type
    • event_full_name
  • billing_status
    • billing_id
    • billing_type
  • dates (Note that all available dates are listed in this table, however there are some dates that are not able to be used for my purposes so not every day is listed here. Think of it as an inventory of available days.)
    • date_id
    • date
  • calendar_event
    • calendar_event_id
    • consultant_id
    • client_id
    • event_id
    • billing_id
    • date_id

Every time a consultant is assigned to an event, it gets added to the calendar_event table, and a new calendar_event_id is created.

On my PHP page, I have the following query:

$query = "SELECT * FROM billing_status, calendar_event, client, consultant, dates, event_type  WHERE (consultant.consultant_id = calendar_event.consultant_id AND client.client_id = calendar_event.client_id AND event_type.event_id = calendar_event.event_id AND billing_status.billing_id = calendar_event.billing_id AND dates.date_id = calendar_event.date_id) ORDER BY calendar_event.date_id";
    $consultantresults = mysql_query($query) or die ('Query failed: ' . mysql_error()); 

I am then displaying the output as follows:

echo "<html><head></head><body><table>"; 
while ($consultantresult=mysql_fetch_array($consultantresults))
{
    $consultantname = $consultantresult[f_name];
    $consultantname .= " ";
    $consultantname .= $consultantresult[l_name]; // to display the first and last names together

echo "<tr style=\"background-color:#eee;\"><th>Date</th><th>" . $consultantname . "</th></tr>
<tr><td>" . date('D M d, Y', strtotime($consultantresult[date])) . "</td><td>" . $consultantresult[client_name] . " " . $consultantresult[event_type] . "</td></tr>";
}
echo "</table></body></html>";
} else {
header("Location: /index.php");
exit;
}

This manages to display all the records contained in the calendar_event table, but each record is displayed in its own rows like:

<table>
<tr style="background-color:#eee;"><th>Date</th><th>Consultant #1</th></tr>
<tr><td>Mon Aug 05, 2013</td><td>Client A</td></tr>
<tr style="background-color:#eee;"><th>Date</th><th>Consultant #2</th></tr>
<tr><td>Mon Aug 05, 2013</td><td>Client B</td></tr>
<tr style="background-color:#eee;"><th>Date</th><th>Consultant #3</th></tr>
<tr><td>Mon Aug 05, 2013</td><td>Client C</td></tr>
<tr style="background-color:#eee;"><th>Date</th><th>Consultant #1</th></tr>
<tr><td>Tue Aug 06, 2013</td><td>Client D</td></tr>
</table>

You'll notice that the first three entries are on the same date (date gets repeated each time), and the first and last entries are for the same consultant (name also gets repeated).

Instead, I'd like to have the table displayed with each unique consultant's name in the top heading row, and each unique date in the first column. None of the names or dates should be repeated in the first row/column. For example:

<table>
<tr style="background-color:#eee;"><th>Date</th><th>Consultant #1</th><th>Consultant #2</th><th>Consultant #3</th></tr>
<tr><td>Mon Aug 05, 2013</td><td>Client A</td><td>Client B</td><td>Client C</td></tr>
<tr><td>Tue Aug 06, 2013</td><td>Client D</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

It will be possible for a consultant to not be assigned on a particular day, so that day would be blank for them (I used &nbsp; as a placeholder).

I will also need to provide the end user with a way of only displaying certain dates, rather than every single entry in the entire calendar_event table.

Any ideas how I can modify the code above to get it to display the output correctly?

ScottD
  • 582
  • 3
  • 9
  • 24
  • Questions: Can more then one Consultant or Client be assigned to an event? – SH- Jun 21 '13 at 14:26
  • Yes, but the client and/or event should repeat itself under each consultant as applicable. For example Consultant #1 can be assigned to Client A and B, and work on the event "Training" for both, while Consultant #2 can be assigned to Client B and C, and work on Training for Client B, and Executive Prep for Client C. – ScottD Jun 21 '13 at 14:56
  • "Event" is probably not the best name for it here - think of "event" as a class that can be taught multiple times to multiple different clients by multiple consultants. – ScottD Jun 21 '13 at 15:02

3 Answers3

0

Short answer use joins. This will enable you to perform your logic.

If for some reason this is not possible. You can also process your result set in the PHP code to organize and manipulate as you see fit.

Community
  • 1
  • 1
SH-
  • 1,642
  • 10
  • 13
0

Have You ever heard about SQL JOIN?

Using this Your query should be:

SELECT * 
FROM calendar_event ce
LEFT JOIN billing_status bs ON ce.billing_id = bs.billing_id
LEFT JOIN client cl ON ce.client_id = cl.client_id
LEFT JOIN consultant co ON ce.consultant_id = co.consultant_id
LEFT JOIN dates dt ON ce.date_id = dt.date_id
LEFT JOIN event_type et ON ce.event_id et.event_it 
ORDER BY ce.date_id

Furthermore, if You'd look at dates within dates table You'd find out that every data should be unique itself therefore there is no need to have also unique primary key date_id while only the date should a primary column.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • I tried this with the `LEFT JOIN` as you mentioned, but the output still looks the same. Essentially a new table is created for each consultant on each date. The same date gets displayed multiple times, and the same consultant also gets displayed multiple times. – ScottD Jun 21 '13 at 14:52
0
I have not tested this code you need to do something like

    echo "<html><head></head><body><table>"; 

$out="";

$preDate="";
while ($consultantresult=mysql_fetch_array($consultantresults))
{
    $consultantname = $consultantresult[f_name];
    $consultantname .= " ";
    $consultantname .= $consultantresult[l_name]; // to display the first and last names together
    if($preDate!=date('D M d, Y', strtotime($consultantresult[date])))
   {
        if($out!="")
            echo $out."</td></tr>";        
        echo "<tr style=\"background-color:#eee;\"><th>Date</th><th>" . $consultantname . "</th></tr><tr><td>";
        $preDate= date('D M d, Y', strtotime($consultantresult[date]));
   echo date('D M d, Y', strtotime($consultantresult[date])) . "</td><td>";
     $out="";
   }
else
  {
     $out.=$consultantresult[client_name] . " " . $consultantresult[event_type];  
  }

}
echo "</table></body></html>";
} else {
header("Location: /index.php");
exit;
}
Swapnil
  • 592
  • 3
  • 13
  • This changed the output, but only allowed the data from the first consultant to be displayed. However it still displayed each date for the consultant in a new table (header "Date" "Consultant #1", row "Mon Aug 05, 2013" "Client A" then a new table with "Date" "Consultant #1", row "Tue Aug 06, 2013" "Client D") – ScottD Jun 21 '13 at 14:54
  • The number of consultants are predefined or may increase? if yes then rather than just echo data consider putting data in an array use date as index of main array then find the number of consultants and then show that consultants as new columns titles. $consultant=array( 'client_name'=>$consultantresult[client_name], 'event_type'=>$consultantresult[event_type]) eg $dataHolder[][date'][]=$consultant; You can show this data as you want iterating thorugh this array. – Swapnil Jun 22 '13 at 07:43