Generate Custom array when Select using relation table on mysql - php

i have a problem when i want to select data from database.
this is my query
SELECT J.id as id_job, J.title as job_title, J.companies_id, C.name as company_name,J.created_at, L.name as Location,CL.postal_code, CL.address
FROM jobs J
INNER JOIN bla1 C ON ( C.id = J.company_id )
INNER JOIN bla2 CL ON ( CL.id = J.company_location_id )
INNER JOIN bla3 L ON ( L.id = CL.location_id )
WHERE J.status =1
AND C.status =2
And CL.status = 1
AND L.status = 1
ORDER BY J.created_at DESC
LIMIT 0 , 30
and this is the result of that query using var_dump()
["_result":protected]=>
array(15) {
[0]=>
array(8) {
["id_job"]=>
string(2) "27"
["job_title"]=>
string(10) "YUYUYUYUYU"
["company_id"]=>
string(1) "3"
["company_name"]=>
string(16) "voyage indonesia"
["created_at"]=>
string(19) "2012-10-03 09:35:37"
["Location"]=>
string(4) "Bali"
["postal_code"]=>
string(6) "989767"
["address"]=>
string(11) "dimana mana"
}
[1]=>
array(8) {
["id_job"]=>
string(2) "23"
["job_title"]=>
string(11) "OYEEEEAAHHH"
["company_id"]=>
string(1) "3"
["company_name"]=>
string(16) "voyage indonesia"
["created_at"]=>
string(19) "2012-09-28 09:06:54"
["Location"]=>
string(7) "Bandung"
["postal_code"]=>
NULL
["address"]=>
NULL
}
[2]=>
array(8) {
["id_job"]=>
string(2) "20"
["job_title"]=>
string(14) "TKPDKJKLADKSJA"
["company_id"]=>
string(1) "3"
["company_name"]=>
string(16) "voyage indonesia"
["created_at"]=>
string(19) "2012-09-20 09:24:50"
["Location"]=>
string(7) "Bandung"
["postal_code"]=>
NULL
["address"]=>
NULL
}
}
["_total_rows":protected]=>
int(3)
["_current_row":protected]=>
int(0)
["_as_object":protected]=>
bool(false)
my goal : example in array index at 8 like this or something else better than this code
array(8) {
["id_job"]=>
string(2) "27"
["job_title"]=>
string(10) "YUYUYUYUYU"
["company_id"]=>
string(1) "3"
Array ( [companies] => array(1) {
["company_name"]=>
string(16) "voyage indonesia"
})
["created_at"]=>
string(19) "2012-10-03 09:35:37"
["Location"]=>
string(4) "Bali"
["postal_code"]=>
string(6) "989767"
["address"]=>
string(11) "dimana mana"
}
company_id is relation table database table bla1,
i want to giving array in relation table, so I can easily to parse data,
like array('bla1' = > array('company_id',1));
any suggestion ?

I think this is what you want.
array[$row_id] = $row;

I don't see that you can get nested arrays from an SQL query.
A database query delivers rows and columns without further substructure.
You can of course do this programmatically, by just taking your tables with jobs and locations as arrays and then generate the desired nested list from this. However, then you're losing the power of the join, and need to embed it into your code.

Related

Get two date from a column where the value is an array in SQL

I'm try to get two dates date are on a column table saved in a complex array. I need this 2 date to compare with "today" and return the id only if it's between this two date.
This is the array with var_dump:
array(1) {
["set_57c440778b89f"]=> array(8) {
["conditions_type"]=> string(3) "all"
["conditions"]=> array(1) {
[1]=> array(2) {
["type"]=> string(8) "apply_to"
["args"]=> array(1) {
["applies_to"]=> string(8) "everyone"
}
}
}
["collector"]=> array(1) {
["type"]=> string(7) "product"
}
["mode"]=> string(5) "block"
["date_from"]=> string(10) "2016-08-28"
["date_to"]=> string(10) "2016-08-31"
["rules"]=> array(1) {
[1]=> array(4) {
["from"]=> string(0) ""
["to"]=> string(0) ""
["type"]=> string(14) "price_discount"
["amount"]=> string(0) ""
}
}
["blockrules"]=> array(1) {
[1]=> array(5) {
["from"]=> string(1) "2"
["adjust"]=> string(1) "1"
["type"]=> string(18) "percent_adjustment"
["amount"]=> string(3) "100"
["repeating"]=> string(3) "yes"
}
}
}
}
And this how i see it inside my column:
a:1:{s:17:"set_57de848bdde1b";a:8 {s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:1:{s:10:"applies_to";s:8:"everyone";}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:5:"block";s:9:"date_from";s:10:"2016-09-01";s:7:"date_to";s:10:"2016-09-30";s:5:"rules";a:1:{i:1;a:4:{s:4:"from";s:1:"1";s:2:"to";s:1:"1";s:4:"type";s:19:"percentage_discount";s:6:"amount";s:3:"100";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:1:"1";s:6:"adjust";s:1:"1";s:4:"type";s:18:"percent_adjustment";s:6:"amount";s:3:"100";s:9:"repeating";s:3:"yes";}}}}
At the moment with mine query i retrive only id that have this array property without filter the range:
SELECT wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'product'
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key IN ('_pricing_rules')
Now i need to add something like(pseudocode):
AND wp_postmeta.meta_value.from >= TODAY
AND wp_postmeta.meta_value.from <= TODAY

