Building query strings in PHP - php

I'm wondering if there is a technique or function to do this or if I'm just going to have to have a ton of IF statements/arrays here-
I have a page called products.php and a few different filters I add in the query string.
All the filters (if chosen) could look like this:
products.php?cat=chairs&type=pine&search=Vienna+Range
Is there a simple way to build the query for use again?
I'm going to have buttons/a search box to change the filters I'm using, so will I have to build the URL and query up again for each filter?
For example I'd like to have:
Tables
Beds
Chairs
but also build the query so that it remembers my search term, wood type and range; so clicking on "Tables" would take me to
products.php?cat=chairs&type=pine&search=Vienna+Range.

You can write something like:
<?php
$params = array(
'cat' => 'chair',
'type' => 'pine',
'search' => 'Vienna Range',
);
print_r(http_build_query($params) . PHP_EOL);
You'll get this:
cat=chair&type=pine&search=Vienna+Range

Related

Cakephp - Getting items from next page in current page?

I'm currently working on a gallery system using cakephp. In it, we have paginated each image album so each page contains a set number of images at most. I have attained this by using:
$this->paginate = array(
'conditions' => array(
'Item.album_id' => $id
),
'order' => array(
'Item.added' => 'desc'
),
'limit' => '50'
);
On my view controller. With this, I can show all the items on each page with no problems.
I'm currently, however, facing a single issue: I need to show, after all the items in the current page, a button that leads to the next page. This is not a problem, except that by design the button that says NEXT PAGE should have an item from the next page as its background.
I have looked around, trying to figure out a way to pull an item from the next page of a paginated system in cakephp without luck. Has anyone done this, or does anyone have a clue how to go about it?
You have to query the preview image manually in the current page.
To access the page number you can use $this->params in the action. Then you have to query images with 'limit' and 'page' parameters as mentioned in the documentation.
After that set the image URL like this:
$this->set('preview_image_url', $queried_url);
Then in the view use inline styling to set the background for the next button.
With Alto's help, I ended up doing this (Putting it here just in case anyone wonders exactly how I did it):
$CurrPage = $this->params['paging']['Item']['page'];
$NextParams = array(
'limit' => 1,
//'page' => $CurrPage + 1,
'order' => array(
'Item.added' => 'desc'
),
'offset' => $CurrPage*50
//'order' =>('rand()')
);
$NextImage = $this->Item->find('first', $NextParams);
$this->set('NextImage', $NextImage);
This gives me the first item from the following page, which is good enough for me. Note the commented 'order' =>('rand()') - I tried to grab a random item from the following page but, alas, it seems like Cake first scrambles all items and THEN paginates. There's probably a way around this, but for now this one did it for me.
Also, using 'page' => $CurrPage + 1 didn't seem to work, with the system instead returning a seemingly randomly chosen image. That's why I instead defaulted to using 'offset' => $CurrPage*50, where 50 is the amount of items per page I'm showing.

Fuelphp related limit

I am currenty working on a story board, and i upload images there telling a story.
So the problem is i just want to limit the related query.
The story board has a has_manyrelation for images. and on the list where the story boards appear i just only want to show the first image, and when someone clicks it it will show all, the show all is fine, but i can limit just the related images in my view.
And im totally confused with it because i read lot of infos about it in the forum, tried may variations, the limit is just ignored, or i get errors
so here is my code
controller
$stories = Model_Storyboard::find('all', array('related' => array('storyboardimage')));
$this->template->title = "Sotry Board | " . Config::get('site_name');
$this->template->content = View::forge('storyboard/index', array('stories' => $stories));
So how can i limit the storyboardimage to show the first image for each storyboard?
Sorry if im askin to much, but i would be really happy if someone could give me a working example beacuse i tried lots of variations and nothing works...
You can add criteria onto the relations, i.e. where, order_by, limit etc.
For example (note this is untested)
$stories = Model_Storyboard::find('all', array(
'related' => array(
'storyboardimage' => array(
'limit' => 1,
'order_by' => array(
'field_indicative_of_first_image' => 'desc'
),
)
)
));
In the example above I've added in field_indicative_of_first_image which simply means replace this with a column which you can use to get the first image. This might be for example a created_at column or a weight or sort_order column. Without knowing your database schema, I can't tell you which you'd need.

In CakePHP, Why using HTML->link instead of manually writing an anchor text?

