2 Replies - 271 Views - Last Post: 16 September 2011 - 03:23 AM Rate Topic: -----

#1 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

sort options for select depending on database selection

Posted 13 September 2011 - 09:00 AM

Hi guys

what im after doing is adding an extra option that depends on what the data is from the database:

ie

--cask--
twaites
copper dragon
(+20 records)
--spirits--
smirnoff
absolute vodake
(+20 records)
--bottles--
etc..

does anybody know how i can fit a solution into my code

<?php
	define('MyConst', TRUE);
	include 'database/config.php';
	include 'database/opendb.php';

	$query  = "SELECT name,type FROM ".$_SESSION['comboName']." ORDER BY type";
	$res = mysql_query($query);
		
	while($row = mysql_fetch_array($res, MYSQL_ASSOC))
	{	
		echo"<option>{$row['name']}</option>";
	}
		
	include 'database/closedb.php';
?>



i tried adding

if ($row['type'] == "Cask"){
			echo "<option value=''>---Cask---</option>";
		}else if ($row['type'] == "Guest"){
			echo "<option value=''>---Guest---</option>";
		}else if ($row['type'] == "Lagers"){
			echo "<option value=''>---Lagers---</option>";
		}else if ($row['type'] == "Bottled Beer"){
			echo "<option value=''>---Bottled Beer---</option>";
		}else if ($row['type'] == "Wines"){
			echo "<option value=''>---Wines---</option>";
		}else if ($row['type'] == "Ciders"){
			echo "<option value=''>---Ciders---</option>";
		}else if ($row['type'] == "Softs"){
			echo "<option value=''>---Softs---</option>";
		}else if ($row['type'] == "Spirits"){
			echo "<option value=''>---Spirits---</option>";
		}


i added this into the while loop but no success it just repeats the header after each record,

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: sort options for select depending on database selection

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: sort options for select depending on database selection

Posted 13 September 2011 - 11:37 AM

Assuming you want to split the same drop down amongst cask, spirits, bottles, etc., you could do something like:
<?php
	define('MyConst', TRUE);
	include 'database/config.php';
	include 'database/opendb.php';

	$query  = "SELECT name,type FROM ".$_SESSION['comboName']." ORDER BY type";
	$res = mysql_query($query);
	$current_type = "";
	while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {	
		if($row['type'] != $current_type) {
			echo "<option>{$row['type']}</option>";
		}
		echo"<option>{$row['name']}</option>";
		$current_type = $row['type'];
	}
		
	include 'database/closedb.php';
?>


This post has been edited by CTphpnwb: 13 September 2011 - 11:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 Kersh86  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 11-August 11

Re: sort options for select depending on database selection

Posted 16 September 2011 - 03:23 AM

thanks that worked great
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1