I want to get data from an array according to the code of the item above. but the array that I made actually produces a double result. the data array that I can only fit is the item
$query = "SELECT IDBRG,Disc FROM $RTL.tmasterbarang1 WHERE IDBRG IN (1167646,1170635,1170634)";
// echo $query;
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$a = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);
foreach($arr1 as $x=>$x_value)
{
echo "<br>";
$queryz = "SELECT CAT_DISC_NBR FROM $RTL.cat_disc_test WHERE FLR_NBR = 1 AND CAT_DISC_CD = '".$x_value."' ";
// echo $queryz;
$resultz = mysqli_query($connect, $queryz);
$rowz = mysqli_fetch_array($resultz);
$elements[] = $rowz['CAT_DISC_NBR'];
}
echo $all = implode(',', $elements);
}
result of print_r($arr1)."<br>";
1. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
2. Array ( [0] => Y [1] => 4 [2] => S [3] => V )
3. Array ( [0] => Y [1] => G [2] => 3 )
// and the each of array have value Y=69, 4=39, S=66, V=66, G=51, 3=38
echo $all = implode(',', $elements);
69,39,63,66
69,39,63,66,69,39,63,66
69,39,63,66,69,39,63,66,69,51,38
// i want it should be
69,39,63,66
69,39,63,66
69,51,38
When you add the data at
$elements[] = $rowz['CAT_DISC_NBR'];
this just keeps on adding data and the $elements array is never cleared out, so the next loop will just keep on adding data to the results of the last loop.
You need something like...
$a = $row['IDBRG'];
$b = $row['Disc'];
$arr1 = str_split($b);
$elements = []; // Reset list
This happens because you are adding data to elements every time and you never set it to empty again. You should empty the elements array before each foreach loop:
$elements=[];
foreach($arr1 as $x=>$x_value)
{
echo "<br>";
$queryz = "SELECT CAT_DISC_NBR FROM $RTL.cat_disc_test WHERE FLR_NBR = 1 AND CAT_DISC_CD = '".$x_value."' ";
// echo $queryz;
$resultz = mysqli_query($connect, $queryz);
$rowz = mysqli_fetch_array($resultz);
$elements[] = $rowz['CAT_DISC_NBR'];
}
Related
I cannot seem to get the syntax correct to loop through my results and insert them into an associative array- at the moment it is only giving me the first result and not looping through the rest. I've tried numerous combinations of foreach etc but just cannot complete this successfully.
Here is my code:
$count = array(
"timeinsert"=>"value",
"rfid"=>"value"
);
$readlags = "SELECT id_arduino, Device_id,time_inserted, message, message_type,
LAG(message) OVER(PARTITION BY device_id ORDER BY time_inserted)
AS 'Previous Entry'
FROM Arduino_Data
WHERE Datediff(Curdate(), time_inserted) < $period AND Device_id = $device AND message != 2
GROUP BY time_inserted ASC";
$result = $conn->query($readlags);
if (!$result) {
echo $conn->error;
}
while ($row = mysqli_fetch_array($result)) {
$eventtime = $row['time_inserted'];
$message = $row['message'];
$message_type = $row['message_type'];
$previous = $row['Previous Entry'];
if ($previous != 'Broken' and $previous != null) {
$catid = "SELECT RFID_id
FROM Cat_User
WHERE RFID_id = $previous ";
$result1 = $conn->query($catid);
if (!$result1) {
echo $conn->error;
}
while ($row1 = mysqli_fetch_array($result1)) {
$cat_id = $row1['RFID_id'];
//echo $cat_id . '<br>';
}
}
if ($message == "Broken" && $message_type == "BreakBeam" && $previous==$cat_id) {
$count["timeinsert"] = $eventtime;
$count["rfid"]=$previous;
}
}
print_r($count);
This is the output :
Array
(
[timeinsert] => 2020-09-17 16:02:44
[rfid] => 5609
)
When I do an array_push the results are the following :
Array
(
[0] => 2020-09-17 15:51:37
[1] => 23641
[2] => 2020-09-17 15:52:20
[3] => 5609
[4] => 2020-09-17 15:53:23
[5] => 5609
[6] => 2020-09-17 16:02:44
[7] => 5609
)
this because you overwritten all result in same variable so when you print reasult you will see last data written in variable.
1) define $count as array and $c to count array element like this
$count = array();
$c=0;
2) when you want to save new data but it in multidimensional array like this:
$count[$c]["timeinsert"] = $eventtime;
$count[$c]["rfid"]=$previous;
$c++;
3) retrieve your data like this:
$c=count($count);
for($i=0;$i<$c;$i++){
echo $count[$i]["timeinsert"];
echo $count[$i]["rfid"];
}
I really need your help! How can I do to group the array results and assemble select.
Database
Image Database
PHP
<?php
$rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");
foreach ($rsPA as $key => $rsRowPA){
$dis1[] = explode("," , $rsRowPA['disciplinas']);
}
echo '<pre>';print_r($dis1);echo '</pre>';
?>
Result:
Array
(
[0] => Array
(
[0] => EJA-1
)
[1] => Array
(
[0] => EJA-1
[1] => EJA-5
[2] => TTI-1
)
)
Expilando: From the result of the array, I will only get the number after the -, Ex: EJA-1, I only need 1, which is the ID of the discipline table.
End result you would like
Image select
I thank everyone who can help me.
You could perform an additional for loop, and explode each element on - and take the last part:
<?php
$rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");
foreach ($rsPA as $key => $rsRowPA){
$temp = explode("," , $rsRowPA['disciplinas']);
foreach($temp as $elem){
$number = explode('-',$elem);
$number = end($number);
$numbers[] = $number;
}
}
$dis1 = array_count_values($numbers);
echo '<pre>';print_r($dis1);echo '</pre>';
?>
I'm trying to create foreach statement using multidimensional array.
Controller:
function index()
{
$index1 = 0;
$index2 = 0;
$index3 = 0;
$index4 = 0;
$result1 = $this->data->get_test('kdprogram','kdprogram');
foreach($result1 as $row1){
$array_temp[$index1] = $row1;
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $row2){
$array_temp[$index1][$index2] = $row2;
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $row3){
$array_temp[$index1][$index2][$index3] = $row3;
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4){
$array_temp[$index1][$index2][$index3][$index4] = $row4;
$index4++;
}
$index3 ++;
}
$index2 ++;
}
$index1 ++;
}
//print_r($array_temp);
$data['damn'] = $array_temp;
$this->load->view('report/laporan_output', $data);
}
$data contains:
Array
(
[0] => Array
(
[kdprogram] => 06
[0] => Array
(
[kdgiat] => 3400
[0] => Array
(
[kdoutput] => 001
[0] => Array
(
[kdsoutput] => 001
)
[1] => Array
(
[kdsoutput] => 006
)
)
[1] => Array
(
[kdoutput] => 008
[2] => Array
(
[kdsoutput] => 001
)
)
)
)
)
How to echo each array (kdprogram, kdgiat, etc) on view especially with html table?
Am i doing it right?
Thanks
it looks kinda ugly and i would use some sort of recursive function but here is your way
in controller ( i assume the arrays have numeric index otherwise you have to use some sort of counter like you did in your code )
foreach($result1 as $a_counter=>$row1)
{
$array_temp[$a_counter] = array( 'parent'=>$row1 , 'child'=>array());
$result2 = $this->data->get_test('kdgiat','kdgiat','kdprogram = '.$row1['kdprogram']);
foreach($result2 as $b_counter=> $row2)
{
$array_temp[$a_counter]['child'][$b_counter] = array( 'parent'=>$row2 , 'child'=>array());
$result3 = $this->data->get_test('kdoutput','kdoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat']);
foreach($result3 as $c_counter=>$row3)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter] = array( 'parent'=>$row3 , 'child'=>array());
$result4 = $this->data->get_test('kdsoutput','kdsoutput','kdprogram = '.$row1['kdprogram'].' and kdgiat = '.$row2['kdgiat'] .' and kdoutput = '.$row3['kdoutput']);
foreach($result4 as $row4)
{
$array_temp[$a_counter]['child'][$b_counter]['child'][$c_counter]['child'][] = $row;
}
}
}
}
in the view
foreach($result as $a )
{
// show a
foreach($a['child'] as $b )
{
// show b
foreach($b['child'] as $c )
{
// show c
foreach($c['child'] as $d )
{
// show d
}
}
}
}
I have following array:
Array
(
[0] => ALFA01
[1] => BETA02
[2] => GAMMA03
[3] => DELTA04
[4] => EPSILON05
[5] => ZETA06
[6] => THETA07
[7] => IOTA08
[8] => KAPPA09
[9] => LAMBDA10
)
Then I separate the values into characters and numbers in the following way:
foreach($portfolio as $value) {
$char = substr("$value", 0, 2);
$numb = substr("$value", 2);
and do a query using this values:
$query = "
SELECT id, typ1, typ2, typ3
FROM data_table
WHERE char = '$char'
AND numb = $numb
LIMIT 1";
$result = mysqli_query($mysqli, $query);
//Now we have some important variables that will help finding our result.
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$typ1 = $row['typ1'];
$typ2 = $row['typ2'];
$typ3 = $row['typ3'];
}
Then I do some queries in case typ* are = 1 (it can be only 0 or 1).
if ($typ1 == 1) {
$query_id_num2 = "
SELECT id_num2
FROM values_table01
WHERE id_num1 = $id";
$result_id_num2 = mysqli_query($mysqli, $query_id_num2);
// Return an array with all ids.
$id_num2_typ01 = array();
while($row = mysqli_fetch_array($result_appln_id_num2)) {
$id_num2_typ01[] = ($row['id_num2']);
}
}
/== EDIT ==/
In each different typ we have a different values_table.
I'm now writing the complete query for typ2, so it will be emphasised.
// == /EDIT ==/
Then I do the same to typ2 and typ3...
if ($typ2 == 1) {
$query_id_num2 = "
SELECT id_num2
FROM values_table02
WHERE id_num1 = $id";
$result_id_num2 = mysqli_query($mysqli, $query_id_num2);
// Return an array with all ids.
$id_num2_typ02 = array();
while($row = mysqli_fetch_array($result_appln_id_num2)) {
$id_num2_typ02[] = ($row['id_num2']);
}
}
etc.
In the end I merge the three arrays (if they are set) and print the result on the screen:
// typ01
if (isset($id_num2_typ01)){
$id_num2_typ01 = $id_num2_typ01;
} else {
$id_num2_typ01 = array();
}
// typ02
if (isset($id_num2_typ02)){
$id_num2_typ02 = $id_num2_typ02;
} else {
$id_num2_typ02 = array();
}
// typ03
if (isset($id_num2_typ03)){
$id_num2_typ03 = $id_num2_typ03;
} else {
$id_num2_typ03 = array();
}
// merge arrays
$id_num2 = array_merge($id_num2_typ01,$id_num2_typ02,$id_num2_typ03);
// print it on screen
echo '<pre>id_num2:<br />';
print_r($appln_idNum2);
echo '</pre>';
} // this closes the above foreach, i.e. the code will loop to every value of the first array
As result I receive for example following data:
id_num2:
Array
(
[0] => 41558194
[1] => 44677841
[2] => 44503689
[3] => 40651770
[4] => 41667291
)
id_num2:
Array
(
[0] => 15458354
[1] => 35477154
[2] => 15703123
[3] => 95151111
[4] => 55567125
)
etc.
In our case we will have 10 small arrays like that.
My problem is: How can I merge this small arrays so in the ende I have only one array?
Thank you for your help to solve this particular problem. Maybe is there also another possibility to write the code to be better and more efficient.
I have following 2 arrays
$Name = Array
(
[0] => A
[1] => B
[2] => C
[3] => D
)
$zip = Array
(
[0] => 411023
[1] => 411045
[2] => 411051
[3] => 411023
)
Final array should be like this
$final = Array
(
411045 => B
411051 => C
411023 => A B
)
Hope you guys get it what i mean to say.
Here is another solution :
<?php
$Name = Array('A','B','C','D');
$zip = Array(411023,411045,411051,411023);
$namezip = array_combine($Name,$zip);
$res = array();
foreach($namezip as $nam=>$zp){
if(array_key_exists($zp,$res)){
$res[$zp] .= " ".$nam;
}
else{
$res[$zp] = $nam;
}
}
echo "<pre>";
print_r($res);
?>
$name = array ('A', 'B', 'C', 'D');
$zip = array (411023, 411045, 411051, 411023);
$final = array ();
for ($i = 0; $i < sizeof ($zip); $i++)
{
$n = $name [$i];
$z = $zip [$i];
if (!array_key_exists ($z, $final))
$final [$z] = array ();
$final [$z][] = $n;
}
print_r ($final);
You are looking for the second use case of phps array_keys() function where you can specify a value range. Using that you can simply iterate over the second array:
$final=array();
foreach ($zip as $key=>$anchor) {
if (! array_key_exists($final,$key))
$final[$key]=array();
$final[$key][]=array_keys($name,$anchor);
}
This generates a result $final where each element is an array again, most likely what you want. One could also interpret your question as if you ask for a space separated string, in that case just additionally convert the resulting array:
foreach ($final as $key=>$val)
$final[$key]=implode(' ',$val);