Find product in database with "like" statement in cakephp - database

I have a function that searches in the database all products with the description like the user search, but it is not working. Where is the problem?
public function searchProduct(){
$search = $this->request->query('search');
$products = $this->Product->find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
if($this->Auth->user()!= null){
$userId = $this->Auth->user('id');
$user = $this->User->findById($userId);
$filters = $user['Filter'];
$arrayFiltersOfUser = array();
$arrayOfProductIds = array();
for($i = 0; $i < count($filters); $i++){
$arrayFiltersOfUser[$i] = $filters[$i]['id'];
}
for($i = 0; $i < count($products); $i++){
$arrayOfProductIds[$i] = $products[$i]['Product']['id'];
}
for($i = 0; $i < count($arrayOfProductIds); $i++){
$arrayFiltersOfProduct_aux[$i] = $this->FiltersProduct->findAllByProductId($arrayOfProductIds[$i]);
$array = $arrayFiltersOfProduct_aux[$i];
for($j = 0; $j < count($array); $j++){
$array2[$j] = $array[$j]['FiltersProduct']['filter_id'];
}
$containsAllValues = !array_diff($arrayFiltersOfUser, $array2);
if($containsAllValues == true){
$this->set("is_valid", "yes");
$this->set("_serialize", array("is_valid"));
}
else{
$this->set("is_valid", "no");
$this->set("_serialize", array("is_valid"));
}
}
}
$this->set("response", $products);
$this->set("_serialize", array("response"));
}

You're now searching for: %$search% and not the value of $search.
Try to replace
find("all",array('conditions'=>array('Description LIKE'=>"%$search%")));
with
find("all",array('conditions'=>array('Description LIKE'=>"%".$search."%")));
This should do the trick.

Related

How to map 2 arrays of object using PowerShell?

I would like to map an array object. I need to find the matched object of array. After the object match, I need to stop checking other objects, then continue to the next process.
I tried this, but it always return not match, even if the array object match exists.
$ID = #("8537", "8538", "8539", "8540", "85AC", "85DE", "82EA")
$Signal = #("8537", "8220")
for ($i = 0; $i -lt $Signal.count; $i++)
{
if ($Signal[$i] -like "$ID[$i]")
{
"found ‘$($Signal[$i])’
at index $i"
# Do some process
}
else {
"Not Match"
# Do some process
}
}
If you need to go Match process if exists at least 1 match object, you can try this:
$ID = #("8538", "8539", "8540", "85AC", "85DE", "82EA","8537")
$Signal = #("8537","8220","85DE")
$matched = $false
for ($i = 0; $i -lt $ID.count; $i++) {
if ($matched) {
break
}
for ($j = 0; $j -lt $Signal.count; $j++) {
if ($ID[$i] -like $Signal[$j])
{
$matched = $true
"found $($ID[$i]) at index $i"
break
}
}
}
if ($matched) {
# Do some process
} else {
"Not Match"
# Do some process
}
Something like that you should test :
cls
$ID = #("8538", "8539", "8540", "85AC", "85DE", "82EA","8537")
$Signal = #("8537","8220","85DE")
for ($i = 0; $i -lt $ID.count; $i++) {
for ($j = 0; $j -lt $Signal.count; $j++) {
if ($ID[$i] -like $Signal[$j])
{
"found $($ID[$i]) at index $i"
# Do some process
}
else
{
"Not Match"
# Do some process
}
}
}
As mentioned in my comment above - to make your example work you can use a nested loop like this:
$ID = #("8537", "8538", "8539", "8540", "85AC", "85DE", "82EA")
$Signal = #("8234", "8512", "8220")
$MatchFound = $false
for ($i = 0; $i -lt $Signal.count; $i++) {
for ($x = 0; $x -lt $ID.Count; $x++) {
if ($Signal[$i] -eq $ID[$x]) {
"found '$($Signal[$i])' at index $i"
$MatchFound = $true
}
}
}
if (-not $MatchFound) {
"No match was found"
}
If you have a complex object you want to compare you can take a look at the cmdlet Compare-Object.

Unable to find if one item exists in array of items and return the necessary message in Perl

I have array of IDs. I have one ID which I want to find if that ID exists in the array of IDs in Perl
I tried the following code:
my $ids = [7,8,9];
my $id = 9;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
} else {
print 'no';
}
}
I get the output as:
nonoyes
Instead I want to get the output as only:
yes
Since ID exists in array of IDs
Can anyone please help ?
Thanks in advance
my $ids = [7,8,9];
my $id = 9;
if (grep $_ == $id, #ids) {
print $id. " is in the array of ids";
} else {
print $id. " is NOT in the array";
}
You just need to remove the else part and break the loop on finding the match:
my $flag = 0;
foreach my $new_id (#$ids) {
if ($new_id == $id) {
print 'yes';
$flag = 1;
last;
}
}
if ($flag == 0){
print "no";
}
Another option using hash:
my %hash = map { $_ => 1 } #$ids;
if (exists($hash{$id})){
print "yes";
}else{
print "no";
}
use List::Util qw(any); # core module
my $id = 9;
my $ids = [7,8,9];
my $found_it = any { $_ == $id } #$ids;
print "yes" if $found_it;
The following piece of code should cover your requirements
use strict;
use warnings;
my $ids = [7,8,9];
my $id = 9;
my $flag = 0;
map{ $flag = 1 if $_ == $id } #$ids;
print $flag ? 'yes' : 'no';
NOTE: perhaps my #ids = [7,8,9]; is better way to assign an array to variable

