简体   繁体   中英

Populate the array in php using mysql dynamically

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
     )

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;
}

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM