Pagination using multiple searcing criteria in codeigniter - php

Im trying to implement pagination using multiple searching criteria.
Supposed I Have student table. I also use pagination when the list of student displayed.
The pagination link is. site_url . '/student/page/'; so I use $config['uri_segment'] = 1;
so the pagination link will be
1
2
and son.
After that I wanna search student data using 3 searching criteria implemented using textfield.
id name address.
user can search by id or name or address or combination of the three criteria.
the url become
http://mysite/index.php/student/page/0
href=http://mysite/index.php/student/page/1
and son.
but I use get method for searching. and while trying to search using the search criteria field the url become
href="http://mysite/index.php/student/page/1?id=1&name=a&address=b
the problem occurred when I try create pagination based on criteria. because the pagination link have contain query string
i don't know how to create become
href="http://mysite/index.php/student/page/0?id=1&name=a&address=b
href="http://mysite/index.php/student/page/1?id=1&name=a&address=b
or do you have a best practice to solve this problem ?
Hi phill ....
I have try your suggestion.
$array = array('id' => '001', 'name' => 'a', 'address' => 'canada');
the url become
id/001/name/a/address/canada. I use $this->uri->uri_to_assoc() function to get key and value of the segment.
array (
id => 001,
name=>a,
address=>canada
)
but while there some searching criteria that not included while searching. let say, the user only search by name and address. the array become
$array = array('id' => '', 'name' => 'a', 'address' => 'canada'); and the url id/name/a/address/canada
the assoc array become
array (
id => name,
a=>address,
canada=>
)
the assoc array is not disorganized again. so I can't get the right value of the assoc array.
I think i will set the identifier to the searching criteria if not included. supposed i put #.
if isset($_GET['id']) then
$id = '#'
else
$id = $_GET['id']
$array = array('id' => $id, 'name' => 'a', 'address' => 'canada');
How about that ... ? or if there are another best practice ?

For that I would use $this->uri->uri_to_assoc():
index.php/user/search/name/joe/location/UK/gender/male
Using this function you can turn the
URI into an associative array with
this prototype:
[array]
(
'name' => 'joe'
'location' => 'UK'
'gender' => 'male'
)
See the full documentation here.
You can still use page/1 as the first lot then have /name/whatever afterwards.

Actualy, we can create pagination with multiple and unlimited criteria. Read here http://dengkul.com/2010/07/08/codeigniter-pagination-with-multiple-unlimited-searching-criteria/

Related

Is there any other option to insert data in the database using CodeIgniter?

I have a one form with more than 30 fields. I know how to submit the data in the database using CodeIgniter. I just want to know that is there any other option to submit the data without writing the whole input postcode. I mean check below code. It looks very big.
$reg_dat = array(
'surname' => $this->input->post('surname'),
'name' => $this->input->post('firstname'),
'age' => $this->input->post('age'),
'school' =>$this->input->post('school'),
'course' =>$this->input->post('course'),
'email' => $this->input->post('email')
'a' => $this->input->post('a'),
'b' => $this->input->post('b'),
'c' => $this->input->post('c'),
'd' =>$this->input->post('d'),
'e' =>$this->input->post('e'),
'f' => $this->input->post('f')
'g' => $this->input->post('g'),
'h' => $this->input->post('h'),
'i' => $this->input->post('i'),
'j' =>$this->input->post('j'),
'k' =>$this->input->post('k'),
'l' => $this->input->post('l')
and so on..
);
Is there any other option to make the above code in the small code?
$all_data = $_POST;
Above mentioned line will make an array with keys same as the column name as you mentioned in your question, so there is no need to write so many lines are you shown in your post.
OR
$all_data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter. This is secure way.
And then to insert all that data in DB, we have syntax in Codeigniter is:
$this->db->insert('table_name',$all_data); // Write this in your model.
So in table_name all the data will be inserted.
While using all $_POST elements can be quick, it's probably best to whitelist the ones you want to insert.
$whitelist = array('a', 'b', 'c', 'd' ...);
$reg_dat = array();
foreach($key => $value in $whitelist){
$reg_dat[$value] = $this->input->post($value);
}
In terms of security, ensuring that you're using XSS filters and any other validation you may want to do on these elements prior to insertion. Also, make sure that your database driver is mysqli, pdo or some other, more secure one than mysql.
Based on your below comment about the $_POST keys not lining up with your database columns, you could do the following.
$whitelist = array();
$whitelist['a'] = 'database_column_for_a';
$whitelist['b'] = 'database_column_for_b';
$reg_dat = array();
foreach($postKey => $databaseColumn in $whitelist){
$reg_dat[$databaseColumn] = $this->input->post($postKey);
}
If you have POST fields names and MySQL columns names exactly same then just do this
$reg_dat = $_POST
And enter that array into DB
Simply Do:
$reg_dat = $this->input->post();
That's all. You will get $reg_dat array which contains all post input field and you can directly insert it in Codeigniter like.
Note: For Use, this directly makes sure your input name and your column name must be same.
$this->db->insert('table_name',$reg_dat);
For get,
$this->input->get();

Array format for web service

I have 2 tables i.e user and photo. According to user_id get all the user details and all the photos from the photo table.
I am creating a web-service using slim framework. How is it possible to make a array like above given format?
How to make a array like following format?
array(
[id]=>
[name]=>
[email]=>
[dob]=>
[gallery]=>array(
[0] => 1.png
[1] => 2.png
)
[address]=> xyz
)
If your question is strictly about how to make the nested array, then here is a solution.
// create the nested portion of the array
$images = ['1.png', '2.png'];
// create the rest of the array
$result = [
'id' => 1,
'name' => 'default',
'email' => '123#abc.com',
'dob' => '3/4/89',
'gallery' => $images,
'address' => 'xyz'
];
Note how we set 'gallery' => $images. If you need data from two different tables to be retrieved at the same time and formatted like this, then that would probably be easiest to achieve using a join in your query. If you need help with that just say so.

Doctrine: Update using collection

The Problem/How
Pass angularJS the array result of a query that includes many joins.
Using angularJS to sort with ui-sortable reorders the dataset when sorting.
Pass data back to PHP and use synchronizeWithArray to save back (creates a collection).
Doctrine doesn't like receiving the data of the collection back in a different order than it outputs.
** If all I change are values - without reordering elements it saves with no problems.
Update: http://www.doctrine-project.org/jira/browse/DC-346
Noticed it was an old bug they never fixed, is there anything to still do what I want?
Details
$model = Doctrine_Core::getTable('TableName')->findOneById(...);
$model->synchronizeWithArray(array);
$model->save();
Doctrine (1.2) / mysql throws an Integrity error, duplicate primary key id = 2 - it is trying to change the id field.
When I reorder the elements with ui-sortable, it moves the arrays within 'Fields' around while also updating the 'position' value.
This is example data:
The problem would be array 0 and array 1 swap places - causing doctrine to cause primary key error as it tries to change the ids over.
array( // the root of the array is part of one table
id => 1001,
label => 'xxx',
Fields => array( // related table data
0 => array(
id => 1,
position => 0,
name => 'item1'
),
1 => array(
id => 2,
position => 1,
name => 'item2'
),
2 => array(
id => 3,
position => 2,
name => 'item3'
),
3 => array(
id => 4,
position => 3,
name => 'item4'
)
)
)
Well I guess we could either look on the Doctrine side and try to fix this bug, or make it work with the way Doctrine operates. The second option is probably easier.
Why don't you just save the order of the data, and put it back in that order before feeding it back to doctrine? One way to do that would be in angular. Where ever you get the array of data in angular, call saveOrder(), and before feeding it back, call reOrder():
var order = {};
function saveOrder(data)
{
for(var key in data)
{
if(data.hasOwnProperty(key))
{
order[data[key].id] = key;
}
}
}
function reOrder(data)
{
var ordered = [];
for(var key in data)
{
if(data.hasOwnProperty(key))
{
ordered[order[data[key].id]] = data[key];
}
}
return ordered;
}

