I use pivot to select some record(s) from my table. This is the query:
$data['master'] = $this->db->query("select * from
(
select row, c.Nilai,b.Fullname,a.KodePenilaian,d.Description from penilaian_header a
left join employee b on a.Nip = b.Nip
left join outlet d on a.Outlet = d.OutletCode
left join (select ROW_NUMBER() OVER(PARTITION BY KodePenilaianH ORDER BY idPenilaiand DESC) AS Row, Nilai,KodePenilaianH from penilaian_Detail
) c on a.KodePenilaian = c.KodePenilaianH where a.Outlet like '%$outlet%' and Periode like '%$periode%'
) nilai
pivot
(
sum(nilai)
for row in ([1],[2],[3],[4],[5])
) piv;")->result();
And this the result when I run it via MS SQL Management Tools
now, my problem is to retrieve these [1],[2],[3],[4],[5]
values. When I do this:
echo "<pre>";print_r($data['master']);
die();
I only get this:
Array
(
[0] => stdClass Object
(
[Fullname] => maya kristiana
[KodePenilaian] => PE0000001
[Description] => KIOS ATRIUM3
)
[1] => stdClass Object
(
[Fullname] => SHELVIA GITA PUTRI
[KodePenilaian] => PE0000002
[Description] => KIOS ATRIUM3
)
)
Update: I found the solution, thanks to @Alex Tartan (see accepted answer below):
I simply changed my ->result()
to ->result_array();
and the result is now as desired:
You might want to use
->result_array(); // instead of `->result();`
Numerical properties for objects are "weird" to say the least.
Plus, there's the hassle of reading them. You'd have to echo $obj->{1}
Also, setting them implies the same approach: $obj->{1} = 'stuff'
Codeigniter might not do that.
Getting them as arrays will bypass this issue (hopefully)
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
This is will return All the Records as Array Result:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Also If you ever need to get a Single record then you can use this:
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->title;
echo $row->name;
echo $row->body;
}
Also refer this It will help you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.