madmatuk
madmatuk

Reputation: 127

Creating dynamic matrix table using a formula with PHP

I am trying to get my head around creating a matrix type table created dynamically using a formula and some predefined numbers, I know a little php but this is far beyond my scope.

Ok I have numerous static widths (mm) eg : 100 , 200, 300, 400, 500 .... up to say 1200. I also have numerous heights (mm )eg : 50, 60, 70, 80 ... up to say 1500

I have a price start point of £15, this would relate to the minimum width and height, 100 x 50

I then have a formula to multiply the preceding row cell by 1.6 to give a new price.

How would I be able to create a matrix table on the fly using this data? What I am trying to achieve as an example is as below.

width=> 100     200     300     400     500     600

Drop v

50      £15     £24     £38     £61     £98     £157

60      £24     £38     £61     £98     £157    £251

70      £38     £61     £98     £157    £251    £401

80      £61     £98     £157    £251    £401    £643

90      £98     £157    £251    £401    £643    £1028

100    £157     £251    £401    £643    £1028   £1646

I also need to assign all the values to a table in mysql defined as below for each permutation. The records already exist for each width and height permitation.

eg

width : 100

height : 50

price : 15

So I need to get all the prices in a workable array to do an insert into the relevant record in database.

I hope this makes sense and someone can point me in the right direction.

Upvotes: 1

Views: 8655

Answers (2)

MeLight
MeLight

Reputation: 5575

Here's how you build the values matrix when the first value is 15.

<?php
$initVal = 15;
$rows = 6;
$cols = 6;
$matrix = array();
for($i = 0; $i < $rows; $i++) {
    if($i != 0)
        $initVal = round($matrix[$i-1][0]*1.6);
    $matrix[$i] = array();
    for($j = 0; $j < $cols; $j++) {
        if($j == 0) 
            $matrix[$i][$j] = $initVal;
        else
            $matrix[$i][$j] = round($matrix[$i][$j-1]*1.6);
    }
}

print_r($matrix);
?>

Upvotes: 1

deepi
deepi

Reputation: 1081

100,200,...........1200---->12 columns 50,60,70,..........1500---->145 rows

echo "<table id='tb'>";
 $height=50;
$i=0;
for($i=0;$i<145;$i++)
 {
   echo "<tr height='".$height."'>";
   echo "<td width='100px'>".$value1."</td>";
   echo "<td width='200px'>".$value2."</td>";
   echo "<td width='300px'>".$value3."</td>";
   echo "<td width='400px'>".$value4."</td>";
   echo "<td width='500px'>".$value5."</td>";
   echo "<td width='600px'>".$value6."</td>";
   echo "<td width='700px'>".$value7."</td>";
   echo "<td width='800px'>".$value8."</td>";
   echo "<td width='900px'>".$value9."</td>";
   echo "<td width='1000px'>".$value10."</td>";
   echo "<td width='1100px'>".$value11."</td>";
   echo "<td width='1200px'>".$value12."</td>";
   echo "</tr>";
   $height=$height+10;
   //here '$value1 to $value12' is caluculated values
 }
 echo "</table>";

Upvotes: 0

Related Questions