MeDoo - Conditional LIKE in the WHERE part - php

How can I write the follow sql query in MeDoo???
SELECT * FROM lockers WHERE Active = 1 AND GymID = " . $gimid . " AND ForMale = " . $male .
($filter ? " AND Locker LIKE '%" . $filter . "%'" : "")
The problem for me is the conditional LIKE.
$total = $this->db->count('lockers',
['AND' => [
'Active' => 1,
'GymID' => $gimid,
'ForMale' => $male
]]
);
Some suggestion????
Thanks a lot!!!

Finally, I solved it in the follow way:
// getting locker's total
$total = $this->db->debug()->count('lockers', [
'AND' => [
'Active' => 1,
'GymID' => $gimid,
'ForMale' => $male,
'Locker[~]' => $filter ? $filter : '' //<- LIKE with empty string shows all
]
]);

Related

Shortcode parameter not working in WordPress

I am trying to pass amount parameters in url from the shortcode. The from and to attributes are working fine but the amount attribute is not working. The output shows the value of 1.
using the shortcode like this:
[exchange_rate from="USD" to="EUR" amount="100"]
function exchange_rate_shortcode($atts) {
$atts = shortcode_atts(array(
'from' => 'AED',
'to' => 'NPR',
'amount' => '1',
), $atts);
$url = "https://api.fastforex.io/convert?from=" . $atts['from'] . "&to=" . $atts['to'] . "&amount=" . $atts['amount'] . "&api_key=xxxx-xxxxx-xxxx";
$result = file_get_contents($url);
$result = json_decode($result, true);
return number_format((float)$result['result']['rate'], 2, '.', '') . " " . $atts['to'];
}
add_shortcode('exchange_rate', 'exchange_rate_shortcode');
Perhaps you somehow wrote the word "amount" wrong and therefore an error occurs. To avoid confusion, use the build_query function.
Your code will look like this:htt
function exchange_rate_shortcode($atts)
{
$atts = shortcode_atts(array(
'from' => 'AED',
'to' => 'NPR',
'amount' => '1',
'api_key' => 'xxxx-xxxxx-xxxx',
), $atts);
// to make sure the correct value type is specified
$atts['amount'] = (float)$atts['amount'];
$url = "https://api.fastforex.io/convert?" . build_query($atts);
$result = file_get_contents($url);
if ( ! empty($result)) {
$result = json_decode($result, true);
return number_format((float)$result['result']['rate'], 2, '.', '') . " " . $atts['to'];
}
return '';
}
add_shortcode('exchange_rate', 'exchange_rate_shortcode');

saving php array to mariadb dynamic colum

I have an array like this
$test = array(
'Subscription' => '1',
'Streaming' => '1',
'Download' => '0'
)
so during updating to mariaDB
$query = 'UPDATE table_1 set category_dynamic = COLUMN_CREATE(' . $test . ') where id = 1';
$this->Model->query($query);
I want to save the array this way ('Subscription',1,'Streaming',1,'Download',0)
any suggestion ?
You can try this :
<?php
$test = array(
'Subscription' => '1',
'Streaming' => '1',
'Download' => '0'
);
$data = '';
foreach($test as $key=>$value)
{
$data .= '"'.$key.'"'.', '.$value.', ';
}
$data = rtrim($data,', ');
$query = 'UPDATE table_1 set category_dynamic = COLUMN_CREATE(' . $data . ') where id = 1';
Hope it will solve the problem.
You could use a combination of array_map and implode for that.
$test = array(
'Subscription' => '1',
'Streaming' => '1',
'Download' => '0'
);
$list = implode(',', array_map(function ($v, $k) { return "'".$k."'" . ',' . $v; }, $test, array_keys($test)));
$query = 'UPDATE table_1 set category_dynamic = COLUMN_CREATE(' . $list . ') where id = 1';
$this->Model->query($query);

PHP Insert Values if they are set

