Post by blackarch » Mon Feb 01, 2016 10:40 am

This is for Controller :
Input name is idp (product_id), example user input twice it's 5656 & 8989.

Code: Select all

private $error = array(); 
     
  	public function index() {
		$this->language->load('item/item');
		
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('item/item');
		
		$this->getList();
	}
	
	protected function getList(){
		
		if (isset($this->request->get['head_text_field'])){
			$head_text_field = $this->request->get['head_text_field'];
		} else {
			$head_text_field = null;
		}
		
		if (isset($this->request->get['title_text_field'])){
			$title_text_field = $this->request->get['title_text_field'];
		} else {
			$title_text_field = null;
		}
		
		if (isset($this->request->get['max'])){
			$max = $this->request->get['max'];
		} else {
			$max = null;
		}
		
		if(isset($this->request->get['idp'])){ 
			$product = $this->request->get['idp'];
			$product2 = array();
			foreach($product as $p)
			{
				$product2[] = $p;
			}
 
		}else {
			$product = null;
		}
		
		
		// BREADCRUMBS //
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/item', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);
		// END //
		
		// Call Language //
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['entry_head'] = $this->language->get('entry_head');
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_max_item'] = $this->language->get('entry_max_item');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		// END //
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['action'] = $this->url->link('item/item/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['token'] = $this->session->data['token'];
	
		
		$data	=	array(
			'head_text_field'	=>	$head_text_field,
			'title_text_field'	=>	$title_text_field,
			'max'				=>	$max
		);
		
		$this->template = 'item/item.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
			
	}
	
	public function insert()
	{
		
		$this->language->load('item/item');
		
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('item/item');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			//$this->model_item_item->insert_head($this->request->post); // THIS ALREADY WORK WELL

	  		$this->model_item_item->insert_detail($this->request->post);
			// CHECK THIS OUT

		    //$this->session->data['success'] = $this->language->get('text_success');
			//$this->redirect($this->url->link('item/item', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}
	
	protected function validateForm() {
		if (!$this->user->hasPermission('modify', 'item/item')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

		if ((utf8_strlen($this->request->post['head_text_field']) < 1) || (utf8_strlen($this->request->post['head_text_field']) > 64)) {
      		$this->error['head'] = $this->language->get('error_head');
    	}
		
        if (!$this->request->post['title_text_field']) {
            $this->error['title'] = $this->language->get('error_title');
        }
					
    	if (!$this->error) {
			return true;
    	} else {
      		return false;
    	}
	}
And this for model :

Code: Select all

public function insert_head($data) // IGNORE THIS ALREADY WORK
	{
		$this->db->query("INSERT INTO " . DB_PREFIX . "show_product SET head_text = '" . $this->db->escape($data['head_text_field']) . "', title_text = '" . $this->db->escape($data['title_text_field']) . "', max_item = '" . $this->db->escape($data['max']) . "'");
	}
	
	public function insert_detail($product2)
	{
		foreach($product2 as $detail) // THIS THE PROBLEM
		{
			//$this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail SET product_id = '". $this->db->escape($detail['product']) . "'");
		}
		echo $detail[0];
	}
Last View (Autocomplete + Add Row Table)

Code: Select all

	<?php echo $header; ?>
	<div id="content">
	  <div class="breadcrumb">
		<?php foreach ($breadcrumbs as $breadcrumb) { ?>
		<?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
		<?php } ?>
	  </div>
	  <?php if ($error_warning) { ?>
	  <div class="warning"><?php echo $error_warning; ?></div>
	  <?php } ?>
	  <?php if ($success) { ?>
	  <div class="success"><?php echo $success; ?></div>
	  <?php } ?>
	  <div class="box">
		<div class="heading">
		  <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
		  <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
		</div>
		<div class="content">
		  <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
			<table class="form">
			<tr>
				<td><span class="required">*</span> <?php echo $entry_head; ?></td>
				<td><input type="text" name="head_text_field" value="" placeholder="Input Head Text" size="40"/>
					<?php if ($error_head) { ?>
						<span class="error"><?php echo $error_head; ?></span>
					<?php } ?>
				</td>
			</tr>
			<tr>
				<td><span class="required">*</span> <?php echo $entry_title; ?></td>
				<td><textarea name="title_text_field" placeholder="Input Title Text" style="width:230px;"/></textarea>
					<?php if ($error_title) { ?>
						<span class="error"><?php echo $error_title; ?></span>
					<?php } ?>
				</td>
			</tr>
			<tr>
				<td><?php echo $entry_max_item; ?></td>
				<td>
					<select name="max" id="maxitem">
					<?php
					for($i=1; $i<=6; $i++)
					{
						if($i == 1)
							echo "<option selected='selected' value=".$i.">".$i."</option>";
						else
							echo "<option value=".$i.">".$i."</option>";
					}
					?>
					</select>
				</td>
			</tr>
			<tr>
				<td><?php echo $entry_product; ?></td>
				<td><input type="text" id="product" name="product" value="" placeholder="Input Product" size="40"/></td>
				<td><input type="hidden" id="idp" name="idp" value="" /></td>
			</tr>
			<tr>
				<td></td>
				<td>
				<table>
					<tr>
						<td><input type="button" id="ADD" value="Add Item"></td>
						<td><input type="reset" id="RESET" value="Reset"></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				
			</tr>
			</table>

			<table border="1" id="tblname" cellpadding="5" cellspacing="5">
				<thead>
				<tr>
					<td>
						Total Item
					</td>
					<td>
						Id Item
					</td>
					<td>
						Name Item
					</td>
					<td>
						DELETE
					</td>
				<tr>
				</thead>
				<tbody align="center">
				</tbody>
			</table>
		  </form>
		</div>
	  </div>
	</div>
	<script type="text/javascript"><!--
	$('input[name=\'product\']').autocomplete({
		delay: 100,
		source: function(request, response) {
			$.ajax({
				url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
				dataType: 'json',
				success: function(json) {		
					response($.map(json, function(item) {
						return {
							label: item.name,
							value: item.product_id
						}
					}));
				}
			});
		}, 
		select: function(event, ui) {
			$('input[name=\'product\']').val(ui.item.label);
			$('input[name=\'idp\']').val(ui.item.value);
			return false;
		},
		
		focus: function(event, ui) {
			return false;
		}
	});

	//--></script>
	<Script type="text/javascript">
	$(document).ready(function(){
		var item = 1;
		var isAllowed	= 3;
		var isSet		= 0;
		$('#ADD').click(function(){
			var maxitem = parseInt($("#maxitem").val(), 10); //from max item in html
			var iCount = 0;
			if($('#product').val()){  // check input product
				if( item <= maxitem )
				{
					iCount = $('#tblname tbody tr').length + 1;
					szTr = "<tr><td>";
					szTr = szTr + iCount + "</td>";
					szTr = szTr +   "<td>" +$('#idp').val() +"</td>";
					szTr = szTr +   "<td>" +$('#product').val() +"</td>";
					szTr = szTr +   "<td><input type='button' class='DEL' value='DELETE'></td>";
					szTr = szTr +   "</tr>";                     
					$('#tblname tbody').append(szTr);
					item +=1;
					isSet = 1;
					restFormOpts();
				}
				else
				{
					alert ("Max Limit !!!");
				}
			}else{alert('Enter Product !!! ');}
		});

		// for delete row
		$('body').on('click','#RESET', function() {
			item		= 1;
			isAllowed	= 3;
			isSet		= 0;
			$("#maxitem").attr("disabled",false);
			$('.DEL').parents('tr').remove();
		});
		
		$('body').on('click', 'input.DEL', function() {
			$(this).parents('tr').remove();  
			item -= 1;
		});
		
		function restFormOpts()
		{
			if(isSet === isAllowed) {
				$("#ADD").attr("disabled",true);
				$("#maxitem").attr("disabled",false);
			}
			else {
				$("#ADD").attr("disabled",false);
				$("#maxitem").attr("disabled",true);
			}
		}
	});
	</script>
	<?php echo $footer; ?>
if i try echo $detail[0]in model result is 8, [1] 9, [2] 8 , [3] 9. what i hope is [0] 5656, [1] 8989,
Did i mistake in store input to array??? can some one help me write the correct code please? ???
Last edited by blackarch on Mon Feb 01, 2016 6:15 pm, edited 2 times in total.

New member

Posts

Joined
Thu Jan 07, 2016 6:00 pm

Post by pprmkr » Mon Feb 01, 2016 4:58 pm

How is the product_id entered ?
Is it a single input field, select or whatever ?
Do you only want to store product_id's in an database table? Whats the use?

If you want to build for OpenCart, use same structure:

Code: Select all

<?php

		// input values seperated by comma
		// input names: Detail: text, products: text
		$entered_values = array(
			'detail' => 'Details here ...',
			'products' => '5656,8585,9191'
			);

		// Entering controller with post values
		// Check if postvalues is set and valid

		if ($entered_values && validate($entered_values)) {			
			insertDetails($entered_values);		

			$success = '<br /><br />Values stored!';
		}
		
		if (isset($error)) {
			echo $error;
		}
		
		if (isset($success)) {
			echo $success;
		}
		
		// set variables and text to display on form
		// When values are entered, assign to variables
		
		// show form

	function validate($values) {
		$error = false;
		if (!$values) {
			$error = true;
			echo 'No values entered!<br />';
		}

		return !$error;
	}

	function insertDetails($data) {

		//$this->db->query("INSERT INTO `" . DB_PREFIX . "show_product_detail` SET detail = '" . $this->db->escape($data['detail']) . "'");
		echo $data['detail'] . '<br />';

		$product_detail_id = getLastId();
		$products = explode(',',$data['products']);
		foreach ($products as $product_id) {
			//$this->db->query("INSERT INTO " . DB_PREFIX . "show_product_detail_to_product SET product_detail_id = '" . (int)$product_detail_id . "', product_id = '" . (int)$product_id . "'");
			echo $product_detail_id . ' -> ' . $product_id . '<br />';
		}
	}
	
	function getLastId(){
		return 12;	
	}


?>
Gets all post values, when set, call model
Model stores 1 table with details and related table stores id's

User avatar
Active Member

Posts

Joined
Sat Jan 08, 2011 11:05 pm
Location - Netherlands

Post by blackarch » Mon Feb 01, 2016 5:46 pm

Sorry it's a part code i edit now, i try build sub menu in menu admin and build in folder is Item.php (admin/controller/item/item.php), view & model too in new folder item/item.php /item.tpl
i build 2 table in db for store this data 1 for(head(PK(show_product_id,AI), Head, Title, max_item), 1 for (detail (PK(AI),(show_product_id(FK)), id_product)) This just for related

I use add row table with button click(add item) and use autocomplete too for product and idp (id_product).
so user only input name product and id_product will be store to idp(hidden), check the view for make you sure
I make it's 2 time for input db 1 for head 1(head,title,dll) and 1 for detail(product_id only).

After that user click save for insert to database, for another field like max_item or title already work to insert db.
Last only product it's fail and stuck this problem for 1 week :-[ ,i really be thankful if you can help me :'(

I already try store variable to array but still fail. idkw when i try var_dump($product2) results is head,title,max_item,product_id(if you input twice only last result, the first will be gone.

New member

Posts

Joined
Thu Jan 07, 2016 6:00 pm

Post by blackarch » Wed Feb 03, 2016 5:12 pm

nvm i already fix it thx for trying help me

New member

Posts

Joined
Thu Jan 07, 2016 6:00 pm
Who is online

Users browsing this forum: No registered users and 2 guests