Populate the array in php using mysql dynamically - php

I have the database and i want to create a matrix or probably 2d array in PHP where the first column comes from first query and then i loop again another query. I am getting it but when it comes to the matrix i want first column populate from the first column and the rest of the column in that row should populate depending on the data for that particular query. you might better understand after looking at the code
<!DOCTYPE html>
<html>
<?php
require ('connect.php');
$pro_name = $_POST['project'];
$sheet_type = $_POST['sheet_set'];
echo $pro_name ."-";
echo $sheet_type . "<br/>";
?>
<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));
while($doc_num = mysqli_fetch_row($result_doc_num)){
if($doc_num[0] != ''){
$qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
$result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
// array_push($doc_num_data, $doc_num);
// $doc_num_data = array();
while($ins_doc = mysqli_fetch_row($result_qry_ins)){
$instruments_tag = $ins_doc[0];
array_push($doc_num_data, $doc_num);
}
}
}
echo "<pre>";
print_r($doc_num_data);
echo "</pre>";
?>
</html>
I want something like this
[doc1] [ins1] [ins2] []
[doc2] [ins1] [] []
[doc3] [ins1] [ins2] [ins3]
[doc4] [ins1] [ins2] []
thats the result what i get basically each element is saving in different array
Array (
[0] => Array
(
[0] => test1
)
[1] => Array
(
[0] => PIT-100
)
[2] => Array
(
[0] => test1
)
[3] => Array
(
[0] => PIT-330
)
[4] => Array
(
[0] => PIT-330
)
[5] => Array
(
[0] => PIT-330
)
[6] => Array
(
[0] => test2
)
[7] => Array
(
[0] => PIT-300
)
[8] => Array
(
[0] => PIT-300
)
[9] => Array
(
[0] => PIT-300
)
[10] => Array
(
[0] => PIT-300
)
[11] => Array
(
[0] => PIT-300
)

The problem you're having is with array_push(). All that is going to do (as you can see) is stack the values into a LIST, not a hash / multi-dimensional array.
What you want is a list of things that are referenced by that doc ID, right?
Try this:
if($instruments_tag != null) array_push($doc_num_data[$doc_num], $instruments_tag);
I think that is what you're looking for, or close to it at any rate, and it won't add that empty value at the end. If you want that, remove the if.

Array
(
[0] => Array
(
[0] => test1
[1] => PIT-100
[2] => PIT-330
)
[1] => Array
(
[0] => test2
[1] => PIT-300
)
[2] => Array
(
[0] => test3
[1] => TIT-100
)
[3] => Array
(
[0] => test5
[1] => TIT-200
[2] => TIT-900
[3] => PIT-000
)
[4] => Array
(
[0] => test4
[1] => TIT-1000
)
[5] => Array
(
[0] => test6
[1] => TIT-999
)
)
using this code
<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));
$i=0;
while($doc_num = mysqli_fetch_row($result_doc_num)){
$j=1;
if($doc_num[0] != ''){
$qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
$result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
//echo $doc_num[0];
$doc_num_data[$i][0] = $doc_num[0];
//echo $doc_num_data[$i];
// $doc_num_data = array();
while($ins_doc = mysqli_fetch_row($result_qry_ins)){
$instruments_tag = $ins_doc[0];
$doc_num_data[$i][$j]= $instruments_tag;
$j=$j+1;
}
}
$i= $i+1;
}

Related

Using odbc_fetch_array but got "arrayarrayarray "instead

