how can I fix the foreach with separate files? - php

how can I fix it the values from fields because when I write in url http://localhost/storeLTE/login/getmodules/1 everything works well, but when I want to pass them through my home with <?php $this->load->view('modules_view'); ?> it isnt work as separate files. how can I fix it?
controller
public function getModules($id_module){
if($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion){
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
$seccions[$index]['data']= $modules;
if (!count($seccions[$index]['data']))
unset($seccions[$index]);
}
foreach ($seccions as $item)
array_push($menu,$item);
$this->data['fields'] = $menu;
$this->load->view('modules_view',$this->data);
}
}
view
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php $this->load->view('modules_view'); ?>
</div>
</div>
</div>
view_modules
<?php $this->load->view('header'); ?>
<div class="module_item">
<?php foreach ($fields as $session) : ?>
<?php foreach ($session['data'] as $itemData) : ?>
<div class="module_item" title="<?= $itemData['value'] ?>">
<img src="<?= $itemData['icon'] ?>"/>
<?= $itemData['value']?>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>

Try changing the following line:
<?php $this->load->view('modules_view'); ?>
to:
<?php $this->load->view('modules_view', ['fields' => $fields]); ?>
This should give modules_view.php access to your $fields variable from the controller. Hope this helps!

Related

Subcategory inside category in the Codeigniter framework

I want to show subcategory inside the main category. My table looks like this. but when i print_r($cat_id) it returns only the last id of the category. How can I fix this? Here is my controller:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
foreach($data['categories'] as $ct){
$cat_id = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_id);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
}
and here is my model:
public function get_all_categories_for_courses() {
$query = $this->db->get_where('categories', array('parent_id' => NULL));
return $query->result_array();
}
function get_all_subcategories_for_courses($cat_id) {
$query = $this->db->get_where('categories', array('parent_id' => $cat_id));
return $query->result_array();
}
and here is my view page:
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="#" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php foreach($subcategories as $subcat): ?>
<li><?= $subcat['name_rus']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
Thanks in advance!
Or you can do this
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="<?= site_url('/courses/category/'.$cat['category_slug']); ?>" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php
$query = $this->db->get_where('categories', array('parent_id' => $cat['id']));
?>
<?php foreach($query->result() as $subcat): ?>
<li><?= $subcat->name_rus; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
As Ukasyah said in a comment on your question, you're overwriting the $cat_id value each time you loop through your categories.
What you will need to do is get an array populated with the IDs and use $this->db->where_in() to match an array rather than a single value.
Your controller will need to look more like this:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
$cat_ids = [];
foreach($data['categories'] as $ct){
$cat_ids[] = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_ids);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
While you will need to change your model function to look like this:
function get_all_subcategories_for_courses($cat_ids) {
$query = $this->db->where_in('parent_id', $cat_ids)->get('categories');
return $query->result_array();
}
Best of luck.

what am i wrong with my arrays?

I want to make a menu list based on roles, but when I want to display them to my home view it doesnt show up the list.. how can I fix it? the problem is that the menu list is not showing with separates files.
controller
public function getModules($id_module){
if($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion){
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
$seccions[$index]['data']= $modules;
if (!count($seccions[$index]['data']))
unset($seccions[$index]);
}
foreach ($seccions as $item)
array_push($menu,$item);
$this->data['fields'] = $menu;
$this->load->view('modules_view',$this->data);
}
}
model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
return $this->db->get()->result_array();
}
public function query($query){
return $this->db->query($query)->result_array();
}
view
home.php
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php $this->load->view('modules_view'); ?>
</div>
</div>
</div>
<!-- Main row -->
<div class="row">
<!-- Left col -->
<section class="col-lg-12 connectedSortable">
</section>
</div>
</section>
modules
<?php $this->load->view('header'); ?>
<div class="module_item">
<?php foreach ($fields as $session) : ?>
<?php foreach ($session['data'] as $itemData) : ?>
<div class="module_item" title="<?= $itemData['value'] ?>">
<img src="<?= $itemData['icon'] ?>"/>
<?= $itemData['value']?>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>
try this at your model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
$this->db->get();
return $query->result();
}

What is the best way to rewrite this repeated if statement?

