Moe Moe - 2 months ago 6
PHP Question

Print query in while loop, print some fields once, print some field many times

I wonder how i should do if i wanna print my $sta and $stb once, but print my $test aslong as i find them in database, $sta and $stb is unique key, but there is many $test in every sta and stb

<?php
//Establish connection to database
$host = "*****";
$conn = *******;
$query = " Select *
from p100f
LEfT OUTER JOIN ***** test ****
where p100sta = 1 AND p100stb = 1



//Execute query
$queryexe = db2_exec($conn, $query) ;
//Fetch results


while(db2_fetch_row($queryexe)) {

$sta = db2_result($queryexe, 'p100sta');
$stb = db2_result($queryexe, 'p100stb');
$test = ****test******

print("<div class='text-center'>
<h2>$sta and $stb</h2> <---------- this one i want to only print once
$test <------------------- this one i want it to repeat aslong
as i find them in database
</div>");


Now when i have both in while loop its printing like this.

$sta
$stb
$test

$sta
$stb
$test

$sta
$stb
$test

$sta
$stb
$test


but i want it to print like this

$sta
$stb
$test
$test
$test
$test
$test


any suggetions? thanks in advance

Answer
Try this code


//Execute query
$queryexe = db2_exec($conn, $query) ;
//Fetch results

$i=0;
while(db2_fetch_row($queryexe)) {

 $sta = db2_result($queryexe, 'p100sta');
 $stb = db2_result($queryexe, 'p100stb');
 $test = ****test******

   print("<div class='text-center'>

   if($i==0)
    {
        <h2>$sta and $stb</h2>      <---------- this one i want to only print once
    }
    else
    {
        continue;
    }
    $test       <------------------- this one i want it to repeat aslong
 as i find them in database
</div>");  
$i++;
}//while closed