how to check the value of an array in codeigniter - arrays

$result=array();
$count = count($days);
for($i=0;$i<$count-1; $i++)
{
$bookDate = $days[$i];
$fromDate = $this->booking_model->Get_BookedDate($bookDate,$roomname);
if ($fromDate != 0)
{
$result[] = $bookDate;
}
}
return $result;
This is my codeigniter controller function here I want to check the value of $fromDate is zero or not. I am getting the array like this
Array ( [0] => Array ( ["2013-04-27" between fromDate and toDate] => 1 ) )
I am very new to codeigniter. Thank you for any help.

$result=array();
$count = count($days);
for($i=0;$i<$count-1; $i++)
{
$bookDate = $days[$i];
$fromDate = $this->booking_model->Get_BookedDate($bookDate,$roomname);
if (!empty($fromDate[0]['"'.$bookDate.'" between fromDate and toDate']))
{
$result[] =$bookDate;
}
}
return $result;
Try this.IT may help you

Make use of empty() function.
if(!empty($fromdate)) { //if $formdate is empty or 0 or false the loop won't be executed
//Your code.
}

if (!empty($fromDate))
{
$result[] = $bookDate;
}
Try this.

Related

Array check if object key has same value

I want to push values from old array to a new array. when pushing I am checking the object value is already exist or not. the value should be pushed only when the same object value is not exist in new array. here is my code.
$sorted_array = array();
foreach ($data as $nkey => $value) {
if (count($sorted_array) > 0) {
dd($sorted_array[$nkey]);
if ($sorted_array[$nkey]['store'] != $value['store']) {
array_push($sorted_array, $value);
}
}
else{
array_push($sorted_array, $value);
}
}
If I understood correctly, you can easily handle it using the in_array function:
$sorted_array = array();
foreach ($data as $nkey => $value) {
if ( !in_array($value, $sorted_array) ) {
array_push($sorted_array, $value);
}
}
in_array function return boolean value. In my code, if the $value parameter is not in the $sorted_array, then it will pushed. Otherwise operation is not performed.
array_push in this case is wrong.
Try this code:
foreach ($data as $nkey => $value) {
if (count($sorted_array) > 0) {
if ($sorted_array[$nkey]['store'] != $value['store']) {
$sorted_array[$nkey] = $value;
}
}
}
Hope can help you

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

How can i get top 15 occurrences in an array and use each value to fetch data from mysql database?

I am currently working on a project (A music website) with PHP Codeigniter. I want to fetch top 15 songs by number of downloads. To my knowledge, I got all unique ID's out ranging from highest to least. But now, I want to use each of these unique ID's to fetch data from another table in the database.
I have tried using for loop; but it seems to only return one of the rows which happens to be the first and highest number of occurrence in the array. The code below echo's the ID's but how can I use each of those ID's to fetch data.
function top15(){
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row)
{
$musiccode[] = $row['musiccode'];
}
$mode = array_count_values($musiccode);
arsort($mode);
$i = 0;
foreach ($mode as $field => $number) {
$i++;
echo $field." occured ". $number." times <br>";
if ($i == 15) {
break;
}
}
}
}
for ($b=0; $b < count($field); $b++) {
$this->db->where(array('track_musiccode'=> $field));
$field = $this->db->get('tracks');
return $field->result_array();
}
The code above only produce one row for me. I expect to have more than one and according to the number of ID's in the array. Thanks in advance.
For a single column condition, you could use where_in() instead and drop the for loop :
$this->db->where_in('track_musiccode', $field);
$field = $this->db->get('tracks');
return $field->result_array();
Yes Finally i did it. Incase if anyone needs it. this works just fine
function top15(){
$this->db->where('d_month', date('m'));
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row){
$musiccode[] = $row['musiccode'];
}
$res='';
$count=array_count_values($musiccode);
arsort($count);
$keys=array_keys($count);
for ($i=0; $i < count($keys); $i++) {
$res[] .= "$keys[$i]";
}
$this->db->where_in('track_musiccode', $res);
return $this->db->get('tracks')->result_array();
}
}
And I got this too. If I wasn't going to count occurrences but just iterate the number of downloads which is far more better than counting in other not to over populate the database and make your website work slowly.
function top15(){
$tracks = '';
$this->db->group_by('counter');
$result=$this->db->get('downloads');
$query = $result->result_array();
$counter = '';
$fields = '';
if(!empty($query)) {
foreach($query as $row) {
$counter[] = $row['counter'];
}
rsort($counter);
for ($i=0; $i<=15; $i++) {
$this->db->where('downloads.counter', $counter[$i]);
$this->db->join('tracks', 'tracks.musicid = downloads.musicid');
$fields[] = $this->db->get('downloads')->row_array();
}
return $fields;
}
}
Enjoy...

