0

Hello I am trying to solve a problem. I've research and take time but unable to solve it sorry for that.

I am trying to show a database table query result in multiple table on a html page and every table should be a range i've put here 5 row at a table so if my query contain 29 row then it will show on 6 table and last table will contain 4 result the serial no of table will be correctly if first table 1st row is 01 then 2nd table 1st row will be 06. "the query result is not constant but it will depend on database record"

here is my code it shows only 1 table but not showing others table with result.

Thanks for your time. :)

$students = DB::table('mark_prc')
            ->select('student_roll','mark')
            ->where('cen_code', $c_code)
            ->where('sub_code',$subject)
            ->get();

    $k=0; //counter for serial no
    $m=5; // no of row each table
    $c = count($students); // now have data on array after query 29
    $p = ceil($c/5); // the data should be show on 6 tables now

    for($i=0;$i<$p;$i++){
   echo "<table>
            <tr>
                <th>SL</th>
                <th>Roll</th>
                <th>Mark</th>
            </tr>";
        for($j=$k;$j<$m;$j++){
            echo '<tr>
                    <td>'.($k+1).'</td>
                    <td>'.$students[$k]->student_roll.'</td>
                    <td>'.$students[$k]->mark.'</td>
                  <tr>';
            $k++;
        }
        echo '</table>';
    }
1
  • You should use better names for variables really. Commented May 15, 2015 at 5:06

1 Answer 1

1

Not sure why, but for($j=$k;is doing a reference assignment, so $j=&$k.

A workaround is to do -

for($j=($i*$m);$j<min((($i*$m)+$m),$c);$j++){

The ($i*$m), gets your start value, and ($i*$m)+$m) adds the 'no of row each table' The min((($i*$m)+$m),$c) in $j<min((($i*$m)+$m),$c) is to max out the loop at $c.

So now your code would look like -

$students = DB::table('mark_prc')
            ->select('student_roll','mark')
            ->where('cen_code', $c_code)
            ->where('sub_code',$subject)
            ->get();

    $m=5; // no of row each table
    $c = count($students); // now have data on array after query 29
    $p = ceil($c/5); // the data should be show on 6 tables now

    for($i=0;$i<$p;$i++){
   echo "<table>
            <tr>
                <th>SL</th>
                <th>Roll</th>
                <th>Mark</th>
            </tr>";
        for($j=(($i*$m));$j<min((($i*$m)+$m),$c);$j++){
            echo '<tr>
                    <td>'.($j+1).'</td>
                    <td>'.$students[$j]->student_roll.'</td>
                    <td>'.$students[$j]->mark.'</td>
                  <tr>';
        }
        echo '</table>';
    }
Sign up to request clarification or add additional context in comments.

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.