Multiple where condition codeigniter active record - php

I want to make a multiple where query with AND and OR. Suppose my array is:
array (size=8)
0 => array (size=2)
'timeline.type' => string 'videos' (length=6)
'timeline.sourceId' => string '7' (length=1)
1 => array (size=2)
'timeline.type' => string 'loadshedding' (length=12)
'timeline.sourceId' => string '5' (length=1)
2 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '3' (length=1)
3 => array (size=2)
'timeline.type' => string 'news' (length=4)
'timeline.sourceId' => string '5' (length=1)
The above array could be dynamic. I want to make active record query like:
(timeline.type = 'videos' AND timeline.sourceId = 7) OR (timeline.type = 'loadshedding' AND timeline.sourceId = 5) OR (timeline.type = 'news' AND timeline.sourceId = 3) // and so on.
I tried :
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
foreach($sources as $source){ //$sources is an array like given above
$this->db->or_where($source);
}
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);
But when I echo $this->db->last_query(); to see the query. It returns SELECT * FROM timeline only. What might be the problem. Thank you.

Your Code Should be like
$sources = array (
0 => array (
'timeline.type' => 'videos',
'timeline.sourceId' => '7' ),
1 => array (
'timeline.type' => 'loadshedding',
'timeline.sourceId' => '5' ) ,
2 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '3' ),
3 => array (
'timeline.type' => 'news',
'timeline.sourceId' => '5')
);
end($sources); // move the internal pointer to the end of the array
$lastkey = key($sources);
$pr = "";
foreach($sources as $key=>$source)
{
if($key != $lastkey)
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ") OR ";
}
else
{
$pr.="( `timeline.type` = '". $source["timeline.type"] ."' and `timeline.sourceId` = " . (int) $source["timeline.sourceId"] . ")";
}
}
$this->db->select('timeline.id as postid, timeline.*, sources.*, subscription.*')->from('timeline');
$this->db->join('sources', 'sources.type = timeline.type and timeline.sourceId = sources.id');
$this->db->join('subscription', 'subscription.type = timeline.type and timeline.sourceId = subscription.sourceId');
$this->db->where($pr);
$this->db->order_by("timeline.id", "DESC");
$this->db->limit(15);

Related

Merge inner PHP arrays into one multidimensional array - No duplicates basing on Key value

I have tried a number of PHP functions like array_unique to merge the arrays I have but that does not work since these are not strings.
The starting output would be this on var_dump( $country_cities );
array (size=3)
0 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7886
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Laurence' (length=8)
'city_name_arabic' => string '3684hdfpfwbhisf' (length=15)
1 =>
array (size=1)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
2 =>
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
The Code below merges the different arrays above.
<?php
// Make the cities unique. Remove duplicates form the inner array.
$uniques = [];
foreach($country_cities as $arr ) {
foreach( $arr as $v) {
if( !in_array($v, $uniques, false) ) {
$uniques[$v['country']] = $v;
}
}
}
var_dump($uniques);
Results logged show the code cuts out some of the arrays.
/Users/..... on line php:74:
array (size=2)
'BH' =>
array (size=4)
'post_id' => int 7885
'country' => string 'BH' (length=2)
'city_name_eng' => string 'Bahrain City' (length=12)
'city_name_arabic' => string 'vgdg824762' (length=10)
'KW' =>
array (size=4)
'post_id' => int 7841
'country' => string 'KW' (length=2)
'city_name_eng' => string 'Kuwait City' (length=11)
'city_name_arabic' => string ' مدينة الكويت' (length=24)
Please help figure out my error or suggest a better way to fix it.
If I understood you correctly, this should be the result you wanted to achieve.
Each country code will be included once, while the cities (based on post_id) will only be added once.
<?php
$result = [];
$processedIds = [];
foreach ($country_cities as $ccs) {
foreach ($ccs as $cc => $details) {
// If the country has not yet been added to
// the result we create an outer array.
if (!key_exists($cc, $result)) {
$result[$cc] = [];
}
$postId = $details['post_id'];
if (!in_array($postId, $processedIds)) {
// Add the unique city to country's collection
$result[$cc][] = $details;
// Keep track of the post_id key.
$processedIds[] = $postId;
}
}
}
print_r($result);
<?php
//...
$uniques = [];
foreach($country_cities as $data ) {
foreach( $data as $countryCode => $item) {
if( !array_key_exists($countryCode, $uniques) ) {
$uniques[$countryCode] = []; //Create an empty array
}
$targetArray = &$uniques[$countryCode];
if( !array_key_exists($item['post_id'], $targetArray) ) {
$targetArray[$item['post_id']] = $item; //Create an empty array
}
}
}
Maybe this code will help?

