Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

multiple select menu - categories.php - insert values to table


drferrari

Recommended Posts

I create multiple select menu with extra values for products.

 

$materials_query_selected = tep_db_query("select products_id, materials_id from " . TABLE_PRODUCTS_TO_MATERIALS . " where products_id = '" . $HTTP_GET_VARS['pID'] . "'");
while ($materials = tep_db_fetch_array($materials_query_selected)) {
 $materials_array_selected[] = array('id' => $materials['materials_id']);
}

$materials_query = tep_db_query("select * from " . TABLE_PRODUCTS_MATERIALS . " where language_id = '".$languages_id."' order by materials_id");
while ($materials = tep_db_fetch_array($materials_query)) {
 $materials_array[] = array('id' => $materials['materials_id'],
								 'text' => $materials['materials_name']);
}
echo tep_draw_mselect_menu('materials_id[]', $materials_array, $materials_array_selected, 'size=5');

 

New Tables:

 

products_materials

_________________________________________
| materials_id | materials_name | language_id |
----------------------------------------------------------------------
| 1		 | cotton 50%	 |	 1				|
----------------------------------------------------------------------
| 2 		 | cotton 100%		 |	 1			 |
----------------------------------------------------------------------

 

products_to_materials

____________________________

| products_id | materials_id |

------------------------------------------------

 

How I achieve insert values in above Tables. (with multiple select menu)

 

I try something like this in section

 

if ($action == 'insert_product') {

$sql_data_materials_array = array_merge($sql_data_materials_array);
tep_db_perform(TABLE_PRODUCTS_TO_MATERIALS, $sql_data_materials_array);

 

but I think this not work well.

Link to comment
Share on other sites

out of the blue i think this query should do it all @ once for you:

 

$materials_query = tep_db_query("SELECT
p2m.materials_id,		
pm.materials_id,
pm.materials_name
FROM
" . TABLE_PRODUCTS_MATERIALS . " pm,
" . TABLE_PRODUCTS_TO_MATERIALS . " p2m
WHERE
p2m.products_id = '" . $HTTP_GET_VARS['pID'] . "'
AND		
pm.materials_id = p2m.materials_id
");

 

ooopsss i think i misunderstood you.

You want to insert the selected into the product_to_materials?Then read this one:

 

http://stackoverflow.com/questions/21562441/insert-data-to-mysql-database-from-multiple-select-list-html-form

Link to comment
Share on other sites

@@wHiTeHaT

 

Hmmm, foreach but how I make it to work in this section:

if ($action == 'insert_product') {

 

In this section you add the products to mysql with this code.

$insert_sql_data = array('products_date_added' => 'now()');
	    $sql_data_array = array_merge($sql_data_array, $insert_sql_data);
	    tep_db_perform(TABLE_PRODUCTS, $sql_data_array);

 

 

and I forgot this in: includes/functions/html_output.php

 

// Output a form multiple select menu
function tep_draw_mselect_menu($name, $values, $selected_vals, $params = '', $required = false) {
$field = '<select name="' . $name . '"';
if ($params) $field .= ' ' . $params;
$field .= ' multiple>';
for ($i=0; $i<sizeof($values); $i++) {
if ($values[$i]['id'])
{
 $field .= '<option value="' . $values[$i]['id'] . '"';
 if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) ) {
	 $field .= ' SELECTED';
 }
 else
{
for ($j=0; $j<sizeof($selected_vals); $j++) {
if ($selected_vals[$j]['id'] == $values[$i]['id'])
{
	 $field .= ' SELECTED';
}
}
}
} else { $field .= '<option value="0"'; }
 $field .= '>' . $values[$i]['text'] . '</option>';
}
$field .= '</select>';
if ($required) $field .= TEXT_FIELD_REQUIRED;
return $field;
}

Link to comment
Share on other sites

I try in admin/categories.php add with multiple select menu to new product some extra value e.g.

cotton 50% and cotton 100% and save it to mysql table:

 

products_to_materials

_________________________________

| products_id | materials_id |

---------------------------------

you also have this table:

products_materials

_______________________________________________

| materials_id | materials_name | language_id |

--------------------------------------------------

| 1 | cotton 50% | 1 |

--------------------------------------------------

| 2 | cotton 100% | 1 |

--------------------------------------------------

Link to comment
Share on other sites

when I select in multiple select menu: 2 values - 1) cotton 50%, 2) cotton 100%.

 

I try pass this 2 values to:

$sql_data_materials_array = array('materials_id' => tep_db_prepare_input($HTTP_POST_VARS['materials_id']));

and then $sql_data_materials_array goes to:

 

if ($action == 'insert_product') {
	 $insert_sql_data = array('products_date_added' => 'now()');

	 $sql_data_array = array_merge($sql_data_array, $insert_sql_data);
	 tep_db_perform(TABLE_PRODUCTS, $sql_data_array);

	 $products_id = tep_db_insert_id();
	 $insert_sql_data = array('products_id' => $products_id);

	 $sql_data_materials_array = array_merge($insert_sql_data, $sql_data_materials_array);
	 tep_db_perform(TABLE_PRODUCTS_TO_MATERIALS, $sql_data_materials_array);

	 tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . (int)$products_id . "', '" . (int)$current_category_id . "')");
	 }

 

In mysql Table (products_to_materials) products_id is ok, THE PROBLEM is with materials_id column here the code add only the last materials_id (number 2), but I wanna add for products_id 1 add materials_id 1 and another record products_id 1 and materials_id 2.

 

can anybody help.

Link to comment
Share on other sites

if ($action == 'insert_product') {
			 $insert_sql_data = array('products_date_added' => 'now()');
			 $sql_data_array = array_merge($sql_data_array, $insert_sql_data);
			 tep_db_perform(TABLE_PRODUCTS, $sql_data_array);

			 $products_id = tep_db_insert_id();
			 $insert_sql_data = array('products_id' => $products_id);
			 $sql_data_materials_array = array_merge($insert_sql_data, $sql_data_materials_array);
 foreach ($sql_data_materials_array as $key => $val) {
{
			 tep_db_query("insert into " . TABLE_PRODUCTS_TO_MATERIALS . " (products_id, materials_id) values ('" . (int)$products_id . "', '" . $val . "')");
			 //tep_db_perform(TABLE_PRODUCTS_TO_MATERIALS, $sql_data_materials_array);
}
			 tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . (int)$products_id . "', '" . (int)$current_category_id . "')");
			 }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...