1

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?

7
  • what data your table is containing?? and why are you checking the same condition twice?? (strtoupper($dataattach['DOCUMENT_NAME'])=='B') Commented Sep 18, 2014 at 3:36
  • If your ID is unique, then you have only one result, right? So only one match and apparently that's for B. Also, you have 2 else-if for the same value, B, so the second will never execute. Commented Sep 18, 2014 at 3:37
  • @sgt : Sorry that's my fault, it suppose to be 'C', I already edit my question Commented Sep 18, 2014 at 3:46
  • @JeremyMiller : as programming logic you're right as I think, that I face that problem Commented Sep 18, 2014 at 3:48
  • You're clearly trying to get some help, so let me suggest editing to answer this: What are you trying to achieve? As my comment in your answer below suggests, if you already know the answer because A.ID_TRANSACTION='13' exists and has a known value, then you could just make the array without having to access the DB, so what problem are you actually solving? Commented Sep 18, 2014 at 4:21

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

9 Comments

If you have an edit to your post, you should edit it, not post it as an answer. :)
@JeremyMiller : I mean I just solved my problem with this combination codding so I can insert value to my arrays variable. And just not understand why the first combination is not working because as logic it suppose to working. Is that should edit the post even give an answer to my own problem? Please give give a lead because I am new in this forum. Thank You. :)
You have not solved the problem, but faked the solution and that is why you don't understand why it is working. You've identified that it appears to be solving the problem, but are not sure why, that's why I suggested it as an edit.
Namely, $tempAttc[1][...] is empty to begin with and adding in .''. does nothing (it's an empty string concatenated between 2 other strings). All you did was force the values to go into the slots you wanted. That takes away your if-else statements.
Well, on another thought, if A.ID='13' is all you will ever use this for, then you could just hard code the solution, but then why do the DB query in the first place? This is why I have not posted an answer -- I don't fully "get" the question.
|
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.