Sums records inside foreach loop PHP

I'm using php laravel, Lets suppose below $records is an array and has only 3 records, the foreach loop will run 3 times.
foreach ($records as $record) {
// Here im using query to get logs against each record id.
$logs = "SELECT delivery, COUNT(*) as logs FROM logs where id =
$record['id] group by delivery";
// Run query and it returns array
var_dump($logs)
}
This dump query gets records like this..
1st time
array (size=2)
0 =>
array (size=2)
'delivery' => string '0' (length=1)
'logs' => string '1' (length=1)
1 =>
array (size=2)
'delivery' => string '1' (length=1)
'logs' => string '1' (length=1)
2nd time
array (size=1)
0 =>
array (size=2)
'delivery' => string '1' (length=1)
'logs' => string '1' (length=1)
3rd time
array (size=2)
0 =>
array (size=2)
'delivery' => string '0' (length=1)
'logs' => string '1' (length=1)
1 =>
array (size=2)
'delivery' => string '1' (length=1)
'logs' => string '1' (length=1)
I want to calculate records like
Total SMS; // In above example, total are 5
Total Delivered; // In above example, Delivered are 3
Total Failed: // In above example, Failed are 2
Here is how i'm calculating
If delivery = 0 , it means failed, if delivery = 1, it means delivered, So there are 3 delivered and 2 failed in above scenario.
Here is what i have tried but it not giving me correct result.
$logsSENT = $logsFailed = 0;
foreach ($logs as $log) {
if($log['delivery'] == 1) {
$logsSENT += $log['logs'] * $smsConsumedPerMessage;
}
if($log['delivery'] == 0) {
$logsFailed += $log['logs'] * $smsConsumedPerMessage;
}
}
$total_sms += $logsSENT + $logsFailed;
BTW 'logs' => string '1' (length=1) Logs value can be more than 1, It can be 2, 3 etc..
Secondly, In my IFs I'm multiplying it with 1 for example
$logsSENT = $logs[1]['logs'] * 1; 1 is only for above example, It can be changed later to 2, 3 etc
<?php
$array = array(
array(
array("delivery" => 0, "logs" => 1),
array("delivery" => 1, "logs" => 1)
),
array(
array("delivery" => 1, "logs" => 1)
),
array(
array("delivery" => 0, "logs" => 1),
array("delivery" => 1, "logs" => 1)
)
);
$sum = $deliver = $failed = 0;
foreach ($array as $value) {
$i=$j=1;
foreach ($value as $va) {
if($va['delivery']) {
$deliver = $deliver + $i;
$i++;
} else {
$failed = $failed + $j;
$j++;
}
}
}
echo 'Total:' .($deliver + $failed).'<br>';
echo 'Deliver: '.$deliver.'<br>';
echo 'Failed: '.$failed.'<br>';
?>

Write on $_POST an array inside DB like result

I have a problem on $weight_class_title.
When the data is saved it wrote in the database array.
Tk.
the result of var_dump($sql_data_array);
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 1
array (size=4)
'weight_class_title' =>
array (size=1)
'weight_class_title' => string 'tt' (length=2)
'weight_class_key' => string 'pound' (length=5)
'weight_class_id' => int 5
'language_id' => int 2
public function execute() {
$CLICSHOPPING_Language = Registry::get('Language');
$CLICSHOPPING_Db = Registry::get('Db');
$languages = $CLICSHOPPING_Language->getLanguages();
$QlastId = $CLICSHOPPING_Db->prepare('select weight_class_id
from :table_weight_classes
order by weight_class_id desc
limit 1');
$QlastId->execute();
$weight_class_id = $QlastId->valueInt('weight_class_id') + 1;
$weight_class_key = $_POST['weight_class_key'];
for ($i=0, $n=count($languages); $i<$n; $i++) {
$weight_class_title_array = $_POST['weight_class_title'];
$language_id = $languages[$i]['id'];
$weight_class_title_array = ['weight_class_title' => HTML::sanitize($weight_class_title_array[$language_id])];
$sql_data_array = ['weight_class_title' => $weight_class_title_array];
$insert_sql_data = ['weight_class_key' => $weight_class_key,
'weight_class_id' => $weight_class_id,
'language_id' => $languages[$i]['id']
];
$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
$this->app->db->save('weight_classes', $sql_data_array);
}

Store value in multi-dimentional array on unique key

How do I display the following using the array shown below?
Pastors
key=>0, member_id, member_name
Deacons
key=>1, member_id, member_name
key=>2, member_id, member_name
Here is the array.
array (size=3)
0 =>
array (size=4)
'category_name' => string 'Pastors' (length=7)
'member_id' => string '3' (length=1)
'member_name' => string 'Tiny Marshall' (length=13)
'member_email' => string 'jconley#nowhere.com' (length=19)
1 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '1' (length=1)
'member_name' => string 'Jeremiah Conley' (length=15)
'member_email' => string 'jconley#nowhere.com' (length=19)
2 =>
array (size=4)
'category_name' => string 'Deacons' (length=7)
'member_id' => string '2' (length=1)
'member_name' => string 'Marcy Conley' (length=12)
'member_email' => string 'jconley#nowhere.com' (length=19)
Here is the code that I used to build the array:
while( $row = mysql_fetch_assoc( $result ) )
{
$staff[$i] = array
(
'category_name' => $row['category_name'],
'member_id' => $row['member_id'],
'member_name' => $row['member_name'],
'member_email' => $row['member_email'],
);
$i++;
}
This is the final solution:
$category_name = array();
foreach ($staff as $member) {
if (!in_array( $member['category_name'], $category_name ))
{
echo '<h1>' . $member['category_name'] . '</h1>';
$category_name[] = $member['category_name'];
}
echo $member['member_id'] . ', ' . $member['member_name'] . '<br />';
}
Use foreach to loop over your $staff array.
$pastors = array();
$deacons = array();
foreach ($staff as $member) {
if ($member['category_name'] == 'Pastors') {
$pastors[] = $member['member_id'] . ', ' . $member['member_name'];
} else if ($member['category_name'] == 'Deacons') {
$deacons[] = $member['member_id'] . ', ' . $member['member_name'];
}
}
documentation

how to draw statistics line chart in my admin panel using php?

i want to show my chart in my admin panel using GD library using php ..
i get date from my database but when i use any library to show this result in line chart it did not work & it did not show any errors.i can not get what i did wrong?
can anyone help me?
<?php
include ("../setting.php");
include("../classes/phpgraphlib.php");
$result = mysql_query("SELECT DATE_FORMAT(log_date, '%Y-%m-%d') as Date,count(*) as Count FROM log
where operation_type='Add' and user_id='82' and faculty_code='0' group by log_date order by log_date") or die(mysql_error());
$MultiDimArray = array();
$data = array();
while( $row = mysql_fetch_array($result , MYSQL_BOTH) )
{
$MultiDimArray[] = array ( 'Date' => $row['Date'], 'Count' => $row['Count'] );
}
foreach($MultiDimArray as $value)
{
if ( !array_key_exists($value['Date'], $data) ) {
$data[$value['Date']] = $value['Count'];
}
else{ $data[$value['Date']] += (string)$value['Count'];}
}
$data = array_map('strval', $data);
$graph = new PHPGraphLib(400,300);
$graph->addData($data);
$graph->setTitle("Site Statistics");
$graph->setBars(false);
$graph->setLine(true);
$graph->setDataPoints(true);
$graph->setDataPointColor("maroon");
$graph->setDatadata(true);
$graph->setDataValueColor("maroon");
$graph->setGoalLine(.0025);
$graph->setGoalLineColor("red");
$graph->setXdataHorizontal(true);
$graph->createGraph();
?> `
the output of $data array is like that:
array'2013-05-13' => string '7' (length=1)'2013-05-15' => string '3' (length=1)'2013-05-16' => string '5' (length=1)'2013-05-18' => string '8' (length=1)'2013-05-19' => string '2' (length=1)'2013-05-21' => string '2' (length=1)'2013-05-22' => string '10' (length=2)'2013-05-23' => string '2' (length=1)'2013-05-25' => string '1' (length=1)'2013-05-26' => string '8' (length=1)'2013-05-27' => string '19' (length=2)'2013-05-28' => string '7' (length=1)'2013-05-29' => string '3' (length=1)'2013-06-02' => string '11' (length=2)'2013-06-03' => string '2' (length=1)'2013-06-04' => string '7' (length=1)'2013-06-05' => string '3' (length=1)'2013-06-06' => string '2' (length=1)'2013-06-09' => string '15' (length=2)'2013-06-10' => string '5' (length=1)'2013-06-11' => string '1' (length=1)'2013-06-12' => string '3' (length=1)'2013-06-15' => string '10' (length=2)'2013-06-16' => string '3' (length=1)'2013-06-19' => string '2' (length=1)
I'd suggest using JS for that, feeding it with data from PHP. Take a look at Flot or something similar.

Categories