Separate php Array from main site - php

So, I have a page to display products which I named apple.php :
<?php
foreach($apple as $item) {
?>
<div class="col-lg-4 col-sm-4 col-xs-12 text-xs-center">
<div class="view third-effect">
<a href="<?php echo $item['link'] ?>">
<div class="overlay-thing"><i class="fa fa-share" aria-hidden="true"></i></div>
<img src="<?php echo $item['image'] ?>" />
</a>
</div>
<p><?php echo $item['name'] ?> </p>
</div>
<?php
}
?>
I use an hardcoded array which looks like this:
$apple = array(
array(
'id' => '1',
'name' => 'iPhone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'images/iphone4.png'
),
array(
'id' => '2',
'name' => 'iPhone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'images/iphone4s.png'
)
);
etc etc...
It has around 10 products.
This array is on top on the file, and I wonder how can I separate this array into an separate file, calling it for example products.php.
How can i then access that array in the file apple.php for the foreach loop.
Also, I will have arrays for samsung, sony products and should I put them all into the same file?

You could use include.
i.e. :
product.php
$apple = array(
array(
'id' => '1',
'name' => 'iPhone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'images/iphone4.png'
),
array(
'id' => '2',
'name' => 'iPhone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'images/iphone4s.png'
)
);
apple.php
<?php
include 'product.php';
foreach($apple as $item) {
?>
<div class="col-lg-4 col-sm-4 col-xs-12 text-xs-center">
<div class="view third-effect">
<a href="<?php echo $item['link'] ?>">
<div class="overlay-thing"><i class="fa fa-share" aria-hidden="true"></i></div>
<img src="<?php echo $item['image'] ?>" />
</a>
</div>
<p><?php echo $item['name'] ?> </p>
</div>
<?php
}
?>
Hope it helps.

Create a file apple-products.php or any name and add following lines:
<?php // apple-list.php
return array(
array(
'id' => '1',
'name' => 'iphone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'image/iphone4.png',
),
array(
'id' => '2',
'name' => 'iphone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'image/iphone4s.png',
),
);
Now in the top of apple.php file replace your array with this:
$apple = require('apple-products.php'); // assuming apple.php and apple-products.php are in same directory
That's it. Hope that helped!

Related

Trying to loop through an array key and display it

Array:
$forms = array (
'title' => 'Form Title',
'subtext' => 'Sub Text',
'fields' => [
[
'name' => 'Question A',
'type' => 'input',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
],
[
'name' => 'Question B',
'type' => 'textarea',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
]
]
);
Code:
<?php
$forms_keys = array_keys($forms['fields']);
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Im trying to get the fields => name to display inside the label. I tried the above but cant get it working, When I echo out $forms_key, I get "0" and "1"
From PHP Manual:
array_keys — Return all the keys or a subset of the keys of an array
You have an array of keys but no associated data as you've stripped this out. Remove array_keys from your first line:
<?php
$forms_keys = $forms['fields'];
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Your $forms['fields'] array does not have keys, it contains two array elements which contains keys for their inner elements. You can check them with $forms["fields"][0]["name"] or $forms["fields"][1]["name"].
Remove the array_key and set it as below and fields is a sub array. You need to take of it also.
<?php
$forms_keys = $forms['fields'];
for ($i = 0; $i < count($forms_keys); $i++) {
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_keys[$i]['name']; ?></label>
</div>
<?php
}
?>

Dropdown menus in MVC approach

