473,409 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

selecting multiple rows from a table and printing it in the same line

19
i m trying to extract multiple rows and print it in a single line in a web page.
My code

Expand|Select|Wrap|Line Numbers
  1. while ($line = mysql_fetch_array($app, MYSQL_ASSOC))
  2.  {     
  3.    echo "<tr><td>";    
  4.    echo "<a href=AssignedUpdateForm.php?app=".$line['App'].             
  5.    ">".$line['App']."</a></td>";     
  6.    echo "<td>" . $line['firstName'] ." ". $line['surname'] . "</td>"; 
  7.    echo "<td>" . $line['ServerName'] . "</td>";     echo "</tr>";
}
outputs this:
Expand|Select|Wrap|Line Numbers
  1. Application    employee    Server
  2.    z             a          c
  3.    z             b          c
  4.    z             a          d
  5.    z             b          d
however i want it to output
Expand|Select|Wrap|Line Numbers
  1. Application    employee    Server
  2.    z             a,b        c,d
  3.    z             a,b        c,d
can someone help please
Jan 29 '10 #1
7 2625
kovik
1,044 Expert 1GB
What is the logic here? There are 4 different types of records, but you are compacting them into 2. What is the logic for this compacting? Why is it that you can compact unrelated a's, b's, c's, and d's, but not z's? You need to be more specific.
Jan 29 '10 #2
nlal
19
wel actually the relationship hea is that an application can be handled by more than one employee and an application can be located on more than one server.Thus the one to many relatinship.
For eg.
relationship between Application and Employee
Expand|Select|Wrap|Line Numbers
  1. Employee    Application  
  2. john        sales
  3. mary        sales
relationship between application and server
Expand|Select|Wrap|Line Numbers
  1. Server      Application
  2. Server A    sales     
  3. Server B    sales     
  4.  
when i do a select statement from the two tables to print in a single table, i get
Expand|Select|Wrap|Line Numbers
  1. Application     Employee    Server
  2. sales           john        Server A
  3. sales           mary        Server A
  4. sales           john        Server B
  5. sales           mary        Server B
  6.  
However i want to print
Expand|Select|Wrap|Line Numbers
  1. Application     Employee      Server
  2. sales           john,mary     Server A,Server B
Is that possible?
Jan 30 '10 #3
kovik
1,044 Expert 1GB
Probably, yes. Tell us what your table structure looks like and the query that you are currently using.
Jan 30 '10 #4
nlal
19
My table structure is as follows:
Expand|Select|Wrap|Line Numbers
  1. DROP TABLE IF EXISTS employee;
  2. CREATE TABLE employee(
  3. emp_id VARCHAR(5) NOT NULL,
  4. firstName VARCHAR(30) NOT NULL,
  5. surname VARCHAR(30) NOT NULL,
  6. TelephoneNumber VARCHAR(7),
  7. Email VARCHAR(30),
  8. username VARCHAR(30),
  9. password VARCHAR(256),
  10. CONSTRAINT emp_pk_EDPnum PRIMARY KEY(emp_id));
  11.  
  12. DROP TABLE IF EXISTS Application;
  13. CREATE TABLE Application(
  14. App VARCHAR(30) NOT NULL,
  15. AppName VARCHAR(60) NOT NULL,
  16. AppDescription VARCHAR(180),
  17. YearDeveloped YEAR(4),
  18. Development_Language VARCHAR(30),
  19. Comments VARCHAR(120),
  20. SectionID INT NOT NULL,
  21. DBID INT NOT NULL,
  22. CONSTRAINT Application_pk_App PRIMARY KEY(App),
  23. CONSTRAINT Application_fk_SectionID FOREIGN KEY(SectionID) REFERENCES Section(SectionID),
  24. CONSTRAINT Application_fk_DBID FOREIGN KEY(DBID) REFERENCES DB(DBID));
  25.  
  26. DROP TABLE IF EXISTS App_emp;
  27. CREATE TABLE App_emp(
  28. App_empID INT NOT NULL AUTO_INCREMENT,
  29. emp_id VARCHAR(5) NOT NULL,
  30. App VARCHAR(30) NOT NULL,
  31. CONSTRAINT AppSupp_pk_AppSuppID PRIMARY KEY(App_empID),
  32. CONSTRAINT AppSupp_fk_EDPnum FOREIGN KEY(emp_id) REFERENCES SupportPersonnel(emp_id),
  33. CONSTRAINT AppSupp_fk_App FOREIGN KEY(App) REFERENCES Application(App));
  34.  
  35. DROP TABLE IF EXISTS Server;
  36. CREATE TABLE Server(
  37. ServerName VARCHAR(60) NOT NULL,
  38. RackNum INT NOT NULL,
  39. NetworkStatus INT,
  40. ProcessorSpecification VARCHAR(90),
  41. Memory INT,
  42. AvailableMemory INT NOT NULL,
  43. OperatingSystem VARCHAR(60),
  44. OS_version VARCHAR(60),
  45. LogonServer VARCHAR(30),
  46. BIOSversion VARCHAR(30),
  47. Remarks VARCHAR(60),
  48. App VARCHAR(30),
  49. CONSTRAINT Server_pk_ServerName PRIMARY KEY(ServerName),
  50. CONSTRAINT Server_fk_App FOREIGN KEY(App) REFERENCES Application(App));
  51.  
