PHP/Mysql issue...

This is a discussion on "PHP/Mysql issue..." within the PHP Forum section. This forum, and the thread "PHP/Mysql issue... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 28th, 2006, 16:17
Up'n'Coming Member
Join Date: Sep 2005
Location: athens
Age: 25
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
PHP/Mysql issue...

Hi all!

I have a problem concerning data retrieval and ,in particular, how they are shown to the user.
To be more specific, assume we have the following tables[Mysql database]:

id name vehicle
1 nick LOTUS
2 nick BMW
3 peter MASERATI
4 mary FERRARI
5 lisa PEUGEOT
6 peter HARLEY
7 peter SEAT
8 lisa MERCEDES
9 steven LADA
10 george JEEP

If you run a query like:

$query = "SELECT name, vehicle from TABLE";

you will get the 10 rows as expected.
When it comes to printing, you have:

$result = mysql_query($query);
while ($line = mysql_fetch_row($result) )
{
echo $line[0]."\t".$line[1];
}

where, $line[0] is NAME and $line[1] is VEHICLE
and you get :

nick LOTUS
nick BMW
peter MASERATI
mary FERRARI
peter HARLEY
peter SEAT
... ...

What I want to do is print the results in the following way:

nick: LOTUS|BMW
peter: MASERATI|HARLEY|SEAT
mary: FERRARI
lisa: PEUGEOT|MERCEDES
steven LADA
george JEEP

that is, have one row per person. Is this done by , somehow, checking in the 'while loop' for something
,or is there a special function in Mysql that I am not aware of?
Reply With Quote

  #2 (permalink)  
Old Jan 28th, 2006, 20:00
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP/Mysql issue...

In your sql query sort by name, then...
PHP: Select all

$last '';
$result mysql_query($query);
while (
$line mysql_fetch_row($result) )
{
if(
$last == $line[0]) echo " | ".$line[1];
elseif(empty(
$last)) echo $line[0]."\t".$line[1];
else  echo 
"\n".$line[0]."\t".$line[1];
$last $line[0]

Of course you will need to change things to meet your needs, but that's the gist.
Reply With Quote
  #3 (permalink)  
Old Jan 29th, 2006, 13:59
Junior Member
Join Date: Jan 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP/Mysql issue...

Even simpler than that since you are using mysql use group_concat in your query.

Code: Select all
select user, group_concat(vehicle)
from yourtablename
group by user
Reply With Quote
Reply

Tags
phpmysql, issue

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
css issue acrikey Web Page Design 16 Jul 25th, 2007 18:37
css issue acrikey Web Page Design 4 May 24th, 2007 16:01
an IE issue sxc Web Page Design 3 Apr 23rd, 2007 18:37
Help Odd Issue? drappendix Web Page Design 8 Aug 9th, 2006 17:53
CSS IMG gap issue sisyphus74 Web Page Design 1 May 16th, 2005 17:46


All times are GMT. The time now is 21:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43