Hilarius L. Doren
Hilarius L. Doren

Reputation: 757

PHP Cannot Insert Value Into Array

I have problem to set my array value with query result. I have document table with ID_ATTACHMENT as primary key autoincreament, ID_TRANSACTION, DOCUMENT_NAME, NO_DOCUMENT, REMARKS, NAMA_FILE

$queryattacth = mysql_query("SELECT * FROM document A WHERE A.ID_TRANSACTION='13' ");
$tempAttc = array();
for ($i=1;$i<=15;$i++)
    {
        for ($j=0;$j<5;$j++)
            $tempAttc[$i][$j]="";
    }

while ($dataattach = mysql_fetch_array($queryattacth)){
        if (strtoupper($dataattach['DOCUMENT_NAME'])=='A')
        {
            $tempAttc[1][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[1][3]=$dataattach['REMARKS'];
            $tempAttc[1][4]=$dataattach['NAMA_FILE'];
        }
        else if (strtoupper($dataattach['DOCUMENT_NAME'])=='B'){
            $tempAttc[2][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[2][3]=$dataattach['REMARKS'];
            $tempAttc[2][4]=$dataattach['NAMA_FILE'];
        }
        else if (strtoupper($dataattach['DOCUMENT_NAME'])=='C'){
            $tempAttc[3][2]=$dataattach['NO_DOCUMENT'];
            $tempAttc[3][3]=$dataattach['REMARKS'];
            $tempAttc[3][4]=$dataattach['NAMA_FILE'];
        }
       .................
}

The result is doesn't show any value for this variable

$tempAttc[1][2];
$tempAttc[1][3];
$tempAttc[1][4];

but have result for this variable

 $tempAttc[2][2];
 $tempAttc[2][3];
 $tempAttc[2][4];

Anyone can hep me about why this happened?

Upvotes: 1

Views: 126

Answers (2)

user1932079
user1932079

Reputation:

First RED ALERT: mysql_ functions are deprecated... they are on the cutting room floor and about to be swept away. Take a look into PDO or mysqli_. That said, I have kept the deprecated functions in this answer.

In the code below, I have added comments on why I did things the way I did.

//Assuming that DOCUMENT_NAME goes from A to the 15th letter of
//the alphabet, order by it to make things easier.
$queryattacth = mysql_query("SELECT * 
                             FROM document A 
                             WHERE A.ID_TRANSACTION='13'
                             ORDER BY DOCUMENT_NAME");

//Initialize $tempAttc as an empty array (to be fully populated by the query)
$tempAttc = array();

//Use MYSQL_NUM to get an array without the associative (text) indices.
while ($dataattach = mysql_fetch_array($queryattacth, MYSQL_NUM)) {
  //Assuming the columns are exactly ordered as you have listed, push
  //the results into the array (will include all columns, not just 2-4)
  $tempAttc[] = $dataattach;
}

That should do what you want up to the part about inserting into a PDF (which is not part of your question).

Upvotes: 0

Hilarius L. Doren
Hilarius L. Doren

Reputation: 757

I just have try this and it solved my problem

$tempAttc[1][2]=$tempAttc[1][2].''.$dataattach['NO_DOCUMENT'];
$tempAttc[1][3]=$tempAttc[1][3].''.$dataattach['REMARKS'];
$tempAttc[1][4]=$tempAttc[1][4].''.$dataattach['NAMA_FILE'];

But I still don't understand about my problem, maybe anyone can explain why those combination not working for me...

Upvotes: 1

Related Questions