I am designing search functionality with some data with Codeigniter.
My search model looks like:
class Search_Model extends CI_Model {
public function get_results($search_terms = false) {
// Build query
foreach ($search_terms as $field => $value) {
$this->db->like("{$field}", $value);
}
// Execute query
$query = $this->db->get('exams');
// Return results
return $query->result_array();
}
}
And the search controller method looks like this:
public function results() {
$search_terms = array(
'first_name' => $this->input->get('first_name', TRUE),
'last_name' => $this->input->get('last_name', TRUE),
'exam_name' => $this->input->get("exam_name", TRUE)
);
$data['title'] = "Search Results";
$data['exams'] = $this->search_model->get_results($search_terms);
$this->load->view('templates/header.php', $data);
$this->load->view('exams/index.php', $data);
$this->load->view('templates/footer.php');
}
The search is working as it should but I want to clean up the URL which comes out something like this if I only search for "First Name":
search/results?first_name=tim&last_name=&exam_name=
How can I remove those extra parameters that are unused in the URL?
The quickest way is just to filter the $searh_terms array before passing it to the model.
$search_terms = array(
'first_name' => $this->input->get('first_name', TRUE),
'last_name' => $this->input->get('last_name', TRUE),
'exam_name' => $this->input->get("exam_name", TRUE)
);
// Remove empty search values
foreach ($search_terms as $key => $value) {
if (strlen($value) == 0) {
unset($search_terms[$key]);
}
}
include jquery in your header. then add this script.
<script>
$(document).ready(function(){
$('#myform').submit(function(event){
event.preventDefault();
var url = $(this).attr('action')+'?';
var first = false;
var second = false;
if($('input[name="first_name"]').val()){
url += 'first_name='+$('input[name="first_name"]').val();
first = true;
}
if($('input[name="last_name"]').val()){
if(first){
url += '&';
}
url += 'last_name='+$('input[name="last_name"]').val();
second = true;
}
if($('input[name="exam_name"]').val()){
if(second){
url += '&';
}
url += 'exam_name='+$('input[name="exam_name"]').val();
}
window.location.replace(url);
});
});
</script>
(i didnt ran this script yet, so let me know if u get any syntax error or somethign else.)
and finally change your results function to this,
public function results() {
$search_terms = array();
if(isset($_GET['first_name'])){
$search_terms['first_name'] = $this->input->get('first_name', TRUE);
}
if(isset($_GET['last_name'])){
$search_terms['last_name'] = $this->input->get('last_name', TRUE);
}
if(isset($_GET['exam_name'])){
$search_terms['exam_name'] = $this->input->get('exam_name', TRUE);
}
$data['title'] = "Search Results";
$data['exams'] = $this->search_model->get_results($search_terms);
$this->load->view('templates/header.php', $data);
$this->load->view('exams/index.php', $data);
$this->load->view('templates/footer.php');
}
You can try this:
$mode = array();
if(isset($_GET['first_name'])):
$mode[] = 'first_name='.$_GET['first_name'];
endif;
if(isset($_GET['last_name'])):
$mode[] = 'last_name='.$_GET['last_name'];
endif;
if(isset($_GET['exam_name'])):
$mode[] = 'exam_name='.$_GET['exam_name'];
endif;
//etc...
//Then reformulate link
if(!empty($mode)){
$link = '?' . implode('&',$mode);
}else{
$link = '';
}
redirect("form.php".$link);
just filter what you send...
foreach ($search_terms as $field => $value) {
if ($value <> ""){ //or the test you like to use...
$this->db->like("{$field}", $value);
}
}
Related
From my PHP application, I want to update the inventory_policy (continue/deny) of all products using API. Is there any way to do so without a loop?
I did not find any way to update it at once. Hence, I have updated it one by one. Please have the code below.
public function update_inventory_policy_for_all_item($inventory_policy, $page_info=null){
$response = $this->do_get("/admin/api/2021-04/products.json?limit=250".$page_info);
if ($response === FALSE)
{
return false;
}
$result_products = $response['body']['products'];
$headers = $response['headers'];
foreach($result_products as $shopify_product){
foreach($shopify_product['variants'] as $variant){
$variant_id = $variant['id'];
$data['variant'] = array(
'id' => $variant['id'],
'inventory_policy' => $inventory_policy,
);
$this->do_put("/admin/api/2021-04/variants/$variant_id.json", $data);
}
}
if(isset($headers['link'])) {
$links = explode(',', $headers['link']);
foreach($links as $link) {
$next_page = false;
if(strpos($link, 'rel="next"')) {
$next_page = $link;
}
}
if($next_page) {
preg_match('~<(.*?)>~', $next_page, $next);
$url_components = parse_url($next[1]);
parse_str($url_components['query'], $params);
$page_info = '&page_info=' . $params['page_info'];
$this->update_inventory_policy_for_all_item($inventory_policy, $page_info);
}
}
return true;
}
I defined my postParams where I want to pass "hash" value from db.
What I am trying to accomplish is that if hash exists in my Session table to return TRUE and if not to return FLASE.
Problem is my code always returns TRUE.
What I am missing?
$postData = $this->requirePostParams(['hash']);
$this->container->get('app')->formService(
$this->data['hash']
);
if ($postData['hash']) {
$hash = $this->get('app')->getSessionRepository()->find($this->data['hash']);
if (!$hash) return false;
} else {
return true;
}
And my requirePostParams works fine! (tested on other functions)
protected function requirePostParams($params) {
$currentRequest = $this->get('request_stack')->getCurrentRequest();
$postData = $currentRequest->request->all();
$postContent = json_decode($currentRequest->getContent(), true);
if(!empty($postContent)) $postData = $postContent;
$this->data = $postData;
$missingParams = [];
foreach ($params as $param) {
if (!array_key_exists($param, $postData)) {
$missingParams[] = $param;
}
}
}
And my service:
$findHash = $this->getSessionRepository()->findOneBy([
'hash' => $hash
]);
As xmike mentioned in comments, function requirePostParams returns nothing. That's why $postData['hash'] is never set.
Try to replace $postData['hash'] with $this->data['hash']:
$this->requirePostParams(['hash']);
$this->container->get('app')->formService(
$this->data['hash']
);
if ($this->data['hash']) {
$hash = $this->get('app')->getSessionRepository()->find($this->data['hash']);
if (!$hash) return false;
} else {
return true;
}
I guess I'm just looking over it but I can't get my variables to my view.
In the same controller I call a function, from there I return an array containing the variables (also array's). From within the function I originally started I send the variables to the view.
But, in the view I get an error saying the variable is undefined.
The information I need is in array $items and array $trans. These need to get in the function confirmation and end up in the confirmation view.
The two functions (I tried to remove most code that has nothing to do with the question):
public function confirmation($order_id){
$order = Orders::findOrFail($order_id);
if(isset($order->transaction_id)){
$data = [];
$data['order_id'] = $order->order_reference;
$data['trans'] = $order->dat['tr'];
$data['items'] = $order->dat['it'];
return view('confirmation', $data);
}else{
//Nothing relevant
}
}
public function sendpicqer($order_id){
$order = Orders::with(['orderDetails', 'orderAddress', 'customer'])->where('order_id', $order_id)->first();
$order_details = OrderDetails::where('order_id', $order_id)->get();
$error = $order_id;
$result = $this->picqer->addCustomer($customer);
if(!isset($result['data'])){
$error = $result;
if(is_array($result)){
$error = json_encode($result);
}
return redirect()->route('cancel');
}
$orderData = [
'idcustomer' => $result['data']['idcustomer']
];
$orderData['products'] = [];
$items = [];
foreach($order_details as $od){
$pid = $od->product_id;
switch ($pid) {
case 1:
$pid = 2399983;
break;
case 2:
$pid = 2399990;
break;
}
$orderData['products'][] = [
'idproduct' => $pid,
'amount' => $od->quantity
];
$items[] = [
'sku' => $pid,
'name' => $od->product_id->product_name,
'price' => $od->product_id->product_price,
'quantity' => $od->quantity
];
}
$result = $this->picqer->addOrder($orderData);
if(isset($result['data'])){
//Succeeded!
$idorder = $result['data']['idorder'];
$orderid = $result['data']['orderid'];
$trans = array('id' => $orderid, 'affiliation' => 'Matt Sleeps', 'revenue' => $order->total_price);
$dat = [];
$dat['tr'] = $trans;
$dat['it'] = $items;
return $dat;
$result2 = $this->picqer->sendRequest('/orders/'.$idorder.'/process', null, 'POST');
if(!isset($result2['data'])){
$error = $result2;
if(is_array($result2)){
$error = json_encode($result2);
}
return redirect()->route('cancel');
}
}else{
$error = $result;
if(is_array($result)){
$error = json_encode($result);
}
return redirect()->route('cancel');
}
//Order is successfully confirmed and send to Picqer!
$error = '(Both to the customer and with Picqer)';
}
This is the part of view where I need access to the variables:
<?php
var_dump($order_id);
var_dump($trans);
var_dump($items);
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$trans['id']}',
'affiliation': '{$trans['affiliation']}',
'revenue': '{$trans['revenue']}'
});
HTML;
}
// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
return <<<HTML
ga('ecommerce:addItem', {
'id': '$transId',
'name': '{$item['name']}',
'sku' : '{$item['sku']}',
'price': '{$item['price']}',
'quantity': '{$item['quantity']}'
});
HTML;
}
?>
<script>
<?php
echo getTransactionJs($trans);
foreach ($items as &$item) {
echo getItemJs($trans['id'], $item);
}
?>
ga('ecommerce:send');
</script>
You have to send the variables to the view. You could change the code to something like this:
//Return the view via confirmation function.
public function sendpicqer($order_id){
...
return $this->confirmation($order_id, $items, $trans);
}
public function confirmation($order_id, $items, $trans){
$order = Orders::findOrFail($order_id);
if(isset($order->transaction_id)){
$data = [];
$data['order_id'] = $order->order_reference;
$data['trans'] = $trans;
$data['items'] = $items;
//Send the variables to the view
return view('confirmation', $data);
}else{
return redirect()->route('cancel');
}
}
Hope this helps...
I am trying to create an API using CakePHP that allows searching. For example:
http://localhost:8765/users/index/?username=admin
Which should return users with usernames equal to 'admin':
users: [
{
id: 3,
username: "admin",
image: "",
firstName: "Jeremy",
lastName: "Quick",
userTypeId: 1,
email: "jrquick#test.com",
groupId: 2
}
]
So far, I have been able to accomplish this with a custom get() in the AppController which checks the $_GET and $_POST array for fields on the model. But the function is getting more and more complicated and verging on hackiness as I add more functionality (range search, collection search, and child table filtering). Is there a better, more CakePHP friendly way of accomplishing this? Whether through pure cakephp or a plugin?
I think you want to use the Cakephp Search plugin. It has good documentation and uses a PRG method similar to what you are currently using. It will function just fine through an API. Here's a link to that plugin: github.com/FriendsOfCake/search
If You want to create API, You should create a MiddleWare at first, which will filter tokens, keys etc. to make Your API more protected.
Also, You should use Plugins and RESTful Routes, which will be very helpful.
To create plugin:
bin/cake bake plugin Api
Create Model:
bin/cake bake model Users
For example, You want to have UsersController in Api plugin:
<?php
namespace Api\Controller;
/* This controller will be extending like parent */
use Api\Controller\AppController;
use Api\Model\Table\UsersTable;
/**
* Class UsersController
* #package Api\Controller
* #property UsersTable $Users
*
*/
class UsersController extends AppController{
public function initialize(){
parent::initialize();
$this->loadModel('Api.Users');
}
public function getUser($field ='username', $username = false){
return $this->_jsonResponse(
[
'users' => $this->Users->findBy{ucfirst($field)}($username)
];
)
}
public function _jsonResponse($data, $code = 200){
$this->response->type('json');
$this->response->statusCode($code);
$this->response->body(
json_encode((array)$data)
);
return $this->response;
}
}
Route will be descripbed in plugins/config/routes.php. You need to create Route Map for API in /api path:
function (RouteBuilder $routes) {
$routes->resources('Users', [
'map' => [
'get-user' => [
'action' => 'getUser',
'method' => 'GET' /* Can be also as array ['GET', 'PUT', 'DELETE'] */
]
]
]);
$routes->fallbacks('DashedRoute');
}
If You have frequent calls, You should use Cache that calls and save them for some amount of time. For example - 10 minutes. Cache can be configured in config/app.php. You should create separate Cache prefix and use it in this way:
<?php
use Cake\Cache\Cache;
$data = [];
Cache::write('some_key', $data, 'prefix')
dump(Cache::read('some_key', 'prefix'));
It's just examples. If You will face some problems - just tell in comments :)
Also, use Migrations and Seeds instead dumping sql files
If You want to filter data from Middleware - You should have Event as argument, that will contain request data ($_POST) and request query($_GET) variables that You will be able to easily handle with.
From controllers You need to use $this->request->data to get POST data array or $this->request->query to get GET data array.
I haven't found an answer that seems to work exactly how I am wanting, so here is my current get command. It does allow searching by fields, join tables, greater/less than, in array, and like.
If anyone has recommendations to improve I will update my answer.
public function get() {
$response = new Response();
$model = $this->loadModel();
$fields = $this->getFields();
$joins = $this->getJoins();
$order = $this->getOrder();
$params = $this->getParams();
$limit = $this->getLimit();
$offset = $this->getOffset();
$query = $model->find('all', ['fields' => $fields]);
if (!is_null($joins)) {
$query->contain($joins);
}
if (sizeof($params['equals']) > 0) {
foreach ($params['equals'] as $equalsKey=>$equalsValue) {
$query->andWhere([$equalsKey => $equalsValue]);
}
}
if (sizeof($params['or']) > 0) {
foreach ($params['or'] as $orKey=>$orValue) {
$query->orWhere([$orKey => $orValue]);
}
}
if (!is_null($order)) {
$query->order([$order]);
}
if (!is_null($limit)) {
$query->limit($limit);
if (!is_null($offset)) {
$query->offset($offset);
}
}
$response->addMessage($model->table(), $query->toArray());
$response->respond($this);
}
private function getFields() {
$fields = [];
if (array_key_exists('fields', $_GET)) {
$fields = explode(',', $_GET['fields']);
}
return $fields;
}
private function getLimit() {
$limit = null;
if (array_key_exists('limit', $_GET)) {
$limit = $_GET['limit'];
}
return $limit;
}
private function getJoins() {
$joins = null;
if (array_key_exists('joins', $_GET)) {
$joins = explode(',', $_GET['joins']);
}
return $joins;
}
private function getOffset() {
$offset = null;
if (array_key_exists('offset', $_GET)) {
$offset = $_GET['limit'];
}
return $offset;
}
private function getOrder() {
$results = [];
if (array_key_exists('order', $_GET)) {
$orders = explode(',', $_GET['order']);
foreach ($orders as $order) {
$sign = substr($order, 0, 1);
$direction = 'ASC';
if (in_array($sign, ['+', '-'])) {
if ($sign === '-') {
$direction = 'DESC';
}
$order = substr($order, 1);
}
$result = $order;
if (strpos($result, '.') === false) {
$result = $this->loadModel()->alias() . '.' . $order;
}
$result = $result . ' ' . $direction;
$results[] = $result;
}
}
return (sizeof($results) == 0) ? null : implode(',', $results);
}
private function getParams() {
$params = [
'equals' => [],
'or' => []
];
$parentModel = $this->loadModel();
$array = array_merge($_GET, $_POST);
foreach ($array as $field=>$value) {
$comparisonType = 'equals';
$operator = substr($field, strlen($field) - 1);
if (in_array($operator, ['!', '>', '<'])) {
$field = substr($field, 0, strlen($field) - 1);
$operator .= '=';
} else if (in_array($operator, ['|'])) {
$field = substr($field, 0, strlen($field) - 1);
$comparisonType = 'or';
$operator = '=';
} else if (in_array($operator, ['%'])) {
$field = substr($field, 0, strlen($field) - 1);
$operator = 'LIKE';
$value = '%'.$value.'%';
} else {
$operator = '=';
}
if ($value == 'null') {
$operator = (strpos($operator, '!') === false) ? 'IS' : 'IS NOT';
$value = null;
}
$field = str_replace('_', '.', $field);
if (strpos($field, '.') === false) {
$alias = $parentModel->alias();
} else {
$fieldExplosion = explode('.', $field);
$alias = $fieldExplosion[0];
$field = $fieldExplosion[1];
}
$model = null;
if ($parentModel->alias() !== $alias) {
$association = $parentModel->associations()->get($alias);
if (!is_null($association)) {
$model = $this->loadModel($association->className());
}
} else {
$model = $parentModel;
}
if (!is_null($model)) {
if ($model->hasField(rtrim($field, 's')) && !$model->hasField($field)) {
$field = rtrim($field, 's');
$value = '(' . $value . ')';
$operator = ' IN';
}
if ($model->hasField($field)) {
$params[$comparisonType][$alias.'.'.$field . ' ' . $operator] = $value;
}
}
}
return $params;
}
Codeigniter when i submit more than one option of form_multiselect(), Only just the last one that saved on database.
in my view :
<label>Trimestres :</label>
<div class="controls" >
<?php $options = array(
'trim1' => ' Premier trimestre (Janv,Fév,Mars)',
'trim2' => ' Deuxiéme trimestre (Avril,Mai,Juin)',
'trim3' => ' Troisiéme trimestre (Juill,Aout,Sept)',
'trim4' => ' Quatriéme trimestre (Oct,Nov,Déc)',
);
echo form_multiselect('trimestres', $options , $this->input->post('trimestres') ? $this->input->post('trimestres') : $participant_sport->trimestres, 'id="trim"'); ?>
</div>
</div>
in my controller :
public function inscriresport ($id = NULL)
{
// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'participant non trouvé';
}
else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
}
// Process the form
$this->participantsport_m->array_from_post(array('matricule', 'nom', 'prenom', 'beneficiaire', 'sexe', 'telephone', 'date_naissance', 'date_inscription_sport', 'trimestres' ,'sport_montant_paye', 'sport_debut_periode', 'sport_fin_periode'));
$this->participantsport_m->save($data, $id);
redirect('admin/agent/profile/3608');
}
// Load the view
$this->data['subview'] = 'admin/agent/inscriresport';
$this->load->view('admin/_layout_main', $this->data);
}
The function array_from_post() is defined on application\core\MY_Model.php :
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
in my model :
public function get_new()
{
$participant_sport = new stdClass();
$participant_sport->matricule = '';
$participant_sport->nom = '';
$participant_sport->prenom = '';
$participant_sport->beneficiaire = '';
$participant_sport->sexe = '';
$participant_sport->telephone = '';
$participant_sport->date_naissance = '';
$participant_sport->date_inscription_sport = '';
$participant_sport->trimestres = '';
$participant_sport->sport_montant_paye = '';
$participant_sport->sport_debut_periode = '';
$participant_sport->sport_fin_periode = '';
return $participant_sport;
}
Any help Please? i think that must be an array but i don't know how to do it?
i thing that i must do something like that :
foreach($_POST["strategylist[]"] as $s) {
# do the insert here, but use $s instead of $_POST["strategylist[]"]
$result=mysql_query("INSERT INTO sslink (study_id, strategyname) " .
"VALUES ('$id','" . join(",",$s) . "')")
or die("Insert Error: ".mysql_error());
}
to insert more than one option selected in one row but i don't know how to do it in codeigniter
the get() function :
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
If select name (in HTML tag) is trimestres it will always remember last selection. Use trimestres[] as a name to get array with all selected values`
<select name="trimestres[]" multiple …
By the way:
I don't know how array_from_post() works but it has to change trimestres[] values to one string to save all of them in one column. It is hard to search/add/delete one value if all values are in one string. It is "SQL Antipattern". You could do another table in database for trimestres - one value in one row.
Edit:
It will change all arrays into string with elements connected by ,. Not tested.
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
// print_r($this->input->post($field));
if( is_array( $this->input->post($field) ) ) {
$data[$field] = join(",", $this->input->post($field));
} else {
$data[$field] = $this->input->post($field);
}
// print_r($data[$field]);
}
return $data;
}
Edit:
Not tested.
public function inscriresport ($id = NULL)
{
// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'participant non trouvé';
// explode to array
// print_r($this->data['participant_sport']->trimestres); // test before explode
// $this->data['participant_sport']['trimestres'] = explode(",", $this->data['participant_sport']['trimestres']);
$this->data['participant_sport']->trimestres = explode(",", $this->data['participant_sport']->trimestres);
// print_r($this->data['participant_sport']->trimestres); // test after explode
} else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
}
// rest of code
}
There is a easy way to solve this problem that I found today.
you have to serialize the $_POST['trimestres'] array just after array_form_post .
the this array will save to database as a serialize string.
public function inscriresport ($id = NULL)
{
// Fetch a participant or set a new one
if ($id) {
$this->data['participant_sport'] = $this->participantsport_m->get($id);
count($this->data['participant_sport']) || $this->data['errors'][] = 'participant non trouvé';
}
else {
$this->data['participant_sport'] = $this->participantsport_m->get_new();
}
// Process the form
$this->participantsport_m->array_from_post(array('matricule', 'nom', 'prenom', 'beneficiaire', 'sexe', 'telephone', 'date_naissance', 'date_inscription_sport', 'trimestres' ,'sport_montant_paye', 'sport_debut_periode', 'sport_fin_periode'));
$data['trimestres'] = serialize($_POST['trimestres']);
$this->participantsport_m->save($data, $id);
redirect('admin/agent/profile/3608');
}
// Load the view
$this->data['subview'] = 'admin/agent/inscriresport';
$this->load->view('admin/_layout_main', $this->data);
}
When you just need this data back form database just use php unserialize() function .
Hope it will help to do this easily ....
-thanks