Magento: show admin product category in dropdown - magento

I want to show the category selection as a drop down instead of it rendering with a +sign
magento/admin/product/new product product category selection
See below image for reference.

Here is the code, You can just create bellow function under the block or helper with your module.
function getCategoriesTreeView() {
// Get category collection
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->addFieldToFilter('is_active', array('eq'=>'1'))
->load()
->toArray();
// Arrange categories in required array
$categoryList = array();
foreach ($categories as $catId => $category) {
if (isset($category['name'])) {
$categoryList[] = array(
'label' => $category['name'],
'level' =>$category['level'],
'value' => $catId
);
}
}
return $categoryList;
}
Now its time for design, bellow is the code which will give you the category drop down.
<select id="categorylist" name="categorylist">
<option value="">Select Category</option>
<?php
$categoriesTreeView = getCategoriesTreeView();
foreach($categoriesTreeView as $value)
{
$catName = $value['label'];
$catId = $value['value'];
$catLevel = $value['level'];
$space = ' ';
for($i=1; $i<$catLevel; $i++){
$space = $space." ";
}
$catName = $space.$catName;
?>
<option value="<?php echo $catIdIs; ?>"><?php echo $catName ?></option>
<?php
}
?>
</select>

Related

Laravel many to many relationship update

I have some hard time with many to many relationship when I try to update the pivot columns.
Here is my database 'order_product' table
order_product_table
First, I am trying to update the products of that order.
Order update form
Here is the HTML:
#if($products)
<select class="form-control kt-select2 products" name="products[]" required>
<option selected disabled>Select a product</option>
#foreach($products as $product)
<option value="{{ $product->id }}" data-price="{{ $product->selling_price }}" {{ $product->id === $order_product->id ? 'selected' : '' }}>{{ $product->name }}</option>
#endforeach
</select>
#endif
My spaghetti code...
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
if ($products) {
$order->products()->sync([$order->id => ['product_id' => $products]]);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
Try a lot of things, but it does not work ...
Edit your code like this:
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
//should be an array of products ID
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
foreach($products as $key => $productId) {
//normalize attributes
$attributes = [
'price' => $price[$key],
'quantity' => $quantity[$key],
'discount' => $discount[$key],
];
$order->products()->updateExistingPivot($productId, $attributes);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
For more information about updateExistingPivot() method, check Laravel documention
For the sync() method in many to many relation, you need to provide the relation ID as index of the array.
$syncable = [];
foreach ($products as $key => $productId) {
$syncable[$productId] = [
'price' => $attributes['price'][$key],
'quantity' => $attributes['quantity'][$key],
'discount' => $attributes['discount'][$key]
]
}
if ($syncable) {
$order->products()->sync($syncable);
}

How to autocomplete select option in CodeIgniter

I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>

How to make sure that foreach loop data shows in correct set position views

I have a controller that shows modules for my each position
Array
(
[0] => Array
(
[layout_module_id] => 1
[layout_id] => 1
[module_id] => 1
[position] => column_left
[sort_order] => 1
)
[1] => Array
(
[layout_module_id] => 2
[layout_id] => 1
[module_id] => 2
[position] => column_left
[sort_order] => 2
)
)
Above currently I have only two modules set and the are in the position of column left.
Because the position views are out side of the foreach loop they are picking up that module even though not set for that position? As shown in image.
Question: How can I make sure that the module will only display in its set position view.
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$modules = $this->getlayoutsmodule($layout_id);
echo '<pre>';
print_r($modules);
echo "</pre>";
$data['modules'] = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
// Position Views
$data['column_left'] = $this->load->view('column_left', $data, TRUE);
$data['column_right'] = $this->load->view('column_right', $data, TRUE);
$data['content_top'] = $this->load->view('content_top', $data, TRUE);
$data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE);
// Main view
$this->load->view('welcome_message', $data);
}
public function getlayoutsmodule($layout_id) {
$this->db->select('*');
$this->db->from('layouts_module');
$this->db->where('layout_id', $layout_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
Each of the position views have the same foreach loop
<?php if ($modules) { ?>
<?php foreach ($modules as $module) { ?>
<?php echo $module;?>
<?php } ?>
<?php }?>
main view
<div class="container">
<div class="row">
<?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
I would do it like this (not sure if the library needs to be loaded in each iteration but leaving it there):
$tmpdata = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$tmpdata[$module['position']]['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
$data = array();
foreach ($tmpdata as $pos => $mods) {
$data[$pos] = $this->load->view($pos, $tmpdata[$pos], TRUE);
}
$this->load->view('welcome_message', $data);
Passing $tmpdata[$pos] to each position's view makes the array called $modules available, so in the views, just use
if ($modules) {
foreach ($modules as $module) {
echo $module;
}
}
as before.
This keeps it dynamic and you don't have to modify this code if you change or add positions. I'm using two data arrays just to avoid passing unnecessary data.

Not displaying correct out put on view

I have two database tables called forum and forum_category.
In the forum table "General" and "Vegetables"
On the forum_categories there are two forum categories for General and one in Vegetables.
But for some reason my forum Vegetables is showing multiple categories should be only displaying one.
My question is how can I get the correct categories to display in
correct forum.
Note: It shows the correct results in the print_r image
Preview
View
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php foreach ($forums as $forum) {?>
<div class="panel panel-custom">
<div class="panel-heading"><h1 class="panel-title"><?php echo $forum['forum_name'];?></h1></div>
<div class="panel-body">
<?php foreach ($forum['categories'] as $category) {?>
<table class="table">
<tbody>
<tr>
<td><?php echo $category['category_name'];?></td>
</tr>
</tbody>
</table>
<?php }?>
</div>
</div><!-- Panel -->
<?php }?>
</div>
</div>
Model Functions
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
Printed Results
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories_results = $this->get_result_categories($result['forum_id']);
echo "<pre>";
print_r($categories_results);
echo "</pre>";
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
public function get_result_categories($forum_parent_id) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum_category');
$this->db->where('forum_parent_id', $forum_parent_id);
$query = $this->db->get();
return $query->result_array();
}
public function get_results() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'forum');
$query = $this->db->get();
return $query->result_array();
}
}
Think I have the solution now
Solution
Placing $categories = array();
above $categories_results did the trick
public function index()
{
$results = $this->get_results();
foreach ($results as $result) {
$categories = array();
$categories_results = $this->get_result_categories($result['forum_id']);
foreach ($categories_results as $category_result) {
$categories[] = array(
'category_name' => $category_result['category_name']
);
}
$data['forums'][] = array(
'forum_name' => $result['forum_name'],
'categories' => $categories
);
}
$data['content'] = 'common/home_view';
$this->load->view('theme/default/template_view', $data);
}
Image

create a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>

Resources