I have been trying to understand this existing php code that displays drop down menus with their subsequent menus. while the code works in one level, i am having a hard time how to or what to add another level of the dropdown... i have been able to do that but in this case where php is used to display these fields, it's kind of mind-boggling for me... here's the existing code in the view side -->
<ul class="nav navbar-nav">
<?php
foreach ($nav as $item) {
if (empty($item['children'])) {
?>
<li<?php if (!empty($item['is_active'])) { ?> class="active"<?php } ?>>
<a href="<?php echo $item['link']; ?>"><?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>" data-toggle="tooltip" data-placement="bottom" data-original-title="A large tooltip."></i> <?php } echo $item['label']; ?></a></li>
<?php
}
else
{
?>
<li class="dropdown <?php if (!empty($item['is_active'])) { ?> active<?php } ?>">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">
<?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>"></i>
<?php echo $item['label']; ?>
<b class="caret"></b><?php } ?>
</a>
<ul class="dropdown-menu">
<?php
_main_menu_widget_display_children($item['children']);
?>
</ul>
</li>
<?php
}
}
?>
and the controller looks like this -->
$nav['settings'] = array('label' => 'Settings', 'icon' => 'fa fa-wrench fa-lg', );
$nav['settings/access_rights'] = array('label' => 'Approver', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('admin/access_rights'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_1'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/billing_param'] = array('label' => 'Technician', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('billing_param'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_2'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/support_settings'] = array('label' => 'Support', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('support_settings'), 'parent_id' => 'settings');
//sub-menu sub of Support
$nav['example1'] = array('label' => 'Request Supportt', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example2'] = array('label' => 'Need Features', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example3'] = array('label' => 'Telephone Number', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example4'] = array('label' => 'Testimonials', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example6'] = array('label' => 'Known Issues', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example5'] = array('label' => 'Road Map', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
here's what it looks like :
i just wanted those list under 'support' not shown when i clicked the 'settings' menu but rather after i hover or clicked (it doesnt matter really) the 'support' sub-menu...
i know there's something missing in the 'view' side but i dont know what is... another if-else maybe but i cant see any connection so far... any help is very much appreciated thanks... :)

Yii's infinite-scroll-pager extension doesn't show pager

I am using the Yii's extension infinite-scroll-pager. My data provider has 6 registers, but I want it to show only the first 3. How can I get it done? Would the triggerPageTreshold (sic) have to do this job?
$listView = $this->widget(
'zii.widgets.CListView',
array(
'id' => 'activities-scroll-list',
'dataProvider' => $dataProvider,
'itemView' => '/experience/_experience_item',
'cssFile' => null,
'template' => $this->renderPartial(
'/experience/_experiences_list',
array(),
true
),
'pager' => array(
'id' => 'pager-id',
'class' => 'ext.infiniteScroll.IasPager',
'rowSelector' => '.row',
'listViewId' => 'activities-scroll-list',
'header' => 'TESTE',
'loaderText' => 'Loading...',
'options' => array(
'history' => true,
'triggerPageTreshold' => 3,
'trigger' => 'Load more'
)
),
)
);
The /experience/_experiences_list:
<div class="row">
<div id="home-experience-resume" class="wrapper-k">
<?php
echo '{summary} {items} {pager}';
?>
</div>
</div>
The /experience/_experience_item:
<article class="column-3 <?= ((($index - 1) % 3) == 0 ? 'middle' : ''); ?> equal-height">
<div class="inner-wrapper">
<div class="img-container">
<img src="/images/tmp_thumbExperiencia1.jpg" alt="<?php echo Yii::t('home','Experiência 1'); ?>" />
</div>
<div class="info-column-container">
<div class="info-column">
<p class="more-detail-link">
<img src="/images/experiences/iconLocal.png" alt="icon local"/>
<?= $data->showingCity; ?>
</p>
<h3><?= $data->name; ?></h3>
<a class="button bt-small" href="/experience/info/<?php echo $data->id; ?>" target="_self"><?= Yii::t('home','Saber mais'); ?></a>
</div>
</div>
</div>
</article>
Use:
'template' => '<div class="row">div id="home-experience-resume" class="wrapper-k"> {summary} {items} {pager} </div></div>',

CGridView styling specific column, inserting <div> inside <td> elements

I’m trying to apply custom styling to specific columns generated by CGridView. I need to insert inside the elements, but I can’t figure out how to do it.
Here is the view file:
<div id="shortcodes" class="page">
<div class="container">
<!-- Title Page -->
<div class="row">
<div class="span12">
<div class="title-page">
<h2 class="title">Available Products</h2>
</div>
</div>
</div>
<!-- End Title Page -->
<!-- Start Product Section -->
<div class="row">
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'products-grid',
'dataProvider' => $dataProvider,
'ajaxUpdate' => TRUE,
'pager' => array(
'header' => '',
'cssFile' => false,
'maxButtonCount' => 25,
'selectedPageCssClass' => 'active',
'hiddenPageCssClass' => 'disabled',
'firstPageCssClass' => 'previous',
'lastPageCssClass' => 'next',
'firstPageLabel' => '<<',
'lastPageLabel' => '>>',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
),
'columns' => array(
'id',
'name',
'category',
'brand',
'weight_unit',
'price_unit',
'flavors',
array(
'name' => 'providers',
'htmlOptions' => array('class' => 'label label-info'),
),
),
));
?>
</div>
<!-- End Product Section -->
</div>
</div>
So I want to style the column named “providers”. Using htmlOptions does add the class to the td column, but the alignment of the column is getting out of place. That’s why I wanted to wrap it inside a <div>, to apply the proper alignments and styling.
Right now it appears like this:
<tr class="even">
<td>414</td>
<td>Owl Book</td>
<td>Night Owl Trance</td>
<td>Binaural Beats</td>
<td> 400 Grams</td>
<td>$569.66</td>
<td>Ether</td>
<td class="label label-info">Shrooms.com</td>
</tr>
What I was looking to do is this:
<tr class="even">
<td>414</td>
<td>Owl Book</td>
<td>Night Owl Trance</td>
<td>Binaural Beats</td>
<td> 400 Grams</td>
<td>$569.66</td>
<td>Ether</td>
<td><div class="label label-info">Shrooms.com</div></td>
</tr>
So, how would I do this? Is there an option for it? As far as I know, htmlOptions will only add attributes to the element.
Not tested, but I think you can use something like this.
'flavors',
array(
'name' => 'providers',
'value' => "<div class=label label-info>$data->providers</div>"
),
Ok, i first tried redGREENblue's answer, but i gives a Undefined variable: data
'value' => ''.$data->providers.'',.
Then after some advice in the Yii forums i found the solution. Basically what i did was return the value from within a function closure, and it worked,
The working code ::
<div id="shortcodes" class="page">
<div class="container">
<!-- Title Page -->
<div class="row">
<div class="span12">
<div class="title-page">
<h2 class="title">Available Products</h2>
</div>
</div>
</div>
<!-- End Title Page -->
<!-- Start Product Section -->
<div class="row">
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'products-grid',
'dataProvider' => $dataProvider,
'filter' => $dataProvider->model,
'ajaxUpdate' => TRUE,
'pager' => array(
'header' => '',
'cssFile' => false,
'maxButtonCount' => 25,
'selectedPageCssClass' => 'active',
'hiddenPageCssClass' => 'disabled',
'firstPageCssClass' => 'previous',
'lastPageCssClass' => 'next',
'firstPageLabel' => '<<',
'lastPageLabel' => '>>',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
),
'columns' => array(
'id',
'name',
'category',
'brand',
'weight_unit',
'price_unit',
'flavors',
array(
'name' => 'providers',
'value' => function($data) { //*the closure that works*
return '<div class="label label-info">'.$data->providers.'</div>';
},
'type' => 'raw',
),
),
));
?>
</div>
<!-- End Product Section -->
</div>

Create a label/value (readonly) fieldset using zend framework 2

I just want to create a Label/value row like this:
<label>
<span>My Label:</span><span>My value from my object</span>
</label>
So what should I put in the type attribute ?
$this->add(array (
'name' => 'myFieldName',
'attributes' => array(
'type' => '???????',
),
'options' => array (
'label' => 'My Label:',
),
));
$this->add(array (
'name' => 'myFieldName',
'type' => 'hidden',
'options' => array (
'label' => 'My Label:',
),
));
And in view
<?php $element = $form->get('myFieldName') ?>
<label>
<span><?php echo $element->getLabel() ?></span>
<span><?php echo $this->escapeHtml($element->getValue()) ?></span>
</label>

Categories