foreach loop in codeigniter - in controller or view? - codeigniter

Im learning CodeIgniter and trying to follow best practices.
I was wondering where is the best place to execute a foreach loop?
Lets say I have a Model where I query the database and return all the results.
Then should I execute foreach in the Controller? Or rather View? Why?

It depends what you're trying to do. If you're looping through results and outputting HTML, then I would recommend you do that in the view. If you're looping through results to do something to your data then outputting that to the view, then that logic would belong in the controller (or model).
I try to keep my views as PHP-free as possible, so when I have to loop through I tend to use alternate syntax for the loop. Here's an example:
<?php foreach ($x as $y) : ?>
<div>
<h2><?php echo $y['title']; ?></h2>
<p><?php echo $y['content']; ?></p>
</div>
<?php endforeach; ?>
The above ensures that should someone that only knows frontend development need to tweak the view, it's very clear what's happening there.

Related

Page loading taking too much time

i have joomla view where i am including 4 more sub template views
<?php echo $this->loadTemplate('Market_1'); ?>
<?php echo $this->loadTemplate('Market_2'); ?>
<?php echo $this->loadTemplate('Market_3'); ?>
<?php echo $this->loadTemplate('Market_4'); ?>
and every template file taking too much time to load because every file displays 15 tables with text and images...
and page load when all templates loaded and its taking too much time is there any way to load some fast? or first load 'Market_1' and others one by one and page open in quick time?
First. I cannot think of any reason why you would want to display that much data at once that it actually "overloads" the page.
But to address your question. No. In php you cannot asynchronous load the templates. To do that you should implement some kind of Ajax (javascript).
But again... You really need to reconsider your solution imo.

Joomla Registration form Fields Ordering

How do I order the fields on my joomla registration form?
As you can see, the captcha must be at the bottom:
http://www.celinasoft.com/index.php/component/users/?view=registration
First of all you will need to create your Registration view template override (to keep it Joomla update proof). To do so, create folder /templates/YOUT_TEMPLATE/html/com_users/registration and copy /components/com_users/views/registration/tmpl/default.php file there.
Then you will need to modify the code itself, open /templates/YOUT_TEMPLATE/html/com_users/registration/default.php file, you will see, that by default Joomla takes ALL fields from registration.xml file and just loops them through foreach cycle, this code:
<?php foreach ($this->form->getFieldsets() as $fieldset): ?>
...
...
<?php endforeach; ?>
You need to replace this cycle by your manual fields output like this:
<div class="control-group">
<div class="control-label"><?php echo $this->form->getLabel('email'); ?></div>
<div class="controls"><?php echo $this->form->getInput('email'); ?></div>
</div>
...
...
And output all desired registration fields like that, in order you prefer. Warning: don't forget that almost all of that fields are required for registration and cannot be missed.
You can find all field names in XML file components/com_users/models/forms/registration.xml
Please note that this is only sample code, to understand the logics. It haven't been tested live. So if errors occur - please let me know and we'll fix it :)
If anyone have a better/easier solution - I'd be happy to hear it.

How do I manually theme Views in Drupal 7?

