Associative array always returns only 4 result - php

Hi want to create a associative array for below query result :
$sql = "select s.*,di.dealsimage,
ctm.city,
l.location,
GROUP_CONCAT(DISTINCT cm.cuisine ORDER BY scr.cuisine_sequence_for_store) AS cui,
GROUP_CONCAT(DISTINCT rtm.restaurant_type ORDER BY srr.rest_type_sequence_for_store) AS restauranttype
from stores s
left join city_master ctm on s.city_id = ctm.city_id
left join locations l on s.location_id = l.location_id
left join store_cuisine_relation scr on s.store_id = scr.store_id
left join cuisine_master cm on scr.cuisine_id = cm.cuisine_id
left join store_resttype_relation srr on s.store_id = srr.store_id
left join restaurant_type_master rtm on rtm.rest_type_id = srr.rest_type_id
left join store_dealcat_relation sdr on s.store_id = sdr.store_id
left join deals_category_master dcm on dcm.deal_cat_id = sdr.deal_cat_id
left join deals_image di on di.`store_id` = s.store_id
where $condition1 s.is_active = 1 $condition2 group by (s.store_id) order by s.store_id";
//echo $sql;exit;
//echo $sql;exit;
$sqlex1 = mysqli_query($db,$sql);
$custom_count = #mysqli_num_rows($sqlex1); // it prints 28
while($result1 = mysqli_fetch_assoc($sqlex1)){
$dataArr = array_push_assoc($dataArr, 'store_id', $result1['store_id']);
$dataArr = array_push_assoc($dataArr, 'store_name', $result1['store_name']);
$dataArr = array_push_assoc($dataArr, 'store_logo', $result1['store_image_url']);
$dataArr = array_push_assoc($dataArr, 'deals_image', $result1['dealsimage']);
}
//echo count($dataArr);exit;
//echo $kl;exit;
//$result = array_merge_recursive($gpsArr,$dataArr);
function array_push_assoc($array, $key, $value){
$array[$key][] = $value;
return $array;
}
The query returns 28 result but when I try to echo the count of $dataArr it prints 4 always. What im doing wrong ? How can I achieve this ?
Thanks in advance

