Sorting data in php/mysql -
so have question regarding, hmmm filtering data in mysql/php.
so have table lets say
id | fk1 | fk2 1 1 1 2 1 2 3 1 3 4 2 1 5 2 2 6 3 3
basicaly have in same table many occurancies of fk1 referenced fk2 , when output in table ofcourse result
1 1 1 2 1 3 2 1
etc. , normal. want output 1 time fk2 value lets 1, , corresponding fk1 values below it. sorta group each occurancie of fk2 need fk1s have fk2 in row.
i managed working somehow fk1 print out , corresponding fk2 other way around wont work :s
$fk1 = ''; while($row = mysql_fetch_array($result_set)){ if($fk1 != $row['fk1']){ if($fk1 != ''){ echo $printout; } $fk1 = $row['fk1']; $printout = "<tr class='success'><td>" . "<strong>" . $row['fk1']. "</td></tr>"; } $printout = $printout .'<td class="warning"> '.$row['fk2']. '</td></tr>'; } echo $printout ;
any idea how smart loop instead of using multiple queries on base? thanks
for each iteration of loop prints query, can check if previous iteration printed same thing , (if yes) skip printing or print current 1 if not.
i hope understood question , answer clear enough.
something this:
$cfk1 = ""; foreach($query_result $cval){ if($cval != cfk1){ print $cval; } $cfk1 = $cval; }
Comments
Post a Comment