Jump to content


Photo

Combo Boxes


  • Please log in to reply
11 replies to this topic

#1 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 July 2006 - 01:59 AM

Basically what im doing is trying to load information from multiple tables.
So say i have table1 with some stuff and table2 with some stuff. How do i go about adding table1 and table2 stuff into one combo box.

I found this function on the net
function Combo_Box($cb_name,$table_name,$order_by="",$asc="",$css_class="",$id="") {
		if ($table_name) {
			if($id) {
				$disable = " disabled ";
			}
			if ($order_by) {
				$order_by = " ORDER BY ".$order_by." ".$asc;
			}
			$sql = "SELECT * FROM ".$table_name.$order_by;
			$result = mysql_query($sql);
			$show_Combo_Box = ""
			."<SELECT name=\"".$cb_name."\" class=\"".$css_class."\" ".$disable."> \n"
			."<OPTION value=\"0\"></OPTION>\n";
			WHILE ($row = mysql_fetch_array($result)) {
				$selection = "";
				if($id){
					if($row[0] == $id){
						$selection = " selected ";
					}
				}
				$show_Combo_Box .= ""
				."<OPTION value=\"".$row[0]."\" ".$selection.">"
				.$row[0]
				."</OPTION> \n";
			} // End WHILE
			$show_Combo_Box .= ""
			."</SELECT>\n";
			mysql_free_result($result);
			echo $show_Combo_Box;
		} // End if ($table_name)

But it only accepts 1 table. How can i change to accept more than 1 table? I was thinking if its possible to just do something like.

$table_name = "table1 AND table2"
or making $table_name an array then how can i loop through it until the end of the array?
Im having a hard time trying to get that to work. Any suggestions on how to do this thanks.


#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 03:11 AM

I think you might have to use JOINS, but I honestly don't understand those for the LIFE of me. You could look them up in the mySQL manual for the syntax.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 03:14 AM

are you just trying to make a select box that contains all the entries from one table, as well as all the entries from another table?

#4 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 July 2006 - 03:38 AM

I dont think join will work, join kind of combines the columns together unless there is a way to join rows.
Note: in my case im just taking row[0] as thats all i need is the first column of all entries.
EX:
Table1:
Name, Address, Date
1,ten,0
2,two,0
3,five,0
Table2:
Name, Address, Date
4,0,0
5,0,0
6,0,0

now in the select box it will have the following options

1
2
3
4
5
6
by combining the two tables together.

#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 04:10 AM

something like the following:

<?php
echo '<select name="option">';
$tables = array('table1', 'table2');
foreach ($tables AS $tablename)
{
  $query = "SELECT name FROM $tablename ORDER BY name ASC";
  $resource = mysql_query($query) or die("Error with query ($query): ".mysql_error());
  while (list($name) = mysql_fetch_array($resource, MYSQL_NUM))
  {
    echo "<option>$name</option>";
  }
}
echo '</select>';
?>

give that a whirl.

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 July 2006 - 04:35 AM

Does it work i like that code tell us cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 July 2006 - 03:44 PM

Does it work i like that code tell us cheers.


had to get some sleep to try it out as i still couldnt get it :P

But finally got it, table_name was defined before thats your array.

<?php
echo "<SELECT name=\"itemtype\"> \n"
		."<OPTION value=\"0\"></OPTION>\n";
foreach ($table_name1 AS $tablename)
{
	$sql = "SELECT * FROM ".$tablename.$order_by;
	$result = mysql_query($sql);
  WHILE (list($tablename) = mysql_fetch_array($result))
  {
    $show_Combo_Box .= ""
				."<OPTION value=\"".$tablename."\" ".$selection.">"
				.$tablename
				."</OPTION> \n";
  }
}
$show_Combo_Box .= ""
			."</SELECT>\n";
			mysql_free_result($result);
			echo $show_Combo_Box;
?>


$name 				= "tablecombine";	// 	Name of Combo Box		
$table_name 	= array('table1','table2');	        //	Name of mysql table	
$order_by 		= "";	//	Ordering (optional)
$asc_desc 		= "asc";	//	Ascending/Descending (optional)
$style		 		= "";	//	Css Style Sheets (optional)
$id_value 		= "";	//	Post 'id' to be selected (optional)


$ComboBox = new Combo_Box($name,$table_name,$order_by,$asc_desc,$style,$id_value);

Here it is as a function in case someone wants to use this.

class Combo_Box {
function Combo_Box($cb_name,$table_name,$order_by="",$asc="",$css_class="",$id="") {
     if($id) {
				$disable = " disabled ";
			}
	 echo "<SELECT name=\"".$cb_name."\" class=\"".$css_class."\" ".$disable."> \n"
		."<OPTION value=\"0\"></OPTION>\n";
	foreach ($table_name AS $tablename)
{
      if ($tablename) {
			if ($order_by) {
				$order_by = " ORDER BY ".$order_by." ".$asc;
			}
			$sql = "SELECT * FROM ".$tablename.$order_by;
			$result = mysql_query($sql);
			WHILE (list($tablename)= mysql_fetch_array($result)) {
				$selection = "";
				if($id){
					if($tablename == $id){
						$selection = " selected ";
					}
				}
				$show_Combo_Box .= ""
				."<OPTION value=\"".$tablename."\" ".$selection.">"
				.$tablename
				."</OPTION> \n";
			} // End WHILE
		} // End if ($table_name)
} // foreach
$show_Combo_Box .= ""
			."</SELECT>\n";
			mysql_free_result($result);
			echo $show_Combo_Box;
			} // End function Combo_Box
} // End class Combo_Box

I found this function from another site, just modified it to use an array as akit pointed me in the right direction.

credits: http://www.phpclasse...ckage/1894.html

#8 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 16 July 2006 - 11:41 PM

ok need some help again. working fine to get multiple tables and what not. How can i go about getting multiple columns now. example.

first,second,third

table1
1,2,4
3,5,5
2,6,8

table2
7,3,7
3,7,8
3,6,3

you have your array tables = array('table1','table2')

then it goes through for the foreach and queries them and adds them to the select box.

so..
1
3
2
7
3
3

if you use
$sql = "SELECT * FROM ".$tablename.$order_by;
with the * it will grab the first column

you can use something like
$sql = "SELECT second FROM ".$tablename.$order_by;

and it will grab the second column like
2
5
6
3
7
6

Now how can i get two columns so it will be something like
1 (2)
3 (5)
2 (6)
7 (3)
3 (7)
3 (6)

where the inside the () is the second column i need to get, so in the select box it shows the first column then the second right next to it. So those would be your choices as shown above.

Hope thats understandable. Thanks.


#9 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 17 July 2006 - 06:37 AM

anyone?

#10 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 17 July 2006 - 11:06 PM

Ok i have seem to have got it and i worked it down to 1 query now. Is this better to do one query with a bunch of unions?
This is what i have now:

<?php
class Combo_Box {
function Combo_Box($cb_name,$table_name,$order_by="",$asc="",$css_class="",$id="") {
if (sizeof($table_name) > '0'){
$i='0';
foreach ($table_name AS $tablename)
{
	if($i == "0")
	{
	  $sql .= "SELECT column1,column2 FROM ".$tablename;
	}
	else
	{
		$sql .= " UNION SELECT column1,column2 FROM ".$tablename;
	}
	$i++;
	
}
}
else
{
	echo "Specify a database";
}

		if ($table_name) {
			if($id) {
				$disable = " disabled ";
			}
			if ($order_by) {
				$order_by = " ORDER BY ".$order_by." ".$asc;
			}
			$sql .= $order_by;
			
			$result = mysql_query($sql) or die("Error with query (".$sql."): ".mysql_error());
			$show_Combo_Box = ""
			."<SELECT name=\"".$cb_name."\" class=\"".$css_class."\" ".$disable."> \n"
			."<OPTION value=\"0\">Select</OPTION>\n";
			WHILE ($row  = mysql_fetch_array($result)) {
				$selection = "";
				if($id){
					if($row['indexi'] == $id){
						$selection = " selected ";
					}
				}
				$show_Combo_Box .= ""
				."<OPTION value=\"".$row['column1']."\" ".$selection.">"
				.$row['column1']." (".$row['column2'].") "
				."</OPTION> \n";
			} // End WHILE
			$show_Combo_Box .= ""
			."</SELECT>\n";
			mysql_free_result($result);
			echo $show_Combo_Box;
		} // End if ($table_name)
	} // End function Combo_Box
} // End class Combo_Box
?>

Is this a good way to do it or is there a better way?

#11 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 18 July 2006 - 03:18 AM

as far as i can tell, you don't need a UNION at all.  you simply need to select the second value from your table:

$query = "SELECT column1, column2 FROM table1";
blah blah mysql_query stuff
while ($row = mysql_stuff())
{
  echo "<option>{$row['column1']} ({$row['column2']})</option>";
}

or am i missing something?  you can easily integrate that into your first code - select both columns, and chance the <option> echoed.

#12 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 18 July 2006 - 03:21 AM

its not from table1, its from table1 and table2 whatever is in the table_name array. I have to add more than two tables together and then sort them.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users