Becasue your function array_push_assoc() creates array with 4 keys store_id, store_name,store_logo, anddeals_image`, and each key has 28 rows
Try this:
dataArr = array();
while($result1 = mysqli_fetch_assoc($sqlex1))
{
$dataArr[] = array('store_id'=>$result1['store_id'],
'store_name'=>$result1['store_name'],
'store_logo'=>$result1['store_image_url'],
'deals_image'=>$result1['dealsimage']);
}
This will create array with 28 associative arrays

Your code should be changed to:
while($result1 = mysqli_fetch_assoc($sqlex1))
{
array_push($dataArr, array ('store_id'=> $result1['store_id'], 'store_name'=> $result1['store_name'], 'store_logo'=> $result1['store_image_url'], 'deals_image'=> $result1['dealsimage']));
}

Related

PHP SQL i another SQL query

Description:
table1 - columns: f1,f2,f3,f4,f5
in these columns I have rom_number for temperature sensors
table2 - columns: act_temperature, rom_number
I need act_temperature from table2 for each rom_number stored in table1 (cols f1,f2,...)
I have code:
// main loop
$rows = $db->query("SELECT * FROM tabela1 WHERE active='on' ");
$row = $rows->fetchAll();
foreach ($row as $a) {
$f1 = $a['f1'];
$f2 = $a['f2'];
$f3 = $a['f3'];
$f4 = $a['f4'];
$f5 = $a['f5'];
}
How to change the code ?
You have to LEFT JOIN on table 2 for each columns f1,f2,...
$sql = "SELECT t1.*,
t2_1.act_temperature as tmp1,
t2_2.act_temperature as tmp2,
t2_3.act_temperature as tmp3,
t2_4.act_temperature as tmp4,
t2_5.act_temperature as tmp4
FROM tabela1 t1
LEFT JOIN tabela2 t2_1 ON t2_1.rom_number = t1.f1
LEFT JOIN tabela2 t2_2 ON t2_2.rom_number = t1.f2
LEFT JOIN tabela2 t2_3 ON t2_3.rom_number = t1.f3
LEFT JOIN tabela2 t2_4 ON t2_4.rom_number = t1.f4
LEFT JOIN tabela2 t2_5 ON t2_5.rom_number = t1.f5
WHERE active='on' ";
Then, $a should have following keys : f1,f2,...,f5,tmp1,tmp2,...,tmp5.

Select - Rows for Columns possible?

i have this select:
SELECT
a.`cod_oportunidade`,
b.nome nome_cliente,
c.descricao estado,
d.descricao cidade,
e.nome nome_funcionario_criou,
f.nome nome_funcionario_resp,
i.`descricao`,
h.`valor`
FROM
oportunidades_clientes a
LEFT OUTER JOIN empresas_clientes b
ON b.cod_cliente = a.cod_cliente
LEFT OUTER JOIN sistema_estados c
ON c.cod_estado = b.cod_estado
LEFT OUTER JOIN sistema_cidades d
ON d.cod_cidade = b.cod_cidade
LEFT OUTER JOIN empresas_funcionario e
ON e.cod_funcionario = a.cod_funcionario_criou
LEFT OUTER JOIN empresas_funcionario f
ON f.cod_funcionario = a.cod_funcionario_resp
JOIN formulario_valor h
ON h.`cod_oportunidade` = a.`cod_oportunidade`
JOIN formulario_campo i
ON i.`cod_campo` = h.`cod_campo`
WHERE 1 = 1
AND a.`cod_oportunidade` = 3
The result is therefore:
My question is... Need to inves need to stay several lines that the result is only one line. The data in the column "descricao" must be as columns ...
The implode() function will separate an array (columns from a database) with whatever character you prefer, in your case a pipe:
$mysqli = mysqli_connect("host","user","pass","db");
$result = mysqli_query($mysqli, "SELECT ...");
while ($row = mysqli_fetch_array($result)) {
echo implode(" | ", $row) . '<br />';
}

Setting PHP Query into a nested array

I am trying to use PHP to logically store all of that data in a nested associative arrays, and then whenever I loop through the data or need to reference the data I would refer to the array pointer rather than doing new queries each time.
For example:
My query is
$query = $db->query("
SELECT
c.id campaign_id,
c.campaign_title,
c.start_date campaign_start_date,
c.end_date campaign_end_date,
cat.id category_id,
cat.category_title,
n.id nominee_id,
n.title nominee_title,
u.id voter_id,
u.fullname voter_name
FROM
campaign c
LEFT JOIN categories cat ON cat.campaign_id = c.id
LEFT JOIN category_nominees cn ON cn.category_id = cat.id
LEFT JOIN nominees n ON n.id = cn.nominee_id
LEFT JOIN category_votes cv ON cv.campaign_id = c.id
AND cv.category_id = cat.id
AND cv.nominee_id = n.id
LEFT JOIN users u on u.id = cv.user_id
WHERE
c.active = 1
ORDER BY
u.fullname,
c.campaign_title,
cat.category_order,
cat.category_title,
cn.nominee_order,
n.title
") or die(mysqli_error());
and im trying to set up the nested array like
$array = array() ;
while($row = mysqli_fetch_assoc($query)) {
$array[$row['campaign_id']] = $row['campaign_id'];
$array[$row['campaign_id']]['campaign_title'] = $row['campaign_title'];
$array[$row['campaign_id']]['campaign_start_date'] = $row['campaign_start_date'];
$array[$row['campaign_id']]['campaign_end_date'] = $row['campaign_end_date'];
$array[$row['campaign_id']]['campaign_title'] = $row['campaign_title'];
$array[$row['campaign_id']]['category_id'] = $row['category_id'];
$array[$row['campaign_id']]['category_id']['category_title'] = $row['category_title'];
}
So whenever I point to:
$array[2][3]['category_title']
It would print the category title
Do you have any ideas? I literally been at it for months and can't figure it out.
Use the following:
while ($row = mysqli_fetch_assoc($query)) {
if (!isset($row['campaign_id'])) {
$array[$row['campaign_id']] = $row;
}
if (!isset($array[$row['campaign_id']]['categories'])) {
$array[$row['campaign_id']]['categories'] = array();
}
$array[$row['campaign_id']]['categories'][$row['category_id']] = array('category_id' => $row['category_id'], 'category_title' => $row['category_title']);
}
}
$row is already an associative array, you don't need to assign each element separately. You only need to deal specially with the category information, which has to be put into a nested associative array that I've called categories. So you would access it as
$array[0]['categories'][1]['category_title']
You can loop over all the categories with:
foreach ($array[$campaign]['categories'] as $cat) {
echo "Category ID: {$cat['category_id']} Title: {$cat['category_title']}<br>";
}

query error php codeigniter

this my query
function get_transaksi_by_tgl($tgl_awal,$tgl_akhir,$limit, $offset = 0) {
$rs['query'] = $this->db->query("SELECT a.id_transaksi, a.nama, a.tgl_transaksi,
(SELECT COUNT( id_transaksi ) AS jum FROM tbl_detail_trs_menu WHERE id_transaksi = a.id_transaksi)
AS jumlah, a.status_transaksi, a.total, b.status_pelanggan, c.nama_karyawan FROM tbl_transaksi a
LEFT JOIN tbl_pelanggan b ON a.id_pelanggan = b.id_pelanggan LEFT JOIN tbl_karyawan c
ON a.id_karyawan = c.id_karyawan WHERE a.status_transaksi = 'OK' AND a.tgl_transaksi between ".$tgl_awal."
AND ".$tgl_akhir." LIMIT ".$offset.",".$limit."");
echo "jumlah : ".$rs['query']->num_rows();
return $rs;
}
but this query can't return rows....
whereas i try query in database can return 4 row... why??? thx u

Set variable from alias in SQL query

I have a simple JOIN query:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
My problem is that I have an "id" column in both the tblhosting and tblclients tables, so my variables both have the same id. I tried to set it using an alias in the example above (ho. and cl.) but it doesn't work. How can I do this in the example above?
Thank you!
How about this? (a bit rusty on php details, but this should do it):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
You are selecting the records, with a wild card *, so you can't select the fields like that.
As per your query h.hst_serverid & ho.userid have the exact same value as you want. SO simply do this
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
However, selecting specific rows might sound better too
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}

Categories