Join MySQL Table then filter result as column name

Go To StackoverFlow.com

0

here is my code i am using to fetch mysql result from 4 different tables

SELECT DISTINCT c.title as CourseTitle, t.title as TopicTitle, l.title as LessonTitle,    r.title as ResourceTitle, r.location, r.type, r.duration
FROM j17_lessons l, j17_topics t, j17_courses c, j17_resources r
WHERE
CONCAT(c.title, t.title, l.title, r.title, r.type, r.location) LIKE '%Fatih%'
AND c.id = t.course_id
AND l.topic_id = t.id
AND r.lesson_id = l.id
ORDER BY c.title, t.id, l.id, r.id;

Here is screen shot of my fetch result http://i40.tinypic.com/2v1w0ib.png

Now what i need is to create a HTML Tables for each 'CourseTitle' in database.

Using SQL statement and PHP Code i can get result for first query but i need a second query to split table foreach 'CourseTitle'

/* connect to the db */
$connection = mysql_connect('localhost','root','123');
mysql_select_db('alhudapk',$connection);

/* show tables */
$result = mysql_query('SELECT DISTINCT c.title as CourseTitle, t.title as TopicTitle,     l.title as LessonTitle, r.title as ResourceTitle, r.location, r.type, r.duration
FROM j17_lessons l, j17_topics t, j17_courses c, j17_resources r
WHERE
CONCAT(c.title, t.title, l.title, r.title, r.type, r.location) LIKE '%Taleem%'
AND c.id = t.course_id
AND l.topic_id = t.id
AND r.lesson_id = l.id
ORDER BY c.title, t.id, l.id, r.id',$connection) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {

$table = $tableName[0];

echo '<h3>',$table,'</h3>';
$result2 = mysql_query('SELECT '.$table . 'AS' .$table);
if(mysql_num_rows($result2)) {

Please guide me to build a correct and better code

2012-04-04 18:08
by Amanullah
If your design requires table/field names to be stored in other fields, then your design should be reworked. It'll become an unmaintainable mess VERY quickly - Marc B 2012-04-04 18:14


1

What I would do is put the database results into a big array structure with the data arranged in the same sort of order it should be printed out. This makes maintaining the code a bit easier.

// run the query as you did in the question

$courses = array();

// use mysql_fetch_assoc as it makes the code clearer
while($row = mysql_fetch_assoc($result)) {
    $ct = $row['CourseTitle'];
    // Found a new Course Title? If so create an array to put the data rows in
    if(!isset($courses[$ct]))
        $courses[$ct] = array();

    // add this row to the end of its course array
    $courses[$ct][] = $row;
}

// now print the results out
foreach($courses as $title =>$course) {
    echo "<h3>$title</h3>";
    echo "<table>";
    foreach($course as $line) {
        echo "<tr><td>" . $line['TopicTitle'] . "</td><td>" 
             . $line['LessonTitle'] . "</td></tr>";
    echo "</table>";
}

The code above is only printing out the first 2 columns , but if you can get it to work you should be able to add the rest quite easily.

2012-04-04 18:49
by Annabel
page result is blank : - Amanullah 2012-04-04 19:00
try adding some debugging lines - at the end of the while loop you could put: echo "Adding a line. - Annabel 2012-04-04 19:32
after the while loop put print_r($courses);. Then use view source to see what is coming out. Maybe the problem is that your query isn't working - Annabel 2012-04-04 19:35
You forgot 1 { thanks man : - Amanullah 2012-04-04 19:42


0

add:

GROUP BY c.title

to the end of your SQL statement.

2012-04-04 18:20
by Jarrett Barnett
I will manage SQL query just tell me how can i get foreach html table as foreach Column Nam - Amanullah 2012-04-04 18:39
Ads