Hi all i generated table,but it looks like the code is a bit messy,long,how can i do the same with the arrays or loops?

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
    table{border-collapse: collapse;}
    tr th, tr td{border: 1px solid black;}
   #resize
   {
       width :100%;
       position: relative;
   }
</style>
    </head>
    <body>

        <?php




       $numbers = mt_rand(1,100); 
       $numbers1 = mt_rand(1,100); 
       $numbers2 = mt_rand(1,100); 
       $numbers3 = mt_rand(1,100); 
       $numbers4 = mt_rand(1,100); 
       $numbers5 = mt_rand(1,100); 

           $numbers6 = mt_rand(1,100); 
       $numbers7 = mt_rand(1,100); 
       $numbers8 = mt_rand(1,100); 
       $numbers9 = mt_rand(1,100); 
       $numbers10 = mt_rand(1,100); 
       $numbers11 = mt_rand(1,100); 
       ?>
        <div id="resize">
<table width ="100%">
    <tr>
        <th>knumber</th>
        <th>WP</th>
        <th>WD</th>
        <th>ID</th>
        <th>DV</th>
        <th>HC</th>
        <th>PR</th>
    </tr>
    <tr>
        <td>K001</td>
        <td><pre><?php print_r($numbers);?></pre></td>
        <td><pre><?php print_r($numbers1);?></pre></td>
        <td><pre><?php print_r($numbers2);?></pre></td>
        <td><pre><?php print_r($numbers3);?></pre></td>
        <td><pre><?php print_r($numbers4);?></pre></td>
        <td><pre><?php print_r($numbers5);?></pre></td>

    </tr>
    <tr>
        <td>K002</td>
         <td><pre><?php print_r($numbers6);?></pre></td>
        <td><pre><?php print_r($numbers7);?></pre></td>
        <td><pre><?php print_r($numbers8);?></pre></td>
        <td><pre><?php print_r($numbers9);?></pre></td>
        <td><pre><?php print_r($numbers10);?></pre></td>
        <td><pre><?php print_r($numbers11);?></pre></td>
    </tr>

</table>
            </div>
            </body>
            </html>

Recommended Answers

All 7 Replies

Member Avatar for diafol

From your code it looks aas though you should be outputting an array from the print_r() but you're actually only creating separate variables holding a single integer each.

From your code, what you may be able to do is the following:

$rows = 2;
$columns = 6; //count of cols from WP to PR

$table = '';
for($r=0;$r<$rows;$r++) {
    $table .= "<tr><td>K" . str_pad(($r+1),3,'0',STR_PAD_LEFT) . "</td>";
    for($c=0;$c<$columns;$c++){
        $table .= "<td>".mt_rand(1,100)."</td>";
    }
    $table .= '</tr>';
}

Then your HTML:

<div id="resize">
    <table width ="100%">
        <tr>
            <th>knumber</th>
            <th>WP</th>
            <th>WD</th>
            <th>ID</th>
            <th>DV</th>
            <th>HC</th>
            <th>PR</th>
        </tr>
        <?=$table?>
    </table>
</div>

Thank you.Would like to ask in what situations i need to use nested forloops?

You have very good approach,lots to learn out of it for me.
I tried to experiment a little bit. But how do i add an empty column or raw for calculations without affecting existing once?

Member Avatar for diafol

No real change for HTML:

<div id="resize">
        <table width ="100%">
                <tr>
                    <th>knumber</th>
                    <th>WP</th>
                    <th>WD</th>
                    <th>ID</th>
                    <th>DV</th>
                    <th>HC</th>
                    <th>PR</th>
                    <th>Total</th>
               </tr>
               <?=createRandomTable($rows,$columns)?>
        </table>
</div>

The PHP code to go before it could be like this:

function createRandomTable($rows,$columns)
{
    $table = '';
    for($r=0;$r<$rows;$r++) {
        $table .= "<tr><td>K" . str_pad(($r+1),3,'0',STR_PAD_LEFT) . "</td>";
        $tot = 0;
        for($c=0;$c<$columns;$c++){
            $rnd = mt_rand(1,100);
            $table .= "<td>$rnd</td>";
            $tot += $rnd;
        }
        $table .= "<td>$tot</td></tr>";
    }
    return $table;
}

$rows = 2;
$columns = 6;

Would like to ask one more question regarding table above.I try to calculate now the average on the each column,so far i got this:

 function createRandomTable($rows,$columns)
{
    $table = '';
    for($r=0;$r<$rows;$r++) {
        $table .= "<tr><td>K" . str_pad(($r+1),3,'0',STR_PAD_LEFT) . "</td>";
        $tot = 0;
        for($c=0;$c<$columns;$c++){
            $rnd = mt_rand(1,100);
            $table .= "<td>$rnd</td>";

            $tot += $rnd;
            $thArray = ['#knum', '#wp','#id','#dv','#hc','##pr'];
            $thTotal = count($thArray);
            $avrg = round($tot/$thTotal);


        }
        $table .= "<td><strong>$avrg</strong></td></tr>";

    }
    return $table;
}



$rows = 10;
$columns = 6;


       ?>




        <div id="resize">
        <table width ="100%">

                <tr>
                    <th id="knum">knumber</th>
                    <th id="wp">WP</th>
                    <th id="wd">WD</th>
                    <th id="id">ID</th>
                    <th id="dv">DV</th>
                    <th id="hc">HC</th>
                    <th id="pr">PR</th>
                    <th>Average Grade/Student</th>
               </tr>

               <tr>
                   <td>Average Subject</td>
                   <td> <?php  function scope(){ $GLOBALS['$thArray'];}  scope(); echo $thArray;?> </td>
                   <td></td>
                   <td></td>
                   <td></td>
                   <td></td>
                   <td></td>
                   <td></td>
               </tr>

               <?=createRandomTable($rows,$columns)?>
        </table>
</div>

What is the trick to count columns?Sorry for stupid question,but i am learning how to work with tables data.

Member Avatar for diafol

Good luck

Thank you.

How to count vertically values from the columns?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.