Pass array to view in Codeigniter - php

I have a breadcrumb that i send to my view pages, but the part that have an array not load, just static code.
I think the problem it's in variable inside array, because i have another breadcrumbs with same problem, the static is loaded.
Example breadcrumb that I want: Home -> Product -> "Tshirt name"
Controller:
$itensBreadCrumb["itensBreadCrumb"] = array(
"Home" => "home",
"Produtos" => "produtos", // after this don't load in view.
strtolower($produto->nome) => ""
);
$dataProdutos['produto'] = $produto;
$dataProdutos['estoques'] = $this->produtos_model->retornaEstoqueProduto($produto[0]->id);
$dataProdutos['breadcrumb'] = $this->load->view("breadcrumb", $itensBreadCrumb, TRUE);
Breadcrumb structure View:
<div class="breadcrumb">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php foreach ($itensBreadCrumb as $item => $link) {
if(!empty($link)){
?>
<?=$item?>
<?php }else{ ?>
<span class="item-bread"><?=$item?></span>
<?php
}
} ?>
</div>
</div>
</div>
How i'm calling breadcrumb in pages.
<?= $breadcrumb ?>

Related

Add cart without reloading

I tried adding a cart in my application, but how you can while in order to add a new cart page does not reload, but the cart increases.
my controllers
function add_cart($code) {
$produk = $this->Model->get_id($code);
$data = array(
'id' => $produk->code
'qty' => 1,
'name' => $produk->name );
$this->cart->insert($data);
}
Model
function get_id($code) {
$query = $this->db->select('*')
->from('mytable')
->where('code', $code)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<?php foreach($product as $row) ?>
<div class="col-sm-4">
<div class="col-item">
<div class="info">
<div class="row">
<div class="price col-md-12">
<h5> <?php echo $row->name_product ;?></h5>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add">
class="hidden-sm" href="<?php echo site_url('home/add_cart/'.$row->code);?>">Add</a></p>
</div>
</div>
</div>
</div>
<?php endforeach ?>
Use ajax to replace the href to request the add_chart operation, and get the response to refresh the page content without reload the page.

Masonry, WordPress Loop, and Bootstrap

I am building a wordpress site using bootstrap, and I want to create a featured work section on my homepage using masonry to display the thumbnails and titles of my 3 most recent portfolio items.
I would like the portfolio items to be placed in a seemingly random fashion (similar to this: http://studiosweep.com/) rather than just an orderly grid format. I can't figure out how to assign different widths to my portfolio items because they're just being generated in the featured work section via wordpress loop.
Here's my HTML:
<section id="work">
<div class="container-fluid">
<div class="row">
<div class="col-sm-offset-6 col-sm-6 text-center">
<h1>Featured Work</h1>
</div>
</div>
<div class="row" id="ms-container">
<?php query_posts('posts_per_page=3&post_type=work'); ?>
<?php while ( have_posts() ) : the_post();
$thumbnail = get_field('thumbnail');
$medium = get_field('medium');
$size = "large";
?>
<div class="ms-item col-lg-6 col-md-6 col-sm-6 col-xs-12">
<figure>
<?php echo wp_get_attachment_image($thumbnail, $size); ?>
</figure>
<h3><?php the_title(); ?></h3>
<div class="clearfix"></div>
</div>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query();
</div>
<div class="clearfix"></div>
</div>
</section>
Here's the script:
<script type="text/javascript">
jQuery(window).load(function() {
// MASSONRY Without jquery
var container = document.querySelector('#ms-container');
var msnry = new Masonry( container, {
itemSelector: '.ms-item',
columnWidth: '.ms-item',
});
});
</script>
As for CSS, I don't really know how to go about assigning the different column widths, so I only have this so far:
.ms-item {
width: 38.23%;
margin-right: 80px;
margin-bottom: 70px;
}
Any help would be appreciated!
Let's say you have 3 different classes for column widths:
.ms-item-width-1, .ms-item-width-2, .ms-item-width-3
A possible solution is to add those 3 classes to your css file and randomly assign one of your classes to your container after .ms-item class. Masonry will give the width of the last class you added to that container. For example:
<div class="ms-item <?php echo 'ms-item-width-' . (rand(0, 3) + 1); ?> col-lg-6 col-md-6 col-sm-6 col-xs-12">
<figure>
<?php echo wp_get_attachment_image($thumbnail, $size); ?>
</figure>
Update: To have a randomized without getting repeated values you could have an array $widths = array(1,2,3); and then shuffle this array shuffle($widths); and instead of calling the random function you would be removing the elements of the array and replace it with the following code:
<div class="ms-item <?php echo 'ms-item-width-' . array_shift($widths); ?> col-lg-6 col-md-6 col-sm-6 col-xs-12">
This will provide an unique width to this 3 items.

Theming of custom block is not working

I've been following this post
http://legomenon.io/article/drupal-7-creating-theming-custom-block-content
so i made them accordingly,
i have these, btw name my my custom module is efq_story_list
HOOK_THEME
function efq_story_list_theme() {
return array(
'efq_story_list_function_items' => array(
'variables' => array('items' => NULL),
'template' => 'templates/efq_story_list--block'
),
);
}
FUNCTION GENERATING THE LIST
function __efq_story_list_block_content() {
#SOME CODE HERE and variable $items has the list
return theme("efq_story_list_function_items", array("items" => $items));
}
HOOK_BLOCK_VIEW
The name of the block is opinion
function efq_story_list_block_view($delta = ''){
switch ($delta) {
case 'opinion':
$block['content'] = __efq_story_list_block_content();
break;
}
return $block;
}
Then on my templates/efq_story_list--block i have the file efq_story_list--block.tpl.php.. then i flush all the cache,
but now I get WSOD. I tried this in local machine and its working, so I am wondering what else can I miss in my production site.. Thank you very much.. I am quite new to drupal so I dont know where to look anymore...
EDIT # 1
the content of the template file is
<?php $items = $variables['items']; ?>
<div class="row">
<?php foreach($items as $item): ?>
<div class="col-md-3 news_list panel">
<div class='thumbnail_wrapper'>
<img src="print render($item['image_url']);" />
</div>
<div class="meta small_sprite">
<span class="generic icon source_<?php print render($item['source_class']); ?>"></span>
<span><?php print render($item['source']); ?></span>
</div>
<div class="caption">
<?php print render($item['title']); ?>
</div>
</div>
<?php endforeach;
</div>

Selected option in dropdownlist in yii

How can I have a default selected option in a dropdownlist in Yii? And then when the user enter my view the data would be displayed automatically?For example I pass some months via an action January, February etc and I want to have preselected the current month and when the user enters my view he should see the data from the current month? I think I have to play with javascript but I'm having trouble with the syntax because I'm fairly new to this.
This is my view:
<?php
?>
<div class="panel panel-primary">
<div class="panel-heading">
<?php
echo $schoolclass->description;
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<?php
$presentsData = CHtml::listData($presentmonths, 'presentmonth','presentmonth');
echo CHtml::dropDownList('presentmonthlist', 'presentmonth', $presentsData,
array(
'ajax'=>array('type'=>'POST',
'url'=>Yii::app()->createUrl('present/viewmonthlypresents',array('id'=>$schoolclass->schoolclassid)),
'update'=>'#mypresents',
'data'=>array('presentmonth'=>'js:this.value')
)
));
?>
</div>
</div>
<div id="mypresents">
</div>
<div class="panel panel-default">
<div class="panel-body">
<?php echo CHtml::button(Yii::t('default' ,'Submit Presents'), array(
'submit'=>array('create', 'id'=>$schoolclass->schoolclassid)
)
);
?>
</div>
</div>
you can set default selected option like this, for example i used 'January'
echo CHtml::dropDownList('presentmonthlist', 'January', $presentsData,
array(
'ajax'=>array('type'=>'POST',
'url'=>Yii::app()->createUrl('present/viewmonthlypresents',array('id'=>$schoolclass->schoolclassid)),
'update'=>'#mypresents',
'data'=>array('presentmonth'=>'js:this.value')
)
));

Creating Html Element from multidimensional Array

Not sure how will i able to explain this, but here we go
I have an array as below:
$arr = array(
array('id' => 1,
'content' => '<h1>This is a simple Content</h1>',
'vars' => null,
'path' => 'holders/row.php',
'sub-page' => array(
'id' => 2,
'content' => '<h1>Sample Body Here</h1>',
'vars' => '{"title":"Sample Title Here"}',
'path' => 'holders/box.php',
'sub-page' => array(
'id' => 3,
'vars' => '{"title":"Sample Title NUmber 3 Here"}',
'content' => '<h1>Again Sample Body Here</h1>',
'path' => 'holders/box.php'
)
)
)
);
What i am looking for is
array should go to the deepest sub-page and json_decode the vars key and extract as variables and include the file.
Once this is done, the output is passed to the parent's content key and parse it again
This is basically for creating dynamic content
the holders/row.php contains:
<div class="row">
<?= $content; ?>
</div>
and holders/box.php contains:
<div class="box lite">
<div class="box-title">
<h4><?= $title; ?></h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<?= $content; ?>
</div>
</div>
and the desired output is:
<div class="row">
<div class="box lite">
<div class="box-title">
<h4>Sample Title Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
<div class="box lite">
<div class="box-title">
<h4>Sample Title NUmber 3 Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
</div>
</div>
</div>
</div>
</div>
What have i tried so far
I am trying to loop around the array as below:
foreach ($arr as &$page) {
if (isset($page[$keyToParse])) {
$subMenu = $page[$keyToParse];
unset($page[$keyToParse]);
$page['content'] = $page['content'] . $this->parsePage($keyToParse, $subMenu);
return $page['content'];
} else {
$params = strlen($page['vars']) > 0 ? json_decode($page['vars'], true) : [];
$elementPath = $page['path'];
$params = array_merge($params, array('content' => $page['content']));
$page['content'] = callback(function () use ($params, $elementPath) {
extract($params);
include($elementPath);
});
return $page['content'];
}
and the function callback is as:
function callback($userfunc)
{
ob_start();
$userfunc();
$return = ob_get_contents();
ob_end_clean();
return $return;
}
Please help as i am stuck for a long time now
All you should have to do is something like this.
function renderNode($node){
ob_start();
if (isset($node["sub-page"]) && $node["sub-page"]){
$node["content"] = renderNode($node["sub-page"]);
}
extract($node);
include ($node["path"]);
return ob_get_clean();
}
and then invoke it like:
echo renderNode($arr[0]);

Categories