Isn't it much slower concerning development time ?
What are the advantages of of HTML->link ?
Thanks !
It's just a question of whether you want to generate your own URLs and hard-code them, or if you want Cake to do the work for you. For simple urls leading to the homepage of your site using cake may seem slower, but it's actually useful for dynamic urls, for example:
Say you're printing a table of items and you have a link for each item that deletes that item. You can easily create this using:
<?php
echo $this->Html->link(
'Delete',
array('controller' => 'recipes', 'action' => 'delete', $id),
array(),
"Are you sure you wish to delete this recipe?"
);
Notice how using an array specifying the controller and action as a URL allows you to be agnostic of any custom routes. This can have its advantages.
The corresponding way to do it without the HTML helper would be:
Delete
It can also be really useful for constructing URL query strings automatically. For example, you can do this in array format:
<?php
echo $this->Html->link('View image', array(
'controller' => 'images',
'action' => 'view',
1,
'?' => array('height' => 400, 'width' => 500))
);
That then outputs this line of HTML:
View image
It could be a pain to generate that URL manually.
In summary, while it may seem awkward for simple links, the HTML helper definitely has its uses. For further uses, consult the cakePHP book on the HTML helper's link function.

Yii Dynamic Dropdown - Expensive Query

I have a Yii dropdown which loads a table of cities, states, zipcodes, lat and lon. When loading the state dropdown, it takes forever. Is there a way to speed up the query to cut down on pageload time? I've included my view:
echo $form->dropDownList($model,'State', CHtml::listData(Zipcodes::model()->findAll(),
'State', 'State', 'State'), array('empty'=>'-- Choose State --'));
The table is 41,000 entries. Setting the $groupField in listData() didn't seem to give any noticeable improvements.
I think you can have two ways:
1) Use cache (with long or no expiration time, because this data you have are not dynamic).
First time your loading time will not change, but after that, it will bee much, much faster, because cache will be already saved.
http://www.yiiframework.com/doc/guide/1.1/en/caching.data#query-caching
$zipcodes = Zipcodes::model()->cache(3600*24*7)->findAll(); //cache for a week
Using cache you need to edit your config/main.php file
//...
'components' => array(
//...
'cache'=>array(
'class'=>'system.caching.CFileCache',
//'class'=>'system.caching.CDummyCache',
//other cache class
),
//...
),
//...
2) Maybe consider to try CJuiAutoComplete
http://www.yiiframework.com/doc/api/1.1/CJuiAutoComplete/
It will give you results only on typing and matching your interested data.
Not loading 41k entries and placing them in HTML <option> tags seems to be a very good start... :-)
Only load those you need using AJAX.
Is the state drop-down culling unique states from that table, i.e., you end up with 50-ish results? You might want to construct the query manually and see where the bottleneck is. For complex queries, this is often dramatically faster than using AR, which I'm assuming you are using to create the model. I'm also assuming you are not trying to load 41k records into a drop-down.
We might need a bit more detail about the structure of the table, how you are creating the model, etc.
Using CDbCriteria, I set the "GROUP BY" for the query:
$stateGroupBy = new CDbCriteria; // i got that bitch criteria. bitches love criteria!
$stateGroupBy->group = 'State'; // group by state
echo $form->dropDownList($model, 'State', CHtml::listData(Zipcodes::model()->findAll($stateGroupBy), 'State', 'State'),
array('ajax' =>
array(
'type'=>'POST', //request type
'url'=>CController::createUrl('search/dynamiccities'), #we don't have to use search/, we could just say dynamiccities
'update'=>'#Zipcodes_City',
/*'data'=>'js:jQuery(this).serialize()'*/
),
'empty'=>'-- Choose State --')
);
I flaked out in getting back to this, but this cut down on the load significantly.

Theme function in Drupal 6

Im using Drupal 6.x. In my page i have the following code which prints a paged table.
$headers = array(array('data' => t('Node ID'),'field' => 'nid','sort'=>'asc' ),
array('data' => t('Title'),'field' => 'title'),
);
print theme('pager_table','SELECT nid,title FROM {node_revisions}', 5, $headers );
Is there a way i can pass the rows of a table as an array to the theme function ?
I don't know theme_pager_table, it's not a part of Drupal core. What you can do, it to wrap your sql in pager_query(), then you can loop through your results and create the table rows like normal. pager_query() will handle adding the LIMIT and OFFSET in the query.
Doing this you cam use the normal theme_table and just add the pager with theme_pager. (Remember to use the theme, wrapper function instead of calling the theme functions directly)

Categories