Laravel - Save multiple field values in json/array in a column

I have 3 form fields name, address, profile_photo. I want to save there 3 fields in json/array format in a single column so that I can retrieve it later in view blade.
My form looks like this
I tried
$customer_details= new Customer;
{
$profile_photo = $request->file('profile_photo');
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$cust_details=json_encode(request(['name', 'address', $profile_ph]));
$customer_details->details = $cust_details;
$customer_details->save();
}
With this profile_photo is not saved but other data are saved as:
{"name":["john","Sam"],"address":["CA","FL"]}
How can I save all there fields including profile_photo and show each details distinctly later on view blade?
Hope this will help you.
$customer_details = new Customer;
$profile_photo = $request->file('profile_photo');
$profile_ph = [];
for ($i = 0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph[$i] = '/images/' . $pro_fileNameToStore;
}
$data = [
'name' => $request->name,
'address' => $request->address,
'profile_ph' => $profile_ph
];
$customer_details->details = json_encode($data);
$customer_details->save();
The details column should be text if you are using mysql.
Try this:
$names = $request->input('name');
$addresses = $request->input('address');
$profile_photos = $request->file('profile_photo');
for ($i = 0; $i < count($names); $i++) {
$customer_details = new Customer;
$pro_fileExt = $profile_photos[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photos[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph = '/images/' . $pro_fileNameToStore;
$cust_details = json_encode([
'name' => $names[$i],
'address' => $addresses[$i],
'profile_photo' => $profile_ph,
]);
$customer_details->details = $cust_details;
$customer_details->save();
}
Try this with single database query
$profile_photo = $request->file('profile_photo');
$finalData = [];
$names = [];
$addresses = [];
$images = [];
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$names[] = $request->input('name');
$addresses[] = $request->input('address');
$images[] = $profile_ph;
}
$cust_details=json_encode(['name' => $names, 'address' => $addresses, 'images' => $images]);
$customer_details->insert($cust_details);

How to get selected value in dropdownlist by using cakephp?

My code is something like below :
public function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
And here is the drop down list code :
$this->Form->select('ClubOpenDay.0.open_time', $this->makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
My problem is I am getting the value like 11 am, 12 am.But I want to make it selected when I will get value 11 am or 12 am from database.Any idea how can I do this?
Please try this:
<?php echo $this->Form->select('ClubOpenDay.0.open_time',$this->makeEntryTime(),array("empty" => false,'value' => $value_selected,'class' => 'input-medium')); ?>
"$value_selected" is any value with in the input range.
i.e what $this->makeEntryTime() returns.
Thanks
I assume your method is in view which you shouldn't do this. remove public from your function and remove $this to call your function.
try to keep your method inside a controller at least. Here is if you want to keep your method in view.
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
echo $this->Form->select('selete_id', makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
but if you want to make it MVC.
//controller
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
//page function - for examlpe add() method
public function add(){
$this->set('times', $this->makeEntryTime());
}
//view of add method
echo $this->Form->select('time_id', $times, array("empty" => false, 'class' => 'input-medium'));

Why I can't get values from this web service Array?

Can't figure out why this web service don't work. Just gives me blank. I tested the url and the data it's all there.
http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0
my code:
session_start();
function getNews() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0');
$data = json_decode($json, TRUE);
$newst = array();
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
foreach($newst as $v)
{
$_SESSION['newsid'][] = $v['id'];
$_SESSION['newstitle'][] = $v['title'];
$_SESSION['newstext'][] = $v['news'];
$_SESSION['newslink'][] = $v['link'];
$_SESSION['newsdate'][] = $v['date'];
$_SESSION['newsentityName'][] = $v['entityName'];
$_SESSION['aclikes'][] = $v['account']['likes'] . ")";
$_SESSION['acdislikes'][] = $v['account']['dislikes'] . ")";
$_SESSION['accomentes'][] = $v['account']['commentes'] . ")";
$_SESSION['acshares'][] = $v['account']['shares'] . ")";
$_SESSION['acclicks'][] = $v['account']['clicks'] . ")";
}
}
getNews();
$key = count($_SESSION['newsid']);
for ($i = 0; $i <= $key; $i++) {
echo $_SESSION['newsid'][$i] . "<br />";
}
Are you sure that this line is correct:
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
Reading this makes me think that you are overwriting the entire $newst array with a single item... for each item. Thus the array will end up being the value of the last $item (which might be empty).
I'd expect something like:
foreach($data['data']['item'] as $item) {
$newst.push( $item );
}
(note, not tested and my syntax may be dodgy... but you get the drift).

Resources