Pulling data from multiple mySQL tables... NEED HELP please!
Pulling data from multiple mySQL tables... NEED HELP please!
compcentral
Posts: 40Questions: 0Answers: 0
Okay, pulling data from a mySQL database using the basic PHP example file after configuring server_processing.php works just fine, but how can I relate tables to each other to get the data that I want. Let me try to explain this a little better... Here are the three tables that I want to get information from:
1. Courses Table
- id
- teacherID
- hostDistrictID
- courseName
- courseType
- startDate
- endDate
2. Teachers table
- id
- lastName
- firstName
- title
- emailAddress
3. Districts table
- id
- name
- websiteURL
The "teacherID" column in the Courses table relates to the the "id" column in the Teachers table and the "hostDistrictID" column related to the "id" column of the Districts table. I know how I would normally link all of this information together to display it in a table, but how can I do the same using DataTables?
So, in the end I want the following fields to be displayed to the user:
Course Name, Course Type, Teacher Name (first last with an tag to link to thier email), Host District Name (linked to thier website URL), Start Date, and End Date.
1. Courses Table
- id
- teacherID
- hostDistrictID
- courseName
- courseType
- startDate
- endDate
2. Teachers table
- id
- lastName
- firstName
- title
- emailAddress
3. Districts table
- id
- name
- websiteURL
The "teacherID" column in the Courses table relates to the the "id" column in the Teachers table and the "hostDistrictID" column related to the "id" column of the Districts table. I know how I would normally link all of this information together to display it in a table, but how can I do the same using DataTables?
So, in the end I want the following fields to be displayed to the user:
Course Name, Course Type, Teacher Name (first last with an tag to link to thier email), Host District Name (linked to thier website URL), Start Date, and End Date.
This discussion has been closed.
Replies
Hth,
Gerardo
You can solve this purely in SQL, or you can get each piece of info separately in SQL (so you need more columns in the table), and use bVisible: false to hide the columns you don't want and fnRender to combine the data.
Hth,
Gerardo
Normally, my query would be something like this:
"SELECT courses.id, courses.teacherID, courses.hostDistrictID, courses.courseName, courses.courseType, courses.startDate, courses.endDate, teachers.id, teachers.lastName, teachers.firstName, teachers.title, teachers.emailAddress, districts.id, districts.name, districts.websiteURL
FROM courses, teachers, districts
WHERE teachers.id = courses.teacherID AND districts.id = courses.hostDistrictID"
but I'm just not sure how to implement the WHERE clause without breaking the filtering that is done when someone uses the search feature of DataTables. So my question is: how do I modify the example "server_processing.php" script to accomplish this?
OR you can do the data managing in SQL, like below, leading to less columns in dataTables
SELECT c.name, c.type, concat("", t.firstName, " ", t.lastName, ""), concat("
EDIT: code removed... updated in the next post.
The rest of the script is the same as the example server_processing.php file.
[code]
$aColumns = array( 'c.courseID', 'c.teacherID', 'c.districtID', 'c.name', 'c.courseType', 'c.startDate', 'c.endDate', 't.teacherID', 't.lastName', 't.firstName', 't.title', 't.email', 'd.districtID', 'd.shortName', 'd.websiteURL' );
$sIndexColumn = "c.courseID";
$sTable = "courses c, teachers t, districts d";
$sWhere = "WHERE c.teacherID = t.teacherID AND c.districtID = d.districtID";
/* Database connection information removed */
/*
* MySQL connection
*/
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i
When I load the page, I get the following JSON response:
{"sEcho":3,"iTotalRecords":"3","iTotalDisplayRecords":"1","aaData":[[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]]}
I know there is data there. Any idea why I'm getting all null fields?
Oh, and the field names are all accurate. I don't get any errors... just null data.
[code]
SELECT SQL_CALC_FOUND_ROWS c.name, c.courseType, concat( "", t.firstName, " ", t.lastName, "" ) fullname, concat( "", d.shortName, "" ) district, c.startDate, c.endDate
FROM courses c, teachers t, districts d
WHERE c.teacherID = t.teacherID AND c.districtID = d.districtID
[/code]
If I execute that query in phpmyadmin, I get the following response (which is correct):
name courseType fullname district startDate endDate
Test Course Math Joe Smith Website 02/02/2011 06/03/2011
However, the JSON response is:
{"sEcho":0,"iTotalRecords":"3","iTotalDisplayRecords":"1","aaData":[[null,null,null,null,null,null]]}
Is the data in $rResult? What happens next?
[code]
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i
The sql select statement becomes:
[code]
SELECT SQL_CALC_FOUND_ROWS c.name, c.courseType, concat( "", t.firstName, " ", t.lastName, "" ) fullname, concat( "", d.shortName, "" ) district, c.startDate, c.endDate FROM courses c, teachers t, districts d WHERE c.teacherID = t.teacherID AND c.districtID = d.districtID AND (c.name LIKE '%ma%' OR c.courseType LIKE '%ma%' OR concat( "", t.firstName, " ", t.lastName, "" ) fullname LIKE '%ma%' OR concat( "", d.shortName, "" ) district LIKE '%ma%' OR c.startDate LIKE '%ma%' OR c.endDate LIKE '%ma%' ) ORDER BY c.name asc LIMIT 0, 10
[/code]
I then get an error that states, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fullname LIKE '%ma%' OR concat( "", d.shortName, "<' at line 3" and of course a corresponding JSON error.
What should the SQL statement look like to enable the filtering/sorting to work properly? I've never tried using CONCAT and LIKE together like this before.
Working code in case anyone has a similar problem:
[code]
<?php
$q1 = "'";
$q2 = '"';
$concat1 = "concat( ".$q2."".$q2.", t.firstName, ".$q2." ".$q2.", t.lastName, ".$q2."".$q2." )";
$concat2 = "concat( ".$q2."".$q2.", d.shortName, ".$q2."".$q2." )";
$aColumns = array( 'c.name', 'c.courseType', $concat1, $concat2, 'c.startDate', 'c.endDate');
$sIndexColumn = "c.courseID";
$sTable = "courses c, teachers t, districts d";
$sWhere = "WHERE c.teacherID = t.teacherID AND c.districtID = d.districtID";
/* Database connection information removed */
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i
[/code]
thank you so much .... you save a lot of my time ... thank you twice :)
Btw, did you try to add entries? I want to add an entry in the child table based on the already existing entry in the parent table. So when I click the add button in the form I want a drop down list of the existing parent entries from where I can choose one and then add an entry in the child table corresponding to it. I searched the forum but couldn't find a post relating to this.