Looking for a little guidance as I don't know how to echo all my set options into an asana request. So I have an array from a user submitted form:
Array
(
[_wpcf7] => 5
[_wpcf7_version] => 4.1.2
[_wpcf7_locale] => en_US
[_wpcf7_unit_tag] => wpcf7-f5-o1
[_wpnonce] => 1531937d4e
[your-name] => TEST
[your-email] => test#test.com
[title] => test
[Description] => test
[venues] => Dragonfly
[duedate] => 2015-04-11
[options] => Array
(
[0] => Print Business Card (3.5x2)
[1] => Google Ads Package
[2] => Print Ticket (1.5x5.5)
[3] => Print Magazine Ad (8x10.75)
)
[_wpcf7_is_ajax_call] => 1
[upload] => ditch_fridays_logo.png
)
From there I parse the array to grab the variables:
$submittedBy = $posted_data['your-name'];
$eMail = $posted_data['your-email'];
$projectTitle = $posted_data['title'];
$projectDescription = $posted_data['Description'];
$venue = $posted_data['venues'];
$deadline = $posted_data['duedate'];
$options = $posted_data['options'];
$upload = $posted_data['upload'];
And then I create the asana task
// First we create the task
$result = $asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => $projectTitle, // Name of task
'assignee' => 'somewhere#somewhere.com', // Assign task to...
'due_on' => $deadline,
'notes' => 'Submitted By: ' . $submittedBy . "\n" . 'E-Mail: ' . $eMail . "\n\n" . '--------------------------------------------------' . "\n\n" . 'Task Name: ' . $projectTitle . "\n\n" . 'Description: ' . $projectDescription . "\n\n" . 'Venue: ' . $venue . "\n\n" . 'Deadline: ' . $deadline . "\n\n" . 'Attachments: http://inkentertainment.com/graphics/saved/' . $upload
));
The problem is, I cant include the selected options in the notes part of the task creation. How would I go about including all the options ($posted_data['options']) only if the user has selected them? (on the form there is a list of about 10, but the users will select 2 or 3)
I hope I have been clear enough for you to understand, if not let me know and I will try and clarify
$options = ( isset ( $posted_data[ 'options' ] ) &&
is_array( $posted_data[ 'options' ] ) ) ?
$posted_data[ 'options' ] : array();
here you'll have an array with zero or more elements.
If you need a string you may then use implode
http://php.net/manual/en/function.implode.php
ex: $options = implode( ', ', $options );

Multiple OR conditions in CakePHP v1.3.15

I have issues with OR conditions in cake php query builder. This will return 0 result.
$results = $this->paginate('Did', array('Did.ivr_id LIKE ' => $number . "%",'OR'=>array('Did.did LIKE ' => $number . "%")));
$result = $this->paginate = array(array('Did.did LIKE' => $number . "%"));
$this->set('dids', $results);
When i apply condition on simple column it gives accurate result
$results = $this->paginate('Did', array('Did.ivr_id LIKE' => $number . "%"));
$result = $this->paginate = array(array('Did.did LIKE' => $number . "%"));
$this->set('dids', $results);
You need to put all the conditions that you want to OR inside the OR array.
$results = $this->paginate(
'Did',
array(
'OR'=>array(
'Did.ivr_id LIKE ' => $number . "%",
'Did.did LIKE ' => $number . "%"
)
)
);

Concatenating PHP strings from an iteration

I'm working on some Database functions and, for some reason, I made two functions to make simple inserts/selects with arrays passed as arguments to a php function. The problem starts here.
// basic database functions, to aid the development :D
static public function Insert($table, $items = array())
{
if(is_array($items) == false)
{
return false;
}
$header = 'INSERT INTO ' . $table . '(';
$values = 'VALUES(';
$count = count($items) - 1;
$cur = 0;
foreach($items as $key => $value)
{
$num = is_numeric($value);
$header .= $key . ($cur < $count) ? ', ' : ' ';
$values .= (($num) ? '' : '\'') . $value . (($num) ? '' : '\'') . (($cur < $count) ? ', ' : '');
$cur ++;
}
$header .= ')';
$values .= ')';
self::Query($header . $values);
}
$pair = array('id' => null,
'name' => Database::EscapeString((string)$xml->ID),
'avatar_url' => Database::EscapeString((string)$xml->Avatar),
'personal_msg' => Database::EscapeString((string)$xml->AboutMe),
'level' => (int)$xml->Level,
'progress' => (int)$xml->Progress,
'trophies_total' => (int)$xml->Trophies->Total,
'trophies_platinum' => (int)$xml->Trophies->Platinum,
'trophies_gold' => (int)$xml->Trophies->Gold,
'trophies_silver' => (int)$xml->Trophies->Silver,
'trophies_bronze' => (int)$xml->Trophies->Bronze,
'country' => Database::EscapeString((string)$xml->Country->Culture),
'is_plus' => (string)$xml->Plus,
'points' => (int)$xml->LevelData->Points,
'floor' => (int)$xml->LevelData->Floor,
'ceiling' => (int)$xml->LevelData->Ceiling,
'game_completion' => (double)$xml->GameCompletion,
'background_color' => ((int)$xml->Background->R << 16) | ((int)$xml->Background->G << 8) | ((int)$xml->Background->B),
'jid' => (string)$xml->jid
);
Insert('profiles', $pair);
The data that the query contains is not correct:
INSERT INTO profiles(, , , , , , , , , , , , , , , , , , , )VALUES('', 'AlmamuPP', 'http://static-resource.np.community.playstation.net/avatar/3RD/30004.png', 'EVE Online & DUST fan', 2, 17, 12, 0, 0, 6, 6, 'ES', 'false', 270, 200, 600, 12.5, 458759, 'AlmamuPP#a4.es.np.playstation.net')
As you may notice, it just ignores all the keys in the dictionary, but the funny fact is that if I add
echo $key;
in the foreach the keys are printed as they should... Any idea about what could be hapening there and a way to fix it?
You are missing parenthesis:
$header .= $key . (($cur < $count) ? ', ' : ' ');
Like this it will work.

Categories