I try to get value by using odbc_fetch_array and turn them into variable but when i try to echo it's just said "arrayarrayarray"
Array (
[0] => Array ( [PKDSEQ] => 154604 )
[1] => Array ( [PKDSEQ] => 154604 )
[2] => Array ( [PKDSEQ] => 154529 )
[3] => Array ( [PKDSEQ] => 161689 )
[4] => Array ( [PKDSEQ] => 158940 )
[5] => Array ( [PKDSEQ] => 155383 )
[6] => Array ( [PKDSEQ] => 156247 )
[7] => Array ( [PKDSEQ] => 158123 )
)
and is there a way to get array separate into number?
Code
$PKDSEQ2 = array();
$table4 = "SELECT [PKDSEQ] FROM [PWSWMS].[dbo].[tbTR_PACKD] WHERE [PKDSEQ] = '$PKDSEQRS5'";
$RS4 = odbc_exec($connection2, $table4);
while ($PKDSEQ2 = odbc_fetch_array($RS4)) {
$PKDSEQ[] = $PKDSEQ2;
}
}
print_r(array_values($PKDSEQ));
if(isset($_POST['QTYINPUT1'])) {
$QTYINPUT1 = $_POST['QTYINPUT1'];
$update = "UPDATE [PWSWMS].[dbo].[tbTR_PACKD] SET QTYPCK='$QTYINPUT1' WHERE [PKDSEQ]='$PKDSEQ[1]'";
$result = odbc_exec($connection2, $update);
echo "<br>$QTYINPUT1";
echo "<br>$PKDSEQ[1]";
}
You've got an associative array within each entry in $PKDSEQ. So you need to refer to the property of that array which you want to output. Since the array happens to only have one property, it's pretty simple to choose:
echo "<br>".$PKDSEQ[1]["PKDSEQ"];

Dynamically put condition with the array as per the array value

I have the below array. I want to put condition dynamically as per the sections are coming.
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
In the above array for content_form there are 3 array values and in 0th index genre, 1st index cast and 2nd index category present.
Similarly for content_type there are 3 array values present and in the last array conditions present.
My requirement is that as per the section coming I want to put condition with them.
Example- ((genre OR cast) AND category)
similarly if my output is like below then the condition will be
Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
And if my output is like the below then there will no condition with any other key and it will return the simple result.
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
How I can do this dynamically as per the sections are coming as per sequence with the array of content_type and conditions?
We have done this. Please check:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
Let me know this is your requirement or not?

Remove identical values except first and last

I have a php script creating a multidimensional array:
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
while($row = mysql_fetch_assoc($res)) {
$date = $row['date'];
$temperature = $row['temperature'];
$temp_array = array();
array_push($temp_array, $date);
array_push($temp_array, $temperature);
array_push($outside_temperature_array, $temp_array);
unset($temp_array);
}
print_r($outside_temperature_array);
The multidimensional array looks like this. It has a unix timestamp in sequential order along with a value.
Array
(
[0] => Array
(
[0] => 1452483001
[1] => 40
)
[1] => Array
(
[0] => 1452483301
[1] => 39
)
[2] => Array
(
[0] => 1452483600
[1] => 39
)
[3] => Array
(
[0] => 1452483901
[1] => 39
)
[4] => Array
(
[0] => 1452484201
[1] => 39
)
[5] => Array
(
[0] => 1452484502
[1] => 39
)
[6] => Array
(
[0] => 1452484801
[1] => 38
)
[7] => Array
(
[0] => 1452485101
[1] => 38
)
[8] => Array
(
[0] => 1452485400
[1] => 38
)
[9] => Array
(
[0] => 1452485701
[1] => 39
)
[10] => Array
(
[0] => 1452486002
[1] => 39
)
)
I want to omit all identical values except for the first and last, only when they show up sequentially. Think of this plotted on a line graph. I basically want to remove the unnecessary values that fall between two points of identical values. So the above array would change to this:
Array
(
[0] => Array
(
[0] => 1452483001
[1] => 40
)
[1] => Array
(
[0] => 1452483301
[1] => 39
)
[2] => Array
(
[0] => 1452484502
[1] => 39
)
[3] => Array
(
[0] => 1452484801
[1] => 38
)
[4] => Array
(
[0] => 1452485400
[1] => 38
)
[5] => Array
(
[0] => 1452485701
[1] => 39
)
[6] => Array
(
[0] => 1452486002
[1] => 39
)
)
You need to:
Keep track of the last temperature.
When a temperature change is detected, append the held row (if applicable) and the current row to the final array.
If there was no temperature change, hold the row as the last duplicate.
After the loop, append the held row to the final array if it is set.
This should work for you.
<?php
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
$last_temp = null;
$held_row = null;
while($row = mysql_fetch_assoc($res)) {
// If we're on a new temperature
if ($row['temperature'] !== $last_temp) {
// Append and clear the held row first
if (is_array($held_row)) {
$outside_temperature_array[] = [$held_row['date'], $held_row['temperature']];
$held_row = null;
}
// Append this row and note the temperature
$outside_temperature_array[] = array($row['date'], $row['temperature']);
$last_temp = $row['temperature'];
} else {
// Hold the row in case the next row is different
$held_row = $row;
}
}
// If the last row was not appended to the array
if (is_array($held_row)) {
// Append the held row
$outside_temperature_array[] = array($held_row['date'], $held_row['temperature']);
}
print_r($outside_temperature_array);
$res = mysql_query("SELECT `date`, `temperature` FROM `general` ORDER BY `date`", $db);
$outside_temperature_array = array();
$oldTemperature = null;
while($row = mysql_fetch_assoc($res)) {
if($oldTemperature == $row['temperature'] && next($row)['temperature'] == $oldTemperature )
continue;
}
$oldTemperature = $row['temperature'];
$outside_temperature_array[] = array($row['date'],$row['temperature']);
}
print_r($outside_temperature_array);
You can try passed eqvivalent element in array use continue, but this code non testing. I think maybe you can execute sql query for this. And mysql this old extensions you need use PDO or Mysqli.

