Problem returning results into table

This is a discussion on "Problem returning results into table" within the PHP Forum section. This forum, and the thread "Problem returning results into table 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 15th, 2005, 09:54
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Problem returning results into table

hi guys, I am pretty new to PHP but i am making a search script. Basically the user searchs the db via 2 fields (which are artist and song title), then the results are returned into a table. The code i have for this is as follows...

Code: Select all
	//set up the query
	if (!$artist && !$song_title)
  {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }
   if (!$artist)
   {
      $sql_query = "SELECT * FROM users_table WHERE song_title LIKE '%$song_title%'";
   }
   elseif(!$song_title)
   {
      $sql_query = "SELECT * FROM users_table WHERE artist LIKE '%$artist%'";
   }
  else
   {
      $sql_query = "SELECT * FROM users_table WHERE artist LIKE '%$artist%' AND song_title LIKE '%$song_title%'";
	  
  }
	
		$result = @mysql_query($sql_query) or die ("Invalid Query: " . mysql_error());
 
 
	echo '<table border="1"><tr><th>Artist</th><th>Song Title</th></tr>';
	
	
	while ($row = mysql_fetch_array($result)) 
	
	{
	 echo '<tr><td>'.$row["artist"].'</td><td>'.$row["song_title"].'</td></tr>';
	 
	 }
	 echo '</table>';
	
?>

When the results are returned, i have a table like:

|| Artist || Song Title ||
Greenday Wateva
Greenday Wateva
Greenday Wateva
Etc
etc

As you can see it repeats greenday down in the left column but i only need to have it once, next to the first song which is returned... i.e.

|| Artist || Song Title ||
Greenday Wateva
________ Wateva
________ Wateva
________ Wateva

(with the ____ meaning blank space:P)

I have been told about using the "group_concat " function, but my host users an older version of mysql meaning it will not work. So i have to find an alternavtive....could someone pleaseeeeeeeeeee help,

Many thanks Lobster
Reply With Quote

  #2 (permalink)  
Old Sep 15th, 2005, 15:45
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Code: Select all
$lastartist = '';
while ($row = mysql_fetch_array($result))
{
  $thisartist = '';
  if($lastartist != $row["artist"]) $thisartist = $row["artist"];
  echo '<tr><td>'.$thisartist.'</td><td>'.$row["song_title"].'</td></tr>';
}
there probaly is a cleaner way of doing this, but it works.
Reply With Quote
  #3 (permalink)  
Old Sep 15th, 2005, 21:40
Junior Member
Join Date: Sep 2005
Location: England
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Many thanks, it now works exactly how i want it!!
Reply With Quote
Reply

Tags
problem, returning, results, table

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
Problem with results of form Otter PHP Forum 2 Nov 7th, 2006 13:35
new problem. document.getElementById().width returning unassigned. why? sanchopansa JavaScript Forum 2 Jul 22nd, 2006 20:23
Index Server search results problem starrbuck Classic ASP 0 Feb 22nd, 2006 17:20
mysql_fetch_array - problem displaying results jswebdev PHP Forum 4 Oct 15th, 2005 17:01
Returning results into a table with no repeat of data lobster1983 PHP Forum 7 Sep 24th, 2005 20:23


All times are GMT. The time now is 01:46.


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