1

Need to split result ( dynamic - mysql ) into columns ( as per define below $cols )

Note: if anyone have solution in any other format please let me know ...

$cols=3; // note i can change this to 4,5 ..2 etc..

echo "<table border='0'  align='center' valign='top' >";
do{
        echo "<tr>";
                    for($i=1;$i<=$cols;$i++){    
            $rowsA=data_fetch($sqlquery);
                        if($rowsA){
        echo'<td align="center" valign="top">
                    <table  border="1px" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                        <tr align="center" valign="top">';
                                echo '<td class="dateis" align="center" valign="top" width="35px"> <strong>'.$srno.'</strong> </td>';
                                echo '<td class="dateis" align="center" valign="top" width="100px">'.$rowsA['prdname'].'</td>';
                                echo '<td class="dateis" align="center" valign="top" width="50px">'.number_format($rowsA['prdwgh'],3,'.','').'</td> 
                                    </tr>';
                                   $srno++;
                       echo'</tr>
                    </table>
        </td>';
    } else{
          }
      }
    } while($rowsA);
    echo "</table>";

Hence from above i get below display :

1 Prda 1.4  |  2 prdan  2.3  |  3  prdbc  3.1
4 prdf 0.02 |  5 prdcv  1.0  |  6  prdd   0.9
7 prdc 0.3  |  8 prdbn  2.8  |  9  prdf   0.5

but i need as below :

1 Prda 1.4     |  4 prdf   0.02    |  7  prdc   0.3 
2 prdan  2.3   |  5 prdcv  1.0     |  8  prdbn  2.8
3 prdbc  3.1   |  6 prdd   0.9     |  9  prdf   0.5

Update :

if there is 10 records :

1 Prda 1.4     |  5 prdcv  1.0    |  9  prdf   0.5
2 prdan  2.3   |  6 prdd   0.9    |  10 prdf   2.5
3 prdbc  3.1   |  7 prdc   0.3    |  
4 prdf   0.02  |  8 prdbn  2.8    | 

if there is 11 records :

1 Prda 1.4     |  5 prdcv  1.0    |  9  prdf   0.5
2 prdan  2.3   |  6 prdd   0.9    |  10 prdf   2.5
3 prdbc  3.1   |  7 prdc   0.3    |  11 prdfd  2.1
4 prdf   0.02  |  8 prdbn  2.8    | 

Thanks

5
  • if anyone have solution in any different way ..will do that to... thanks Commented Jan 28, 2016 at 6:31
  • any one please help me.... Commented Jan 28, 2016 at 7:29
  • Suppose $cols=4, then upto where it will display? Commented Jan 28, 2016 at 8:05
  • then there will be fourth column... at last Commented Jan 28, 2016 at 8:27
  • @santhy , can u please tell me solution with $cols=3 ... Commented Jan 28, 2016 at 8:31

1 Answer 1

2

Sample for cols = 3. Please tweak with your db name and its column. hope this results as you expected.

$NoOfColumns = 3;
$dataArr = $matrixArr = array();

$query = "SELECT * FROM testDb ORDER BY prdname";
$result = mysqli_query($conn, $query) or die("error getting data");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    array_push($dataArr, $row);
}
$totalCnt = count($dataArr);
$NoOfRows = floor($totalCnt / $NoOfColumns);
if ($havRem = $totalCnt % $NoOfColumns) {
    $NoOfRows++;
}
$matrixArr = array_chunk($dataArr , $NoOfRows);

//echo "<pre>";print_r($dataArr);echo "</pre>";
//echo "<pre>";print_r($matrixArr);echo "</pre>";

echo "<table border=1>";
for ($row = 0; $row < $NoOfRows; $row++) {
    echo "<tr>";
    $srno = $row+1;
    for ($col = 0; $col < $NoOfColumns; $col++) {        
    echo'<td align="center" valign="top">';
            if ($srno<=$totalCnt) {
             echo   '<table  border="1px" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
                    <tr align="center" valign="top">';
                            echo '<td class="dateis" align="center" valign="top" width="35px"> <strong>'.$srno.'</strong> </td>';
                            echo '<td class="dateis" align="center" valign="top" width="100px">'.$matrixArr[$col][$row]['prdname'].'</td>';
                            echo '<td class="dateis" align="center" valign="top" width="50px">'.$matrixArr[$col][$row]['prdwgh'].'</td> 
                                </tr>';

                   echo'</tr>
                </table>';
            }
    echo '</td>';
    $srno = $srno+$NoOfRows;
}
echo "</tr>";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

3 Comments

thanks its working but as you know my data are dynamic hence if i have 10 records ...then as per u r above structure its showing only 9 records ...the 10 records is hide ... also i tested with $NoOfColumns = 4; its working but same ..10 records is not show ...
oh you mean according to the data count the num of colums will increase. say 9 then 3, 10 to 12 then 4 colm, 13 to 15 then 5 etc...?
no numberofcol will be set as per its variable ..but row need to be updated ..please se my update question

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.