how to return data with oder_by count in codeigniter active records

I have 3 tables which contain restaurant_id in each table, I'm trying to get data on uhd_restaurant join uhd_user_order and uhd_order_history
this is my code :
$this->db->DISTINCT();
$this->db->select("t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total");
$this->db->from("uhd_restaurant as t1");
$this->db->join("uhd_user_order as t2","t1.restaurant_id = t2.restaurant_id","left");
$this->db->join("uhd_order_history as t3","t1.restaurant_id = t3.restaurant_id");
$this->db->group_by("t1.restaurant_name");
$this->db->order_by("total","desc");
$res = $this->db->get()->result_array();
return $res;
that code will be return to be :
array(6) {
[0]=>
array(4) {
["restaurant_id"]=>
string(3) "365"
["restaurant_name"]=>
string(8) "Yukihira"
["restaurant_code"]=>
string(4) "MG99"
["total"]=>
string(2) "88"
}
[1]=>
array(4) {
["restaurant_id"]=>
string(3) "367"
["restaurant_name"]=>
string(5) "alice"
["restaurant_code"]=>
string(4) "ZF42"
["total"]=>
string(1) "3"
}
[2]=>
array(4) {
["restaurant_id"]=>
string(3) "363"
["restaurant_name"]=>
string(6) "takumi"
["restaurant_code"]=>
string(4) "ZO09"
["total"]=>
string(1) "2"
}
[3]=>
array(4) {
["restaurant_id"]=>
string(3) "368"
["restaurant_name"]=>
string(5) "test1"
["restaurant_code"]=>
string(4) "WS94"
["total"]=>
string(1) "2"
}
[4]=>
array(4) {
["restaurant_id"]=>
string(3) "366"
["restaurant_name"]=>
string(6) "nakiri"
["restaurant_code"]=>
string(4) "XN27"
["total"]=>
string(1) "1"
}
}
In my table uhd_user_order have 3 restaurant_id, which
364 have 1 , 365 have 8, and 366 have 1
and in my uhd_order_history have 4 restaurant_id which
365 have 11, 363 have 2, 368 have 2 and 367 have 3
so the total should be
365 = 19, 363 = 2, 364 = 1, 366 = 2 367=3 and 368 = 2
but i return the wrong data, see my 365 its return total with 88 but it should be 19 my 366 only return 1 and i cant return restaurant_id 364
so the data should be return to this :
array(5) {
[0]=>
array(4) {
["restaurant_id"]=>
string(3) "365"
["restaurant_name"]=>
string(8) "Yukihira"
["restaurant_code"]=>
string(4) "MG99"
["total"]=>
string(2) "19"
}
[1]=>
array(4) {
["restaurant_id"]=>
string(3) "367"
["restaurant_name"]=>
string(5) "alice"
["restaurant_code"]=>
string(4) "ZF42"
["total"]=>
string(1) "3"
}
[2]=>
array(4) {
["restaurant_id"]=>
string(3) "363"
["restaurant_name"]=>
string(6) "takumi"
["restaurant_code"]=>
string(4) "ZO09"
["total"]=>
string(1) "2"
}
[3]=>
array(4) {
["restaurant_id"]=>
string(3) "368"
["restaurant_name"]=>
string(5) "test1"
["restaurant_code"]=>
string(4) "WS94"
["total"]=>
string(1) "2"
}
[4]=>
array(4) {
["restaurant_id"]=>
string(3) "366"
["restaurant_name"]=>
string(6) "nakiri"
["restaurant_code"]=>
string(4) "XN27"
["total"]=>
string(1) "2"
}
[5]=>
array(4) {
["restaurant_id"]=>
string(3) "364"
["restaurant_name"]=>
string(6) "test"
["restaurant_code"]=>
string(4) "WY58"
["total"]=>
string(1) "1"
}
}
P.S in uhd_restaurant have all restaurant_id in uhd_user_order and uhd_order_history
guy can you help me how to get the right total of data ?
$this->db->select("t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total");
$this->db->from("uhd_restaurant as t1");
$this->db->join("uhd_user_order as t2","t1.restaurant_id = t2.restaurant_id","left");
$this->db->join("uhd_order_history as t3","t1.restaurant_id = t3.restaurant_id");
$this->db->group_by("t1.restaurant_name");
$this->db->order_by("total","desc");
$res = $this->db->get()->result_array();
return $res;
Please try something like this query in CodeIgniter.. [Though there is some error in your ideal result set but it will give you hint...]
SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, SUM(total) FROM ( SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total FROM uhd_restaurant as t1 LEFT JOIN uhd_order_history as t3 ON t1.restaurant_id = t3.restaurant_id GROUP BY t1.restaurant_id UNION ALL SELECT t1.restaurant_id,t1.restaurant_name,t1.restaurant_code, COUNT(t1.restaurant_id)as total FROM uhd_restaurant as t1 LEFT JOIN uhd_user_order as t2 ON t1.restaurant_id = t2.restaurant_id GROUP BY t1.restaurant_id) as t1 GROUP BY t1.restaurant_id ORDER BY total DESC