I'm thinking there is an easier way to write this code, but not sure on what approach to take. Basically, I want to check if each variable exists, and if so - add the appropriate markup to the page.
Any suggestions would be great.
<?php
$words = get_field('words');
$photography = get_field('photography');
$architect = get_field('architect');
?>
<div class="panel">
<?php if( $words ): ?>
<div>
<p><span>Words</span><span><?php echo $words;?></span></p>
</div>
<?php endif ;?>
<?php if( $photography ): ?>
<div>
<p><span>Photography</span><span><?php echo $photography;?></span></p>
</div>
<?php endif; ?>
<?php if( $architect ): ?>
<div>
<p><span>Architect</span><span><?php echo $architect;?></span></p>
</div>
<?php endif; ?>
</div>
You can use array & loop -
<?php
$fields = array();
$fields['words'] = get_field('words');
$fields['photography'] = get_field('photography');
$fields['architect'] = get_field('architect');
?>
<div class="panel">
<?php foreach($fields as $key => $value):
if($value)
?>
<div>
<p><span><?php echo ucwords($key);?></span><span><?php echo $value;?></span></p>
</div>
<?php
endif;
endforeach;?>
</div>
Create a field array with their appropriate label if you want to have your own labels.
<div class="panel">
<?php
$fields = array (
'words' => 'Words',
'photography' => 'Photography',
'architect' => 'Architect'
);
foreach ( $fields as $field => $label ) {
$value = get_field ( $field );
if (!empty($value)) {
echo '<div>
<p><span>' . $label . '</span><span>' . $value . '</span></p>
</div>';
}
}
?>
</div>
<?php
$required_fields = array("words","photography","architect");
$data = array();
foreach($required_fields as $field)
{
$data[$field] = get_field($field);
}
?>
<div class="panel">
<?php foreach($data as $data_field=>$data_value):
if($data_value)
?>
<div>
<p><span><?=$data_field?></span><span><?=$data_value?></span></p>
</div>
<?php
endif;
endforeach ;?>
</div>
You can create a function like this and then iterate the data that it returns to output your content
<?php
//define a function to fetch data in specific fields
function fetch_data($keys=array()){
$data = array();
foreach($keys as $key){
$val = get_field($key);
if(!empty($val)){
$data[$key] = $val;
}
}
return $data;
}
//now you can easily add or change your fields
$data = fetch_data(array('words','photography','architect'));
?>
<div class="panel">
<?php
foreach($data as $field){
if(isset($data[$field])){
$val = $data[$field];
?>
<div>
<p><span><?php echo ucfirst($field); ?></span><span><?php echo $val; ?></span></p>
</div>
<?php
}
}
?>

Magento: Exclude a specific category from dropdown menu

I am trying to exclude one specific category from a random menu I have. I have searched for answers and have found some potential solutions on here, but none of them worked.
This is what I currently have:
<?php
$catID = $this->category_id;
$catName = $this->category_name;
$catType = $this->category_type;
$category = Mage::getModel('catalog/category')->load($catID);
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail', 'description'))
->addAttributeToFilter('is_active', 1)
->addIdFilter( $category->getChildren() );
$catIdEx = 38;
$categories->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id');
$categories->getSelect()->where('cats.category_id', array('neq' => $catIdEx));
$categories->getSelect()->group(array('e.entity_id'));
$categories->getSelect()->order('RAND()');
$categories->getSelect()->limit(1);
?>
<?php foreach ($categories as $category): ?>
<div>
<a href="<?php echo $category->getUrl(); ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" class="boxedimage" />
</a>
<br />
<h3>Spotlight <?php echo $catName ?></h3>
<?php
$sdesc = $category->getDescription();
$sdesc = trim($sdesc);
$limit = 230;
if (strlen($sdesc) > $limit) {
$sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
}
?>
<?php echo $sdesc."..."; ?>
<div>View <?php echo $category->getName(); ?> <?php echo $catType ?>...</div>
</div>
<?php endforeach; ?>
Your collection call looks pretty over the top for reasons im sure make sense for you, so I'll go with this - if you just want to exclude that category, add this to the foreach;
<?php foreach ($categories as $category): ?>
<?php if($category->getId() !== $catIdEx) { ?>
<div>
<!-- your other code here -->
</div>
<?php } ?>
<?php endforeach; ?>
$excluded_category = array('20','4','6');
<?php foreach ($categories as $category): ?>
<?php if(!in_array($category->getId(),$excluded_category)) { ?>
<div>
<!-- your other code here -->
</div>
<?php } ?>
<?php endforeach; ?>

Unable to apply foreach loop properly in PHP

I want two posts at each slide. but getting only one slide. I am new in programming, please help me.
$widget_id = $widget->id.'-'.uniqid();
$settings = $widget->settings;
$navigation = array();
$captions = array();
$i = 0;
?>
<div id="slideshow-<?php echo $widget_id; ?>" class="wk-slideshow wk-slideshow-revista-articles" data-widgetkit="slideshow" data-options='<?php echo json_encode($settings); ?>'>
<div>
<ul class="slides">
<?php foreach ($widget->items as $key => $item) : ?>
<?php
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php $i=$i+1;?>
<?php endforeach; ?>
</ul>
<?php if ($settings['buttons']): ?><div class="next"></div><div class="prev"></div><?php endif; ?>
<?php echo ($settings['navigation'] && count($navigation)) ? '<ul class="nav">'.implode('', $navigation).'</ul>' : '';?>
<div class="caption"></div><ul class="captions"><?php echo implode('', $captions);?></ul>
</div>
</div>
http://i.stack.imgur.com/sy1ih.png
you're missing the { after the foreach and at the end of the loop.
<?php foreach ($widget->items as $key => $item) {
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php
$i=$i+1;
}
?>
Your foreach syntax looks fine. That syntax is a lot easier for some people to read when embedded in HTML than the traditional braces.
Can I ask what the $this variable refers to? I don't see this instantiated anywhere in your code? Is it supposed to be $item instead?
$settings['index'] never changes within the loop, however $i does.
Change to: ($i<2)

Categories