1

I am making a program that generates monthly report from the weekly report that is save from database. I can fetch the data from database through loop but the problem is i want to use the value from loop and assign it to a variable.

here's my code:

$mon=$_SESSION['mot'];
    $query2 = mysql_query("SELECT * FROM keyper WHERE MONTH(date) = '$mon'");
        while($row2 = mysql_fetch_array($query2))
        {
            $tar1 =  $row2["target"];
            $hi = $row2["high1"];
            echo $tar1;
            echo $hi;
        }

After that query I got the result like this:

target1

high1

target2 

high2

target3

high3

I want to assign the result of the loop into a variable like this

$a="target1";

etc...

How can I do that? Can someone help me?

Thanks in advance.

2

3 Answers 3

2

Instead you pass them into an array..

while($row2 = mysql_fetch_array($query2))
{
    $tar1[] =  $row2["target"]; //<--- The $tar1 variable is now an array
    $hi[] = $row2["high1"];     //<--- The $hi variable is now an array

}

So you access it like echo $tar1[0]; that prints target1 and echo $hi[1]; will print high2 etc and so on....

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

1 Comment

Wow! that is just the code that i needed. thank you so much. you save my life. :)
0

You can use array to store the value.. like...

$tar1 = array();
$hi = array();
$i=0;
while($row2 = mysql_fetch_array($query2))
     {
         $tar1[$i] =  $row2["target"];
         $hi[$i] = $row2["high1"]; 
         $i++;
     }
print_r($tar1);
print_r($hi);

Comments

0
$count = 1;
while($row2 = mysql_fetch_array($query2))
{
    $tar[$count][] =  $row2["target"]; //<--- The $tar1 variable is now an array
    $hi[$count][] = $row2["high1"];     //<--- The $hi variable is now an array
    $count++
}

foreach($tar as $key=>$targets)
{
echo $targets[0].$key;
}

just keep count in keys

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.