k4pil Posted February 25, 2006 Share Posted February 25, 2006 Hi there ppl!I am trying to create a table by querying two database tables but am getting stuck.e.g. One table is case(caseID,priority,created,subject)The other is customer(custID,name,surname,phone)Now my table - i want it to look something like this;CASEID PRIORITY CREATED SUBJECT NAME SURNAME PHONE1 4 1/2/2006 php jim bob 999etc...Normally you'd have a while loop, going through the database table then adding to the html table as $i incremented. But what do u do when there is two loops???NE HELP???thnks in advance Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/ Share on other sites More sharing options...
elias Posted February 25, 2006 Share Posted February 25, 2006 Hi k4pilWhat comes to mind is to have one while loop for the main table as you say, and then within that loop, when you come to the Name, Surname and PHone fields, you start another while loop, within the first one. In this way, for every "hit" in the first loop, the second loop fills out the remaining fields from the other table.good luckelias Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12448 Share on other sites More sharing options...
hitman6003 Posted February 25, 2006 Share Posted February 25, 2006 What is the common field between them? In other words, how are you linking the customers to their case and vice versa?case(caseID,priority,created,subject)customer(custID,name,surname,phone, [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]caseID[!--colorc--][/span][!--/colorc--])[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]customer[/color] [color=green]LEFT[/color] [color=green]JOIN[/color] case ON customer.caseID [color=orange]=[/color] case.caseID [!--sql2--][/div][!--sql3--][code]$result = mysql_query($query);while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo " <tr> <td>CASEID = $row[caseID]</td> <td>PRIORITY = $row[pritority]</td> <td>CREATED = $row[created]</td> <td>SUBJECT = $row[subject]</td> <td>NAME = $row[name]</td> <td>SURNAME = $row[surname]</td> <td>PHONE = $row[phone]</td> </tr>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12458 Share on other sites More sharing options...
k4pil Posted February 26, 2006 Author Share Posted February 26, 2006 The tables are actually like;customer(custID, name, surname, email)case(caseID, custID, date)custID links the two together.i still can't get then query to work. I have tried;SELECT * FROM customer, case WHERE customer.ID = case.custIDandSELECT * FROM case LEFT JOIN customer ON case.custID = case.custID Still no luck. Read the manual and its still no good. Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12573 Share on other sites More sharing options...
hitman6003 Posted February 26, 2006 Share Posted February 26, 2006 Reverse your tables in your sql statement:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]customer[/color] [color=green]LEFT[/color] [color=green]JOIN[/color] case ON customer.custID [color=orange]=[/color] case.custID[!--sql2--][/div][!--sql3--] Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12575 Share on other sites More sharing options...
k4pil Posted February 26, 2006 Author Share Posted February 26, 2006 I have tried that and the error i get is; #1064 - 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 'case ON customer . custID = case . custID LIMIT 0, 30' at line 1 I don't understand why. Does anyone else??? Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12576 Share on other sites More sharing options...
wickning1 Posted February 26, 2006 Share Posted February 26, 2006 CASE is a reserved word. Try this:[code]SELECT * FROM customer cu LEFT JOIN `case` c ON cu.custID = c.custID[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12581 Share on other sites More sharing options...
k4pil Posted February 26, 2006 Author Share Posted February 26, 2006 Its working. I changed it slightly because i was getting a lot of null values output. I changed it to$query = "SELECT * FROM customer, `case` WHERE customer.ID = `case`.custID";Now in $query i get a data which contains all customers with case history. Now i need to get from that data those customer the user has searched for. Using a form i have the value $name, $surname etc.. how can i search these from $query??Or do i need to do something with the $query i.e. "SELECT * FROM customer, `case` WHERE customer.ID = `case`.custID WHERE custID = $custID AND name = $name"Hope this makes senseThanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/3592-creating-a-table-using-php-help/#findComment-12583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.