Sort Data in php array according to some key

I want to sort my array according to idai value. I've tried to use SORT,KSORT,USORT but nothing useful.
Here is my data
Array
(
[0] => Array
(
[0] => Array
(
[idai] => 4
[id] => 6187
[name] => xyz
)
[1] => Array
(
[idai] => 5
[id] => 5256
[name] => abc
)
[2] => Array
(
[idai] => 10
[id] => 21921
[name] => qwe
)
[3] => Array
(
[idai] => 6
[id] => 29679
[name] => IOU
)
[4] => Array
(
[idai] => 11
[id] => 21062
[name] => STU
)
)
)
And I'm not sure why I'm getting this nested array..
Here is my how I declared my array:
$return_arr = array();
$return_arr['feed'] = array();
My code to store data in array from my db
$query = "SELECT * FROM user_post WHERE userid = '$friend_id'";
$result = mysql_query($query);
while( $row = mysql_fetch_array($result) ) {
$row_array['idai'] = $row['id'];
$row_array['id'] = $row['post_id'];
$row_array['name'] = $pic['name'];
array_push($return_arr['feed'],$row_array);
}
Try this code:
$return_arr['feed'][$row['id']] = $row_array;
or if you want not nested array:
$return_arr[$row['id']] = $row_array;
it will automaticly sort as array key is id

How do you add to a new index on a Multi-dimensional array within a foreach loop?

Hell once again, I am wondering how do you go about adding data to a new array index when inside a foreach loop?
the code I have atm is,
// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
$resultArray[] = $assoRow->name;
}
// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
$array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
echo "<br />"; print_r($array);
}
}
Below is the outputted data that comes from the "echo "br />"; print_r($array);" line.
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )
As you can see the array is being created a new over and over, what I need is for the above data to increment the 1st dimension index key on every loop, so it looks like...
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )
Thank you in advance I am out of all options in getting this to work and desperate.
Dan.
Change the values assigning part of your code to
$count=0;
foreach ($resultArray as $row) {
$array[$count][1] = $assoRow2->$row
$array[$count][2]=$row;
$count++;
echo "<br />"; print_r($array);
}
This code gets you the output you asked for without inefficiently using two queries:
// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);
$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);
$count = 0;
foreach ($row as $key => $value) {
$temp = array();
$temp[$count] = array(1 => $value, 2 => $key);
$count++;
echo "<br />"; print_r($temp);
}
Why you want it like this, I have no idea.
//extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
// 1st parameter is the array name, here array name is array .give gd name according to use
array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
echo "<br />"; print_r($array);
}
}

Categories