Returning results into a table with no repeat of data

This is a discussion on "Returning results into a table with no repeat of data" within the PHP Forum section. This forum, and the thread "Returning results into a table with no repeat of data 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 Sep 23rd, 2005, 15:42
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Returning results into a table with no repeat of data

Hi guys, i got another prob with a diff script of mine...

The user can click a letter of the alphabet and all the artists beginning with that letter will be shown. But my problem is if the user has say 5 songs in the db then the artist name will appear 5 times.

Below is the script which i currently have which does this:

Code: Select all
if (!$alphaletter) 
  { 
     echo 'No "letter" passed in, an error has occurred.  Please go back and try again.'; 
     exit; 
  } 
  else 
   { 
      $sql_query = "SELECT * FROM users_table WHERE artist like '$alphaletter%'"; 
  } 


$result = @mysql_query($sql_query) or die ("Invalid Query: " . mysql_error()); 

        echo '<table width="871" border="0" align="center" cellpadding="0" cellspacing="0"><tr><th><table border="1"  width="250" align="left"><tr align="left"><th>Artist</th></tr>'; 
         
$num_results = mysql_num_rows($result); 

        while ($row = mysql_fetch_array($result)) 
        { 
        echo '<tr align "left"><th align="left">'; echo ''.$row[1].'</th></tr>'; 
        echo '
'; 
        } 
        echo '</table></th></tr></table>'; 
?>
Can anyone tell me what i need to do so it only shows the artist once no matter how many songs they have in the db plzzzz?

Many thanks Lobster
Reply With Quote

  #2 (permalink)  
Old Sep 23rd, 2005, 16:09
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sypher
In the mysql query put:
LIMIT 1 at the end.
Reply With Quote
  #3 (permalink)  
Old Sep 23rd, 2005, 16:56
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
If i do that it only shows one artist all together. I want it to show many artists but only 1 of each.

Here is an example of my problem...say i got 3 songs by james smith and 3 songs by james black... i get

Artist ||

James Smith
James Smith
James Smith
James Black
James Black
James Black

But any of the James Smith's when clicked will give the same page..

So i just want

Artist ||

James Smith
James Black

Any ideas on how to do this?

Thanks lobster
Reply With Quote
  #4 (permalink)  
Old Sep 23rd, 2005, 22:37
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
In your sql sort by artist
then in your loop

if($lastartist != $thisartist) echo $thisartist;

if that doesn't make sence, say so, and i'll give you more details.
Reply With Quote
  #5 (permalink)  
Old Sep 24th, 2005, 10:42
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
I understand the IF statement, but im not sure where it goes in the code Sorry im still learning the basics of PHP, if you could explain a bit further that wud be gr8

Thanks Lobster
Reply With Quote
  #6 (permalink)  
Old Sep 24th, 2005, 19:03
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benbacardi Send a message via Skype™ to benbacardi
could you not just add "GROUP BY artistname" to the end of the SQL query string?
Reply With Quote
  #7 (permalink)  
Old Sep 24th, 2005, 19:27
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Ben you done it again! Thankyou!! arghhhhh that problem has been bugging me for the last 2 days!! Thanks!!!!!! Problem solved!

lobster :P
Reply With Quote
  #8 (permalink)  
Old Sep 24th, 2005, 20:23
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benbacardi Send a message via Skype™ to benbacardi
glad to help
Reply With Quote
Reply

Tags
returning, results, table, repeat, data

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
Repeat data entered in a text box... and maybe change it? simbo1231 JavaScript Forum 4 Dec 2nd, 2007 10:24
how access data from table in php, azeemserver PHP Forum 2 Aug 30th, 2007 11:09
Returning ajax data kenoli JavaScript Forum 1 Aug 21st, 2007 11:27
background-repeat: repeat y not working properly AdRock Web Page Design 12 Feb 25th, 2007 22:17
Problem returning results into table lobster1983 PHP Forum 2 Sep 15th, 2005 21:40


All times are GMT. The time now is 05:48.


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