PHP uksort function using global variable fails after PHP upgrade to 5.3.3

I have a user defined sort function which uses a 'global' declaration in order to refer to a multi-dimensional array when deciding on the correct sort order. It used to work fine under PHP 5.1.6 but now fails under 5.3.3.
The code throws PHP warning:
PHP Warning: uksort(): Array was modified by the user comparison function
But the code definitely does not modify the array.
This code duplicates the problem:
$arr = array();
$arr['i1']['val1'] = 99;
$arr['i1']['val2'] = 100;
$arr['i2']['val1'] = 89;
$arr['i2']['val2'] = 101;
function cmp($a, $b)
{
global $arr;
if ($arr[$a]['val2'] > $arr[$b]['val2']) { return 1; }
if ($arr[$a]['val2'] < $arr[$b]['val2']) { return -1; }
return 0;
}
if (uksort($arr, 'cmp'))
{
echo "success";
}
else
{
echo "failure";
}
If you're not going to sort by the actual keys, don't use uksort but usort or uasort:
function cmp($a, $b) {
return $a['val2'] - $b['val2'];
}
uasort($arr, 'cmp');
As sidestepping the issue is so popular, here is the cause of your issue:
global $arr;
I'm sure you sensed that aready, to solve that, remove the line and replace $arr with $GLOBALS['arr']. This removes the modification message and you'er accessing the global variable from the symbol table and not the one uksort is currently operating on.
Example:
<?php
$arr = array();
$arr['i1']['val1'] = 99;
$arr['i1']['val2'] = 100;
$arr['i2']['val1'] = 89;
$arr['i2']['val2'] = 101;
function cmp($a, $b)
{
if ($GLOBALS['arr'][$a]['val2'] > $GLOBALS['arr'][$b]['val2']) { return 1; }
if ($GLOBALS['arr'][$q]['val2'] < $GLOBALS['arr'][$b]['val2']) { return -1; }
return 0;
}
if (uksort($arr, 'cmp'))
{
echo "success\n";
}
else
{
echo "failure\n";
}
print_r($arr);
Output for 5.3.23 - 5.5.3: (other versions crash/misbehave)
success
Array
(
[i1] => Array
(
[val1] => 99
[val2] => 100
)
[i2] => Array
(
[val1] => 89
[val2] => 101
)
)
Ups, maybe that's why you see a warning in more recent versions: (Demo: http://3v4l.org/DkK3v)

How to delete Model Condition?

I wrote a function which is supposed to return an array of clubs for userId. I don't know why by when I add where(Model_ClubUsers::$USERS_ID,$userId) to dsql() it doesn't set the condition and I have to use addCondition(), but I need it only in this function. Is there a way to delete the condition before function returns?
function getUserClubs($userId){
$columns = array('id',self::$NAME,self::$LOGO_NAME,self::$DESC);
$this->addRelatedEntity('club_users', 'club_users', Model_ClubUsers::$CLUBS_ID, 'inner', 'related');
$this->addField(Model_ClubUsers::$USERS_ID)->relEntity('club_users');
$aAliasedColumns = $this->getAliasedColumns($columns, $this->entity_code);
$this->addCondition(Model_ClubUsers::$USERS_ID,$userId);
$rows = $this->dsql()->field($aAliasedColumns)->do_getAll();
$aResult = array() ;
foreach($rows as $key => $value){
foreach($value as $vKey => $vVal){
if($columns[$vKey]=='id'){
$aRow['key'] = $vVal;
}else if($columns[$vKey]==self::$LOGO_NAME){
$aRow[$columns[$vKey]] = self::$CLUBS_LOGO_PATH.$vVal;
}
else {
$aRow[$columns[$vKey]] = $vVal;
}
}
$aResult[] = $aRow;
}
return $aResult;
}
Please upgrade to 4.2 where the problem is no longer present:
$x=clone $this;
$x->addCondition();
also your syntax can be probably improved with the new 4.2 features.

Resources