This topic is locked

Multiple Values Selection From Different Categories

2/19/2009 11:48:18 AM
PHPRunner Tips and Tricks
D
Darkwoods author


Simple but very useful code

A. This tutorial is for who wants to have a multiple selection of fields/items from different categories...

B. Fields/Items are sorted by related categories

-------------------------------------------------------------------------------------------------
1. Create an table for the items:

CREATE TABLE `test`.`items` (

`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,

`item_name` VARCHAR( 265 ) NOT NULL ,

`categoryID` VARCHAR( 65 ) NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE = MYISAM


2. Create the categories table

CREATE TABLE `test`.`categories` (

`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,

`cat_name` VARCHAR( 265 ) NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE = MYISAM


3. Open PHPrunner and go the Visual editor choose the add page and insert this code in the PHP Code Snippet

echo ' <input type=hidden name="" value="multiselect">

<select size = "15" id="" name="" multiple >

';



$dbquery = mysql_query("SELECT * FROM categories");

while ($dbrow = mysql_fetch_array($dbquery))

{

$idvalue= $dbrow['id'];

$cat_name= $dbrow['cat_name'];



echo '<optgroup label="'. $cat_name . '">';



$dbquery2 = mysql_query("SELECT * FROM items where categoryID = $idvalue ORDER BY item_name;");
while ($dbrow2 = mysql_fetch_array($dbquery2))

{



$id= $dbrow2['id'];

$item_name= $dbrow2['item_name'];

$categoryID = $dbrow2['categoryID'];



echo ' <option value="' .$id .'">- ' . $item_name .'</option> ';

}

}
echo '</SELECT>';


That's it...
it only works on the add page for now please if you can make it work on the edit page with selected option it would be perfect

H
hydkrash 2/23/2009



Simple but very useful code

A. This tutorial is for who wants to have a multiple selection of fields/items from different categories...

B. Fields/Items are sorted by related categories

-------------------------------------------------------------------------------------------------
1. Create an table for the items:

CREATE TABLE `test`.`items` (

`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,

`item_name` VARCHAR( 265 ) NOT NULL ,

`categoryID` VARCHAR( 65 ) NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE = MYISAM


2. Create the categories table

CREATE TABLE `test`.`categories` (

`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,

`cat_name` VARCHAR( 265 ) NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE = MYISAM


3. Open PHPrunner and go the Visual editor choose the add page and insert this code in the PHP Code Snippet

echo ' <input type=hidden name="" value="multiselect">

<select size = "15" id="" name="" multiple >

';
$dbquery = mysql_query("SELECT * FROM categories");

while ($dbrow = mysql_fetch_array($dbquery))

{

$idvalue= $dbrow['id'];

$cat_name= $dbrow['cat_name'];
echo '<optgroup label="'. $cat_name . '">';
$dbquery2 = mysql_query("SELECT * FROM items where categoryID = $idvalue ORDER BY item_name;");
while ($dbrow2 = mysql_fetch_array($dbquery2))

{
$id= $dbrow2['id'];

$item_name= $dbrow2['item_name'];

$categoryID = $dbrow2['categoryID'];
echo ' <option value="' .$id .'">- ' . $item_name .'</option> ';

}

}
echo '</SELECT>';


That's it...
it only works on the add page for now please if you can make it work on the edit page with selected option it would be perfect


Hi, I believe the edit page will be highly dependent on how the data is thereafter stored into the database.
Either store it as a deliminated string, or a normalized array.
Any suggestions would be good.

K
kinho 2/26/2009



Hi, I believe the edit page will be highly dependent on how the data is thereafter stored into the database.
Either store it as a deliminated string, or a normalized array.
Any suggestions would be good.


The code itself looks fine to me.

Are you intending to add multiple items from multiple categories to the database?

Judging from the tables - categories and items, I think it saved one item per record,

so you will be saving multiple item records on multiple selections.

By the way, did the code snipnet do any record saving?
Seemingly I am missing something here, can you elaborate a little more after a user

select multiple items/fields from the categoties?
Regards,

Kin

D
Darkwoods author 2/26/2009

The code itself looks fine to me.

Are you intending to add multiple items from multiple categories to the database?

Judging from the tables - categories and items, I think it saved one item per record,

so you will be saving multiple item records on multiple selections.

By the way, did the code snipnet do any record saving?
Seemingly I am missing something here, can you elaborate a little more after a user

select multiple items/fields from the categoties?
Regards,

Kin


the point of this code is to save the value numbers to another table
so you need to create a maintable to save the value numbers in it.

U
unimatrix 5/27/2009

In order to save to field, use this:
[codebox]echo ' <input type=hidden name="" value="multiselect">

<select size = "15" id="" name="value_field_name" multiple >

';[/codebox]
use the name="" of the select to set the name to value_field_name. So if you want the selected options to save to a field called "my_options" in the database:
name="value_my_options"

U
unimatrix 6/19/2009

Here is how to make it work on the edit page (and even select options that are already selected.

$dbquery = mysql_query('SELECT * FROM `com_option_cat` WHERE `vender_id` ='.$_SESSION["OwnerID"]) or die("no results in cat");//print_r($_SESSION);

$myid = $_GET["editid1"];
$dbItem = mysql_query('select item_options from com_items where item_id = '.$myid);

$items = mysql_fetch_array($dbItem);

//print "items ";

//print_r($items);
$iArray = split(",", $items[0]);
//$stuff = mysql_fetch_array($dbquery);
echo ' <input type="hidden" name="type_item_options" value="multiselect">

<select size = "15" name="value_item_options[]" multiple >

';



$i = 0;

while ($dbrow = mysql_fetch_array($dbquery))

{

$idvalue = $dbrow['option_categoryid'];

$cat_name = $dbrow["option_name"];



echo '<optgroup label="'. $cat_name . '">';


$dbquery2 = mysql_query("SELECT * FROM `com_items_options` WHERE `option_categoryid` = ".$idvalue." AND `vender_id` = ".$_SESSION['OwnerID']);
while ($dbrow2 = mysql_fetch_array($dbquery2))

{



$id= $dbrow2['option_id'];

$item_name= $dbrow2['option_name'];

$categoryID = $dbrow2['option_categoryid'];

//print "Printed";

if (in_array("$id", $iArray)) {

$selected = "selected";

} else {

$selected = "";

}



echo ' <option value="' .$id .'"'.$selected.'>- '. $selected. ' ' . $item_name . ' (IDNUM:' .$id .')</option> ';

}
}
echo '</SELECT>';