How would I create a table that multiples height by weight?
I have no idea how to figure it out!
I am trying to create a BMI calculator.
// collect values from a form sent with method=get
$min_weight = mysql_real_escape_string($_GET["min_weight"]);
$max_weight = mysql_real_escape_string($_GET["max_weight"]);
$min_height = mysql_real_escape_string($_GET["min_height"]);
$max_height = mysql_real_escape_string($_GET["max_height"]);
?>
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td cla ss="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<table border="1">
<?php
echo "<tr><td>Height →<br>Weight ↓</td>";
for ( $i = $min_height; $i <= $max_height; $i+=5 )
{
echo "<td>{$i}</td>";
}
echo "</tr>";
for ( $j = $min_weight; $j <= $max_weight; $j+=5 )
{
echo "<tr>";
echo "<td>{$j}</td>";
echo "</tr>";
}
This should do the job, i've also put some isset()'s:
<?php
// collect values from a form sent with method=get
$min_weight = isset($_GET["min_weight"]) ? mysql_real_escape_string($_GET["min_weight"]):0;
$max_weight = isset($_GET["max_weight"]) ? mysql_real_escape_string($_GET["max_weight"]):0;
$min_height = isset($_GET["min_height"]) ? mysql_real_escape_string($_GET["min_height"]):0;
$max_height = isset($_GET["max_height"]) ? mysql_real_escape_string($_GET["max_height"]):0;
?>
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td class="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<?php
$table = '<table border="1">';
$table .= '<tr><td>Height →<br>Weight ↓</td>';
for ($i = $min_height;$i <= $max_height;$i += 5){
$table .= "<td>$i</td>";
}
$table .= "</tr>";
for($j = $min_weight; $j <= $max_weight;$j += 5){
$table .= "<tr><td>$j</td>";
for ($i = $min_height;$i <= $max_height;$i += 5){
$table .= '<td>'. $i * $j .'</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
echo $table;
?>
Even though this works is it wrong to have the table border outside of the php tags?
<table>
<tr>
<td>Key:</td>
<td class="good show">Healthy (20-25)</td>
<td class="warning show">Overweight (25-30)</td>
<td class="bad show">Unhealthy ( -20 or 30+)</td>
</tr>
</table>
<table border="1">
<?php
echo "<tr><td>Height →<br>Weight ↓</td>";
for ( $j = $min_height; $j <= $max_height; $j+=5 )
{
echo "<td>{$j}</td>";
}
for ( $i = $min_weight; $i <= $max_weight; $i+=5 )
{
echo "<tr><td>{$i}</td>";
for ( $j = $min_height; $j <= $max_height; $j+=5 )
{
$var = number_format(($i)/(($j/100)*($j/100)),'2','.','');
{
echo "<td class='warning'>";
}
echo $var;
echo "</td>";
}
echo "</tr>";
}
?>
</table>
Related
I have three tables created by uploaded data from the previous file. I would like to insert data in defined places, so that each service is a separate record in the table and has a separate quantity and amount assigned.
My php function:
function listService()
{
$service_chcecked = $_POST['service_chcecked'];
$quantity = $_POST['quantity_chcecked'];
$net_price = $_POST['net_price_chcecked'];
for ($x = 0; $x < count($service_chcecked); ++$x) {
echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
for ($y = 0; $y < count($net_price); ++$y) {
echo '<td>price:' . $net_price[$y] . '</td>';
for ($z = 0; $z < count($quantity); ++$z) {
echo '<td>quantity:' . $quantity[$z] . '</td>';
};
};
echo '</tr>';
}
}
And my html place:
<div class='services'>
<table>
<tr>
<th><span>NO.</span></th>
<th><span>Service name</span></th>
<th><span>Net price</span></th>
<th><span>Quantity</span></th>
</tr>
<?php listServiceName(); ?>
</table>
</div>
Now it displays to me this way, with repeated data at the end :/
broken table
That's because you are nesting your for loops. You create the <tr> and the "id" and "name" <td> for each item in service_chcecked. Then you create a "price" <td> for each service_chcecked * net_price_chcecked. And your 3rd nested loop creates a "quantity" column for each service_chcecked * net_price_chcecked * quantity_chcecked. That's why you end up with that broken table. It depends on how you receive the POST data, but if your three arrays always have the same length, you could do it all in one loop:
function listService()
{
$service_chcecked = $_POST['service_chcecked'];
$quantity = $_POST['quantity_chcecked'];
$net_price = $_POST['net_price_chcecked'];
for ($x = 0; $x < count($service_chcecked); ++$x) {
echo '<tr><td>id:' . $x . '</td><td>name:' . $service_chcecked[$x] . '</td>';
echo '<td>price:' . $net_price[$x] . '</td>';
echo '<td>quantity:' . $quantity[$x] . '</td>';
echo '</tr>';
}
}
Its hard to understand whats happening but less is more and try not to mix your data & html inside your function its going to confuse you later on.
<?php
$services = $_POST['service_checked'];
$qtys = $_POST['quantity_checked'];
$prices = $_POST['net_price_checked'];
?>
<div class='services'>
<table>
<tr>
<th>NO.</th>
<th>Service name</th>
<th>Net price</th>
<th>Quantity</th>
</tr>
<? foreach($services as $k=>$service){?>
<tr>
<td><?=$k?></td>
<td><?=$service?></td>
<td><?=$qtys[$k]?></td>
<td><?=$prices[$k]?></td>
</tr>
<? }?>
</table>
</div>
I've looked through some of the other answers on this, but they don't seem to solve my particular issue (or I am coding them wrong, which is also possible).
I know my SQL code works right, as I get all 19 rows of output, but when I output it to a PHP page, the page shorts me by 5 rows.
If I don't try to show the results in three columns by whatever (and just use one column) everything shows. When I try to do three by 'X', is when it starts cutting off results.
Here is my old, not working PHP code:
$fleet_query_results = #mysql_query($fleet_query, $connection) or die(mysql_error());
while($row = mysql_fetch_array($fleet_query_results)) {
$display_fleet .= "<tr>";
for($i = 1; $i < 4 && $row = mysql_fetch_array($fleet_query_results); $i++) {
$ship_info = $row['ship_info'];
$mem_info = $row['mem_info'];
$ship_link = $row['ship_link'];
$display_fleet .= "
<td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
";
}
$display_fleet .= "</tr>";
}
Any ideas? It's cutting off the first, fourth, ninth, thirteenth and seventeenth results.
Example page results:
<table border="1">
<tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
<tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
<tr><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td><td>Header<br />Image<br />People</td></tr>
</table>
New code that works, but is UGLY:
$fleet_query_results = #mysql_query($fleet_query, $connection) or die(mysql_error());
while($row = mysql_fetch_array($fleet_query_results)) {
$display_fleet .= "<tr>";
$ship_info = $row['ship_info'];
$mem_info = $row['mem_info'];
$ship_link = $row['ship_link'];
$display_fleet .= "
<td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
";
for($i = 1; $i < 3 && $row = mysql_fetch_array($fleet_query_results); $i++) {
$ship_info = $row['ship_info'];
$mem_info = $row['mem_info'];
$ship_link = $row['ship_link'];
$display_fleet .= "
<td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
";
}
$display_fleet .= "</tr>";
}
Your while was eating some of the data rows.
$fleet_query_results = #mysql_query($fleet_query, $connection) or die(mysql_error());
$rows=1;
while($rows) {
$display_fleet .= "<tr>";
for($i = 1; $i < 4 && $row = mysql_fetch_array($fleet_query_results); $i++) {
$rows=$row;
if (!$row) break;
$ship_info = $row['ship_info'];
$mem_info = $row['mem_info'];
$ship_link = $row['ship_link'];
$display_fleet .= "
<td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
";
}
$display_fleet .= "</tr>";
}
or, as discussed below:
$i=0
while($row = mysql_fetch_array($fleet_query_results)) {
if( $i % 4 = 0 )
$display_fleet .= "<tr>";
$ship_info = $row['ship_info'];
$mem_info = $row['mem_info'];
$ship_link = $row['ship_link'];
$display_fleet .= "
<td valign=\"top\"><div id=\"fleet\"><div id=\"fheader\">$ship_info</div><br/><img align=\"center\" height=\"210\" width=\"350\" alt=\"$ship_name\" src=\"$ship_link\" /><br /><div id=\"fmem\">$mem_info</div></div></td>
";
if( ++$i % 4 = 0 )
$display_fleet .= "</tr>\n";
}
// clean add the missing cells to last row.
if( $i % 4 != 0 )
{
while($i++ %4)
$display_fleet .= "<td></td>";
$display_fleet .= "</tr>\n";
}
I know this has been asked before and I have got it working using the following code:
<?php
$maxcols = 8; $i = 0;
echo "<table id='table1'><tr>";
foreach ($id as $k => $v) {
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols) { $i = 0; echo "</tr><tr>"; }
} $i++;
while ($i <= $maxcols) {
$i++; echo "<td></td>";
}
echo "</tr></table>";
?>
This results in a table that looks like this :
I'd like to add headers to this so the end result looks like this:
I'd like to do it dynamically so if I create a table that is only 5 columns wide I'd get on the first header row ID01 - ID05 and on the second header row ID06 - ID10
I want to limit the header ID values to be no more than $maxid any extra header fields should be blank, like this : If $maxid = 12; then :
I need the header rows are made as follows and not using <TH>
<td class="mark">
I'm using some javascript to allow the movement of cell data.
The class is used to set the formatting on the header and stop items from being dragged into the fields.
Can anyone point me in the right direction on how to do this.
This should help you.
$maxcols = 8;
$maxid = 12;
$startid = 1;
echo "<table id='table1'>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {
echo "<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
if ($startid <= $maxid)
echo " <td class='mark'>ID".$startid++."</td>\n";
else
echo " <td> </td>\n";
echo "</tr>\n<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
echo "<td>Content</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
Generates
<table id='table1'>
<tr>
<td class='mark'>ID1</td>
<td class='mark'>ID2</td>
<td class='mark'>ID3</td>
<td class='mark'>ID4</td>
<td class='mark'>ID5</td>
<td class='mark'>ID6</td>
<td class='mark'>ID7</td>
<td class='mark'>ID8</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td class='mark'>ID9</td>
<td class='mark'>ID10</td>
<td class='mark'>ID11</td>
<td class='mark'>ID12</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
try this. It will work in the same way as you desired
<?php
$id= array("1","2","3","4","5","6","7","8","9","10","11","12");
$maxcols = 8; $i = 0;$j=0;$t=0;$s=0;
$maxid = count($id);
echo "<table id='table1'><tr>";
foreach ($id as $k => $v)
{
if($t == 0)
{
while ($t <= $maxcols-1) {
if($s < $maxid)
{
$s++;$t++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$t++;$s++;
}
}
echo "</tr><tr>";
}
else
{
}
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols)
{
echo "</tr><tr>";
if($j == 0)
{
while ($j <= $maxcols-1) {
if($s < $maxid)
{
$s++;$j++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$j++;$s++;
}
}
echo "</tr><tr>";
}
$i=0;
}
}
echo "</tr></table>";
?>
Output
Hi You can Use My Library:
class generate{
private $row = "<tr>{columns}</tr>";
private $td = "<td {attr}>{data}</td>";
private $attributeTR="";
private $attributeTD="";
private $tdBuilder="";
public function addCol($ColumValsArr=array("class='motota'"=>"Example")){
foreach($ColumValsArr as $key=>$val){
$newCol = str_replace("{data}",$val,$this->td);
$newCol = str_replace("{attr}",$key,$newCol);
$this->tdBuilder .= str_replace("{data}",$key,$newCol);
}
}
public function getRow(){
return str_replace("{columns}",$this->tdBuilder,$this->row);
}
}
<?php
function TableFunc($Data)
{
$Table = "<table>" . PHP_EOL;
foreach ($Data as $tags => $array) {
$Table .= "<$tags>" . PHP_EOL;
foreach ($array as $thead) {
$tag=$tags==="tbody"?"td":"th";
$Table .= "<tr>" . PHP_EOL;
if (is_array($thead)) {
foreach ($thead as $theadItem) {
if (is_array($theadItem))
$Table .= "<$tag colspan='$theadItem[1]'>$theadItem[0]</$tag>" . PHP_EOL;
else
$Table .= "<$tag>$theadItem</$tag>" . PHP_EOL;
}
}
$Table .= "</tr>" . PHP_EOL;
}
$Table .= "</$tags>" . PHP_EOL;
}
$Table .= "</table>" . PHP_EOL;
return $Table;
}
$Data = array(
"thead" => [
[["GENEL BİLGİ", 2], ["KALORİMETRE (ISINMA)", 2], ["HESAPLAMA", 2]],
["NO", "AD SOYAD", "FARK", "TUTAR", "OKUMA", "ÖDENECEK"]
],
"tbody"=>array(
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
),
"tfoot" => [["NO", "AD SOYAD", "M2", "MAHSUP", "SAYAÇ", "15°", "FARK", "TUTAR", "ORTAK ALAN", "EKSTRA", "MUAFİYET", "OKUMA", "ÖDENECEK"]]
);
echo TableFunc($Data);
I'm using this code to get the latest events:
<?php
$all_events = array();
$ten_events = array();
for($i = 0; $events = mysql_fetch_object($events_resource); $i++){
if($i < 5){
$ten_events[] = $events;
}
$all_events[] = $events;
}
?>
<?php foreach($ten_events as $events){ ?>
<tr>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->date; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->category; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->who; ?></td>
<td class="row_<?PHP echo $i % 2; ?>"><?php echo $events->location; ?></td>
</tr>
<?php } ?>
Now I have this code to make it finally work, but I don't know how to combine it with the code above:
<?PHP
$i = 0;
while($row = #mysql_fetch_row($result)){
?>
<?PHP
$i++;
}
?>
The CSS-classes "row_0" and "row_1" are ready.
.row_0 {
background-color: #424140;
padding: 2px;
color: #f3f2ea;
}
.row_1 {
background-color: #555352;
padding: 2px;
color: #f3f2ea;
}
Hope you can help me.
<?php
$all_events = array();
$ten_events = array();
for($i = 0; $events = mysql_fetch_object($events_resource); $i++){
if($i < 5){
$ten_events[] = $events;
}
$all_events[] = $events;
}
$i = 0;
foreach($ten_events as $event){
$row = $i % 2;
echo "<tr>
<td class='row_{$row}'>{$event->date}</td>
<td class='row_{$row}'>{$event->category}</td>
<td class='row_{$row}'>{$event->who}</td>
<td class='row_{$row}'>{$event->location}</td>
</tr>";
$i++;
}
?>
I would to it like this.
I need to read a bi-dimensional array and convert the values into an HTML table. For example:
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:
That is, I should be able to create the following syntax from the $grid above:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td> </td>
<td> </td>
</tr></table>
I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.
Any help greatly appreciated!
Code:
<?php
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
);
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
$w = count($grid[0]);
$h = count($grid);
for ($y = 0; $y < $h; ++$y) {
echo "<tr>\n";
for ($x = 0; $x < $w; ++$x) {
$value = $grid[$y][$x];
if ($value === null) {
continue;
}
$colspan = 1;
while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
$grid[$y][$x + $colspan++] = null;
}
$rowspan = 1;
$rowMatches = true;
while ($rowMatches && $y + $rowspan < $h) {
for ($i = 0; $i < $colspan; ++$i) {
if ($grid[$y + $rowspan][$x + $i] !== $value) {
$rowMatches = false;
break;
}
}
if ($rowMatches) {
for ($i = 0; $i < $colspan; ++$i) {
$grid[$y + $rowspan][$x + $i] = null;
}
++$rowspan;
}
}
$rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
$colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";
echo " <td$rowspan$colspan>$value</td>\n";
}
echo "</tr>\n";
}
?>
Output:
<tr>
<td colspan="4">4</td>
</tr>
<tr>
<td rowspan="2" colspan="2">3</td>
<td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="2">0</td>
</tr>
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
foreach($grid AS $rowkey => $row)
{
echo "<tr>\n";
foreach($grid[$rowkey] AS $columnKey => $field)
{
echo "<td>{$field}</td>\n";
}
echo "</tr>\n";
}
?>