I have two tables. There is a table place that contains data as shown below:
id name
-----------
1 A1
2 A2
3 A3
4 B1
Other table place_conditions contains data as sown below:
id placeID desription
----------------------------
1 1 Windy
2 1 Dry
3 3 Wet
4 3 Rainy
4 4 Dry
I like to made a query in laravel 4 that make result like this below:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => A1
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Windy
)
[1] => stdClass Object
(
[description] => Dry
)
)
)
[1] => stdClass Object
(
[id] => 2
[name] => A2
[place_cond] => Array
(
)
)
[3] => stdClass Object
(
[id] => 3
[name] => A3
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Wet
)
[1] => stdClass Object
(
[description] => Rainy
)
)
)
)
Here part of my query (it still error) :
$query = DB::table('place');
if(Input::has('search')){
$query->where(function($a){
$a->where('name','LIKE','%'.Input::get('search').'%');
});
}
/* $query->select('place.*',DB::raw('SELECT Description as place_cond FROM place_conditions WHERE place_conditions.placeID = place.id')); // sub query . Arrggghhhhhh... whyyyy.. stillllll.. errorrrrrrr.. */
$data = $query->get();
Is possible to produce results like the above in laravel 4? Can you help me? Thank you
Related
I need to combine those two array into a single array.
1st query result
Array ( [0] => stdClass Object ( [p_type] => Bank [tsales] => 131000 [tdues] => 55000 [tpaid] => 75000 ) [1] => stdClass Object ( [p_type] => Cash [tsales] => 104000 [tdues] => 50000 [tpaid] => 54000 ) [2] => stdClass Object ( [p_type] => bKash [tsales] => 25000 [tdues] => 10000 [tpaid] => 5000 ) [3] => stdClass Object ( [p_type] => Pause [tsales] => 4000 [tdues] => 1000 [tpaid] => 2000 ) )
2nd query result
Array ( [0] => stdClass Object ( [dpayment] => 1000 ) [1] => stdClass Object ( [dpayment] => 5000 ) [2] => stdClass Object ( [dpayment] => 1000 ) [3] => stdClass Object ( [dpayment] => 5000 ) )
try to use array_merge. That should be very easy.
Your query result shows that you using $query->result() to get the value,
Instead of using result(), use $query->row_array();
then you get single dimensional array for both queries
then use array_merge() that would be easy
I have 3 Queries
$this->db->select('count(id) as Lead_Current_Month')->from('admin_lead_distribution');
$result1=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level1_lead_distribution');
$result2=$this->db->get()->result();
$this->db->select('count(id) as Lead_Current_Month')->from('level2_lead_distribution');
$result3=$this->db->get()->result();
When I print it gives
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 12
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 38
)
)
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 10
)
)
How to combine 3 arrays into one?
I want the result to be:
Array
(
[0] => stdClass Object
(
[Lead_Current_Month] => 60
)
)
[Lead_Current_Month] => 60(60 is sum all the 3 array values)
You can just sum up the values of the results, and I don't know why you want to store the information in any array/object, just store it in a variable.
$sum_result = $result1[0]->Lead_Current_Month + $result2[0]->Lead_Current_Month + $result3[0]->Lead_Current_Month;
echo $sum_result; //will echo 60
I'm trying to return some data from a query in this format (or at least very similar)
Array
(
[0] => Array
(
[id] => 1
[name] => Gym Opening Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
[1] => Array
(
[id] => 1
[name] => Resteruant Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
)
I have this query so far, however, it throws an error Error executing PDO query: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SELECT
otg.groupName,
otg.id,
(SELECT
ot.id,
ot.label,
ot.time
FROM
opening_times ot
WHERE
ot.groupId = otg.id) as times
FROM
opening_time_groups as otg
WHERE
otg.venueId = $venueId
AND otg.active = 1
What have I gotten wrong? Or is this something I simply can't do in 1 query?
You sub query is returning more than one row. A sub query in the SELECT must only return a single row.
However it would seem that you could do this simple with a normal join, then sort the array out in the script that calls it:-
SELECT
otg.groupName,
otg.id,
ot.id,
ot.label,
ot.time
FROM opening_time_groups as otg
LEFT OUTER JOIN opening_times ot
ON ot.groupId = otg.id) as times
WHERE otg.venueId = $venueId
AND otg.active = 1
Getting this result after array_merge of 2 arrays:
Array ( [0] => stdClass Object ( [tid] => 3954 [name] => Alleman )
[1] => stdClass Object ( [tid] => 3958 [name] => Jack )
[2] => stdClass Object ( [tid] => 3963 [name] => Aquin Catholic ));
I want to sort the array by [name]. I have been reading and searching for hours but cannot figure out how to sort passed the stdClass Object to [name].
Have tried sort, ksort, uasort, etc. wont sort passed the stdClass Object to [name].
Example
function compareItems($a, $b)
{
if ( $a->name < $b->name ) return -1;
if ( $a->name > $b->name ) return 1;
return 0; // equality
}
uasort($players, "compareItems");
I have very long multidimensional array that have many sub arrays. I would like to insert some value from them into mysql. I would be gratefull for some tip how to deal with it. I would like to use a loop that put only some value to one or different table in database. How I can get value from for example [Things][Thing][k][value]? Thanks for any advice.
Array
(
[Data] => Array
(
[A] => Array
(
[B] => Array
(
[0] => Array
(
[C] => Array
(
[value] => some value1
)
[D] => Array
(
[value] => some value2
)
[E] => some value3
)
[1] => Array
(
[C] => Array
(
[value] => some value4
)
[D] => Array
(
[value] => some value5
)
[E] => 5
)
)
[value] =>
)
[Things] => Array
(
[Thing] => Array
(
[F] => Array
(
[value] => some value6
)
[G] => Array
(
[H] => Array
(
[0] => Array
(
[i] => Array
(
[value] => some value7
)
[j] => Array
(
[value] => some value8
[value] => some value8
)
[k] => Array
(
[value] => some value9
)
[l] => Array
(
[value] => some value10
)
[m] => some value11
[n] =>
)
[1] => Array
(
[o] => Array
(
[value] => some value12
)
[p] => Array
(
[value] => some value13
)
[r] => Array
(
[value] => some value14
)
[d] => Array
(
[value] => some value15
)
[t] => some value16
[u] =>
)
)
[value] =>
)
)
)
[Thing2] => Array
(
(...)
and so on...
In my database I have table for these things from array above. There is id in it of course and some other fields that are connected with other tables.
As you can see below I would like to get some values from array, insert it into Table Thing, and some of them put into
Table for field 4 and Table for field 5 and get id of them and put it into Table Thing. I'll use stored procedure.
I would like to call it from php (I hope that this conception is good?). I have problem to get values that I want to get from this array so David Chan I'm looking for help with array and loop. I tried as Starx has written but I got nothing (white screen). I'm still learning so I know that I have to do something wrong.
............ ................. ..................
Table Thing Table for field 4 Table for field 5
id int id int id int
field 1 varchar value varchar value varchar
field 2 varchar
field 3 varchar ................ ................
field 4 int
field 5 int
(...)
...........
Whenever you are trying to access multidimensional array, you have to use indices to point a particular field.
For example:
$data['A']['B'][0]['C']['VALUE'] would give you the value some value1
Use a similar way to get the values and insert into the database.