Expand|Select|Wrap|Line Numbers
  1. <?php    
  2.    // build query
  3.    $qry = "Select firstName, surname, App_emp.App, ServerName from  employee,App_emp,".
  4.    "Server where App_emp.emp_id=employee.emp_id and   App_emp.App=Server.App";
  5.  
  6.    //execute query
  7.   $app = execute_query($qry) or die('Query failed: ' . mysql_error());
  8.  
  9.    // write a loop to print out the results.
  10.    while ($line = mysql_fetch_array($app, MYSQL_ASSOC)) 
  11.      { 
  12.         echo "<tr><td>";
  13.         echo "<a href=AssignedUpdateForm.php?app=".$line['App'].
  14.         ">".$line['App']."</a></td>";
  15.         echo "<td>" . $line['firstName'] ." ". $line['surname'] . "</td>";
  16.         echo "<td>" . $line['ServerName'] . "</td>";
  17.         echo "</tr>";        
  18.      }    
  19. ?>
Jan 30 '10 #5
kovik
1,044 Expert 1GB
Would you mind showing the database structure in a tabular format? All of us who help you here do so on our own time, and sorting through your CREATE TABLE query isn't exactly fun. :P
Jan 30 '10 #6
nlal
19
I have attached the databse structure.
Attached Images
File Type: jpg untitled.jpg (11.8 KB, 193 views)
Jan 31 '10 #7
kovik
1,044 Expert 1GB
Not what I meant, but at least you've shown the relationships.

Servers have an application, applications can have multiple employees, and employees and can have multiple applications. And you want to merge all employees and applications by server?

I can't think of a way to make that into an atomic query. You'll have to use logic to merge the data together in the loops prior to output. Here's some semi-usable code, using implode():

Expand|Select|Wrap|Line Numbers
  1. $currentServer = null;
  2. $applications = array();
  3. $employees = array();
  4.  
  5. while ($data = next MySQL row) {
  6.   if ($currentServer && $currentServer != $data->server) {
  7.     // output table row here using implode() on the arrays
  8.  
  9.     $applications = array();
  10.     $employees = array();
  11.   } else {
  12.     $applications[] = $data->application;
  13.     $employees[] = $data->employee;
  14.   }
  15.  
  16.   $currentServer = $data->server;
  17. }
Jan 31 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
2
by: Robert | last post by:
Hi All, I'm trying to solve this for a few days now and I just can't figure it out... I have three tables set up, I'll simplify them for this question: Table 1: HOTELS Columns: HOTEL_ID,...
1
by: R. P. | last post by:
Anybody knows a way to produce output from a SELECT statement on a single line, instead of each hit on a separate line? Here is what I mean: let's say I have a table listing families, one per...
4
by: Lucius | last post by:
Hello everyone, I have a query problem. I'll put it like this. There is a 'publishers' table, and there is a 'titles' table. Publishers publish titles (of course). Now I want to make a query (in...
1
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
by: mark | last post by:
I can't seem to get the logic for printing multiple pages. I know I have to do a comparison between the bottom margin and the position of the next line to be printed and initiate a has more pages...
1
by: Andy | last post by:
Hi Gang I have a simple single table that has many duplicate rows in them. The distinctrows only works on more than one table. Is there a quick and easy way to select only the distict rows...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.