Get total from mysql fields

I have prices set up in a simple mysql table and I would like to output the total price of the "services" they selected. I was able to achieve this using Laravel 4 however only way I knew how to do it was using a foreach loop. But that returns all the records that have a service and then adds them as each row is returned. How can I get just the last row returned in the foreachloop? Or is their a better method?
Foreach
#foreach ($serviceSummary as $services)
<tr><td>Total:</td><td>{{{ $zero += $services->price }}}</td></tr>
#endforeach
Var Dump from table
var_dump($serviceSummary); die;
array(2) {
[0]=>
object(stdClass)#211 (9) {
["id"]=>
string(1) "1"
["userID"]=>
string(1) "1"
["services"]=>
string(1) "1"
["price"]=>
string(4) "8.95"
["created_at"]=>
string(19) "2013-10-08 19:55:47"
["updated_at"]=>
string(19) "2013-10-08 19:55:47"
["service"]=>
string(20) "Service"
["count"]=>
string(1) "1"
["label"]=>
string(2) "RC"
}
[1]=>
object(stdClass)#212 (9) {
["id"]=>
string(1) "1"
["userID"]=>
string(1) "1"
["services"]=>
string(1) "1"
["price"]=>
string(4) "8.95"
["created_at"]=>
string(19) "2013-10-08 20:38:56"
["updated_at"]=>
string(19) "2013-10-08 20:38:56"
["service"]=>
string(20) "Service"
["count"]=>
string(1) "1"
["label"]=>
string(2) "RC"
}
}
basically what it looks like when printed is:
Total: 8.95
Total: 17.9
I obviously would like it to be just the total 17.9
Wouldn't Laravel's query builder function sum() do the trick?
In your controller:
$totalCost = DB::table('tableName')
->sum('price');
return View::make('your.view', array('totalCost' => $totalCost));
And then in your blade template:
Total: {{{ $totalCost }}}

Contents of array incorrect

I currently have this SQL statement:
SELECT p.*,k.name FROM posts AS p
LEFT JOIN `post-keywords` AS pk ON p.id = pk.id_post
LEFT JOIN keywords AS k ON pk.id_keyword = p.id
ORDER BY p.id DESC LIMIT :num,:count
however, it doesn't work as expected. This is what the server returns:
array(7) {
[0]=>
array(5) {
["id"]=>
string(1) "2"
["title"]=>
string(28) "Everything about Internet"
["text"]=>
string(41) "Article itself"
["date"]=>
string(1) "0"
["name"]=>
NULL
}
[1]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag1"
}
[2]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag2"
}
[3]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag3"
}
[4]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag4"
}
[5]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
NULL
}
[6]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
NULL
}
}
How to retrieve an array from the server that contains multiple name fields:
array (5) {
["id"] =>
   string (1) "1"
   ["title"] =>
   string (16) "Internet"
   ["text"] =>
   string (32) "text text text"
   ["date"] =>
   string (10) "1371186330"
   ["name"] =>
   string (10) "word1"
   ["name"] =>
   string (10) "word2"
   ["name"] =>
   string (10) "word3"
}
Currently your query is 'overwriting' fields that have multiple entries with the same name. For example, you are getting p.name and k.name. You query doesn't make a difference, so it gets overwritten to (I presume) k.name.
To make sure you get them both, write something like: SELECT p.name AS p_name, k.name AS k_name

Getting Unique Values in Multidimensional Array PHP

I have an array similar to this:
array(5) { [0]=> string(2) "32" [1]=> string(2) "67" [2]=> string(19) "2013-07-15 15:56:28" [3]=> string(1) "1" [4]=> string(4) "Fail"}
array(5) { [0]=> string(2) "32" [1]=> string(2) "89" [2]=> string(19) "2013-09-15 13:50:34" [3]=> string(1) "2" [4]=> string(4) "Pass"}
array(5) { [0]=> string(2) "37" [1]=> string(2) "55" [2]=> string(19) "2013-07-15 16:36:12" [3]=> string(1) "1" [4]=> string(4) "Fail"}
array(5) { [0]=> string(2) "39" [1]=> string(2) "92" [2]=> string(19) "2013-08-15 15:46:20" [3]=> string(1) "1" [4]=> string(4) "Pass"}
The first value displays a content ID, the second a score, the third a date, the fourth an attempt number (no limit), and the fifth a result. I want to print these values in the browser, but if there is more than one attempt, I only want to print the one with the highest score. I can't seem to get it working, any help would be greatly appreciated.
The way you can achieve this is:
1st: Group the results by content ID.
2nd: Descending Order the sub-array by score using usort
3rd: Display the 1st sub-array to user for each content ID.

Categories