Grouping query results and putting in a table

Codes here !

Moderators: macek, egami, gesf

fredws
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Jul 16, 2003 4:16 pm

Grouping query results and putting in a table

Postby fredws » Wed Jul 16, 2003 5:14 pm

I hope I am in the right forum to ask this.
I have a database with a company table and a clients table. There are several clients for each company. What I am wanting to do is group these, such as putting them in a table or box where I would have :
Company1
client1 info
client2 info
client3 info

Company2
client1 info
client2 info

and so on.

Right now, I can get it to print them all out, but they are not grouped.
TIA!

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Wed Jul 16, 2003 5:27 pm

select * from clients group by company

fredws
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Jul 16, 2003 4:16 pm

Postby fredws » Wed Jul 16, 2003 6:03 pm

I tried that, but then it gave only the first instance. I was wrong in my first post, the tables are called "clients" and "orders"

here is the code that I have now:

$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
connect!");

// get project list
$query = "SELECT clients.company, orders.first_name, orders.last_name, orders.address, orders.address2, orders.city, orders.state, orders.zip, orders.Phone, orders.ssn, orders.ssn2,orders.ssn3, orders.dob, orders.memo, orders.date_time FROM clients, orders WHERE clients.clientid = orders.clientid ORDER BY clients.company";
$result = mysql_db_query($database, $query, $connection) or die ("Error in
query: $query. " . mysql_error());

//interate through resultset and display
while (list($company, $first_name,$last_name,$address,$address2,$city,$state,$zip,$phone,$ssn,$ssn2,$ssn3,$dob,$memo,$date_time)=mysql_fetch_row($result))
{
echo"<br>Client: <b>$company</b><br>";
echo"$first_name $last_name<br>";
echo"$address<br>$address2<br>";
echo"$city , $state $zip<br>";
echo"Phone:$phone <br>SSN: $ssn<br>";
echo"SSN-2:$ssn2 SSN-3:$ssn3<br>";
echo"Date of Birth: $dob<br>Notes: $memo<br>";
echo"Entry Date: $date_time";
echo"<hr>";
}

Of course this just prints out a flat list, with out any grouping.

fredws
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Jul 16, 2003 4:16 pm

Postby fredws » Thu Jul 17, 2003 9:52 am

Well, I got it!
I left my query the same, with the ORDER BY company in there, then
I just had to put in some if statements and some checks to see if the company name was different, and if so, put in the html tags to put a border up.


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 3 guests

cron