I am new to drupal and I am trying to figure out how to theme Views. I currently have a content type called Category with the following fields:Title, Image and Body. I created a view for the above mentioned content type so that I would list view of all the categories I have created.
To custom theme the view I created a folder called views in my theme folder, and created the following view files:
views-view-fields--plugin-categories.tpl.php
views-view--plugin-categories.tpl.php
views-view-unformatted--plugin-categories.tpl.php
This is what I currently have in my first file:
<div class="<?php print $classes; ?>">
<?php if ($rows): ?>
<div class="view-content">
<?php print $rows; ?>
</div>
<?php elseif ($empty): ?>
<div class="view-empty">
<?php print $empty; ?>
</div>
<?php endif; ?>
<?php if ($more): ?>
<?php print $more; ?>
<?php endif; ?>
</div><?php /* class view */ ?>
Instead of $rows, I tired to use print $field['image'] and print $field['body'] but this method does seem to work. Could you kindly advise on how I could theme the three fields, within categories, displayed using view?
You should name your template like this
views-view-fields--<machine-name-of-your-view>.tpl.php
So I'm assuming from the above that your view is called 'plugin-categories'. An easy way to check is to go to edit the view and look at the URL while you're on the edit page. It should have the format /admin/structure/views/view/YOUR-VIEW'S-MACHINE-NAME/edit, so you can get it from there.
Once you're sure it has the right name, clear your cache to make sure Drupal is picking up your new template. You just need the one above, not all three to modify the output of the three fields in question.
Once you've cleared cache, Drupal should be picking up the new template. You didn't mention exactly what isn't working, just that it's not working, so I wanted to cover the naming and caching, just in case. Now, to output particular fields in this view template, call them like this:
$fields['your-field-machine-name']
So $fields['body'] (I think you're missing an 's')
You should have nothing about $rows in this template! If you have anything about $rows, you haven't copied and pasted from the correct views template. Simply output the fields as you want them to appear in your view, in whatever order you want with the syntax above and put in whatever css classes, etc you want.
Let us know if that works!

Magento 1.4.1.1 category sort order stored in cookie

The Magento category sort order seams to be stored in cookies.
Lets say default sort oder is by Name ascending. If a user changes this in descending then all next pages will have this sort order too.
How can i change this so the user sees in the next category the default sort order?
This is an old question but comes up as the first Google result for "Magento sort by cookie". So, in the spirit of SO, let's answer it!
To begin we must do some investigation, but first we need to know what our assumptions are.
Assumptions
Sort by state is stored in a cookie
Cookie is updated when the sort by select is changed
Where to start?
When in doubt in Magento in a situation like this, start in the frontend where your output is viewable.
If we look in \app\design\frontend\<your_package>\<your_theme>\template\catalog\product\list\toolbar.phtml
We see that the following handles the javascript action when the "Sort By" select box is changed.
<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<img src="<?php echo $this->getSkinUrl('images/sort_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" />
<?php else: ?>
<img src="<?php echo $this->getSkinUrl('images/sort_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" />
<?php endif; ?>
</div>
In particular, we're interested in the <select onchange="setLocation(this.value)"> portion. So what happens when this method is called?
Deeper down the hole we go! setLocation is defined in \js\varien\js.js around line 30.
function setLocation(url){
window.location.href = url;
}
So that's simple enough, it's just your basic Javascript new location directive.
When this <select> is changed, we are sent to a new URL with some parameters. This is the most probable location of the cookie setting.
Again in particular we are interested in the url parameters ?dir=<asc OR desc>&order=<whatever_metric_you_are_sorting_by>. Let's go out on a limb and take a guess that the order parameter is causing a "setCookie" method or something of the sort to be called.
Where do we find this though? Well that's simple: all Magento actions on a request can be followed from index.php to the final frontend rendering, so it MUST happen somewhere along the way! You can track down just about ANY action in Magento using this method.
However, to save you and I some time, we can also assume that cookies probably aren't set that often in any given web framework. Running a grep on the Magento directory for ('core/cookie') returns 23 hits in 14 files. That's narrowed it down pretty stinkin' well!
Of the 23 hits, only 4 of them are using the ('core/cookie')->set() method.
Of the 4 using the set() method:
\app\code\core\Mage\Persistent\Model\Observer\Session.php - line 79
\app\code\core\Mage\Sales\Helper\Guest.php - line 100
\app\code\core\Mage\XmlConnect\controllers\ConfigurationController.php - line 112
\app\code\core\Mage\XmlConnect\controllers\Adminhtml\Connect\ConfigController.php - line 101
Of those 4, only 2 of them are going to be dealing with your end user directly, namely Session.php, and Guest.php. Since the Guest.php is part of the Sales module in Magento AND part of the loadValidOrder() method, it is quite unlikely that it is dealing with product list sorting. That leaves us with 1 option left, and that is:
\app\code\core\Mage\Persistent\Model\Observer\Session.php - line 79
What does inspection of this code tell us?
// Set new cookie
if ($sessionModel->getId()) {
Mage::getSingleton('core/cookie')->set(
Mage_Persistent_Model_Session::COOKIE_NAME,
$sessionModel->getKey(),
$persistentLifeTime
);
}
That all the cookie is storing is a session ID for the server! It's almost like this is standard practice or something for modern web frameworks! ;). One of our base assumptions was incorrect!
TL;DR The Answer
The sort by preference is saved in the Magento server-side session for that client, and recalled when the user returns with the ID stored in the cookie.
To make it so Magento doesn't save this, we must do the following then:
Magento 'Sort By' - How to make Magento forget which option was selected
I figured instead of just linking the answer, I would explain how to get from your line of thinking to the answer, which is much more valuable since now you understand WHY the answer is the correct one. Enjoy and happy coding.
Update
Since the SO thread I linked accepted answer doesn't really follow Magento XML protocol, but the comment on the answer does, I'll post it here with credit to the Author of the comment:
You need to apply a Layout update with the following xml for the pages you want Magento to "forget" sorting order on:
<reference name="product_list_toolbar">
<action method="disableParamsMemorizing" />
</reference>
by SO User Giel Berkers
I dont think you can. If the user is trying to improve their experience which only effects them then the cookie is updated and until this cookie expires the sort order will always show as they have selected.
You could clear the cookie on every time the pagination is clicked but this could cause other issue if the user is logged in.
Not only that it would remove and products that may have been added to their basket.
I would suggest to accept the user is responsable for they wish to view the listings, or simply remove the option " sort oder is by Name ascending" for example so they cannot select it.

codeigniter dynamic data template

I have this template view at view/include
<?php $this->load->view('include/header'); ?>
<?php $this->load->view($sidebar_column); ?>
<?php $this->load->view($result_column); ?>
<?php $this->load->view($footer_row); ?>
<?php $this->load->view('include/footer'); ?>
and i have the footer_row html at view/include
<div> this is footer row <?php echo $username ?></div>
then i call the footer_row in my controller
$data['footer_row'] = 'include/footer_row';
$this->load->view('include/template',$data);
My question, the footer_row is logged in user info and it appear in EVERY pages. With the above method I use, I have to call and retrieve the user info in every controller. How can i make it reusable so i don't need to repeat myself.
Reusability comes from making use of your constructors, and parent classes. Have a look at my previous answers:
Header and Footer in CodeIgniter
Constructor session validation for different functions
Instead of using this method why dont you use a template library which is easy to use. I would recommend Philsturgeons Template Library but there are some more you can use any that fits to your requirements.
Williams Concepts
http://williamsconcepts.com/ci/codeigniter/libraries/template/
Phil Sturgeons's
http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
Most Simple
http://maestric.com/doc/php/codeigniter_template
And Finally Binpresses's
http://www.binpress.com/app/codeigniter-template-library/223

Resources