Cakephp: Saving the array value not the integer

After filtering some data, i created a variable $customers. This variable is a simple array
which has the following values:
array(
(int) 1 => 'Customer1',
(int) 2 => 'Customer2',
(int) 3 => 'Customer3'
)
I passed this array from the controller to the view like this
$this->set('customers', $customers);
In the view i use this array in a form so that the user can select one
echo $this->Form->input('customer_id', array('options' => array($customers)));
The data which is displayed in the select form is this 'Customer1', 'Customer2', 'Customer3'
Eveyting works ok so far.
Now, after the user has submited the data, i want to do some further logic in the controller. I want to take the data which the user has selected and save it in a second table. So i do this:
$this->Invoice->set('secondtabel', $this->request->data['Invoice']['customer_id']);
The data is being saved in the second table, but the problem is saves the value '1', '2', '3' not the name of the customer. How can i save the name of the customer and not the identifier number from the array.
Please bear with me, i am new to cakephp and to php in general.
I assume this is actually a problem with your HTML, your select box likely looks something like this:
<select name="customer_id">
<option value="1">Customer1</option>
<option value="2">Customer2</option>
<option value="3">Customer3</option>
</select>
This is why your values are 1, 2 or 3 and not Customer1 etc, because $this->request->data['Invoice']['customer_id'] is equal to 1, 2 or 3 etc.
My suggestion would be to fix this at the root of the problem, and I think that by only passing the values into your select box, you should get HTML like this:
<option>Customer1</option>
... which will mean that $this->request->data['Invoice']['customer_id'] will equal Customer1 etc.
So, try this: (array_values will return an array only containing the values, essentially stripping the keys)
$this->set('customers', array_values($customers));
This should fix your problem. However, as far as structured data goes, the way you are currently doing it (storing 1, 2 or 3 etc) is actually the correct way to do this. This way, you simply join the customers table when you are retrieving this data, and you can grab the name that way... Something like this:
$invoices = $this->Invoice->find('all', array(
'conditions' => array(
// your custom find conditions
),
'joins' => array(
array(
'table' => 'customers',
'alias' => 'Customer',
'type' => 'LEFT',
'conditions' => array('Customer.id = Invoice.customer_id')
)
),
'fields' => array(
'Invoice.*', // retrieve regular invoice data
'Customer.name' // retrieve the joined customer name too
)
));
This way you still store a customer ID as an integer, and simply look up the name with SQL when you go to retrieve that data.
A possible reason why you might want to do it simply by storing the customer's name as text is that you want to store the customers name as it appears at the time, meaning if it changes in the future, previous invoices with that name attached won't change with it because that name is stored in text rather than a numeric reference to another table containing a name that has changed.
Hope this helps.
Docs
http://php.net/array_values

CakePHP models findFirst method issue

I'm not familiar with CakePHP too close. Recently I've ran into problem using Model. I need to get exaclty one row from database, modify some columns values and save it back. Pretty simple, right?
What do I try to do:
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findFirst($condition);
BUT i get FALSE in $model variable. At hte same time
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findAll($condition);
returns array. Its structure is something like:
array (
0 =>
array (
'MyModel' =>
array (
'id' => '1',
'some_id_column' => '123456',
'some_field' => 'some text',
...
),
),
I'd go with findAll if it did not return an array of arrays, but array of models (in my case - of one model). What do I want to achieve:
$condition = array('some_id_column' => $another_models_id);
$model = $this->MyModel->findFirst($condition);
$model->some_field = 'some another text';
$model->save();
Could you help me out to understand how it's usually done in CakePHP?
I'd also like to hear why findAll finds row and findFirst fails to find it... It just does not make sense to me... They should work in almost the same way and use the same database APIs...
If I can not do what I want in CakePHP, would you write a receipt how it is usually done there ?
There is no such method as findFirst.
You're probably looking for find('first', array('conditions' => array(...))).

Categories