Wrong output with an addition table in php - php

I am having trouble creating an addition table for my php program. I created my multiplication table just fine (it creates the right output), but when I try and make an addition one the numbers arent inside the table and I cant figure out what I am doing wrong. I will post what my output looks like when I create an addition table, and what it is supposed to look like and also the code that I have wrote. I feel like I am very close to completing this program, but right now I am stuck, thanks for the help in advance.
Here is the output that I am supposed to get for a 4x5 addition table.
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Here is my output .
11112222333344445555
0 1 2 3 4
1
2
3
4
5
And here is my code. Any help is appreciated.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++){
echo '<td>' .$c*$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else if (isset($_POST) && $_POST["operation"] == "addition")
{
echo 'This is a '. $_POST["rows"]. ' x ' . $_POST["columns"] .' addition table';
echo "<table border = 1";
echo'<tr>';
for($a = 0; $a <= $_POST["columns"];$a++){
echo '<th>'.$a.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo '<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++)
{
echo '<td>' .$c+$r. '</td>';
}
echo '</tr>';
}
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>

try this.
Note: i only changed elseif statement which you used for additioin, so you can just copy and paste this as i changes nothing else.
so i divided the solution in three part and leave comment in bold for you to know which part i changed. thanks.
and post the comment what should you do next. In case it did not give you desired out come.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
<tr><td>Operation:</td><td><input type="radio" name="operation" value="multiplication" checked="yes">Multiplication</input><br/>
<input type="radio" name="operation" value="addition">Addition</input>
</td></tr>
</tr><td colspan="2" align="center"><input type="submit" name="submit" value="Generate" /></td></tr>
</table>
</form>
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numberic
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
if(isset($_POST) && $_POST["operation"] == "multiplication"){
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
// for($c = 1; $c <= $_POST["columns"]; $c++){
// echo '<td>' .$c*$r. '</td>';
//}
echo '</tr>';
}
echo "</table>";
}
From here i change the code In case it does not give you desired outcomes. you only have to change for loop part just change $_post['rows'] to $_post['columns'] in the first foreach loop. And in the second for loop chnage $_post['columns'] to $_post['rows']. Thanks
else if (isset($_POST) && $_POST["operation"] == "addition")
{
for($x=0; $x<$_POST['rows']+1; $x++)
{
$i=0;
$y= $i+$x;
echo '<table border 1px solid black>';
echo '<tr>';
//$value[]=$x;
echo '<td>'.$y.'</td>';
for($value=1;$value<$_POST['columns']+1;$value++){
$c=$y+$value;
// $c=$y+$value;
echo '<td>'.$c;
echo '</td>';
}
echo '</tr>';
echo '</table>';
}
My changes finished Every thing is your code as it is.
echo "</table>";
}
else{
echo 'Invalid rows columns parameters';
exit();
}
}
exit();
}}
?>
</body>
</html>

It is actually a simple bug!
There is no error in your logic. The bug is in your echo statement for addition.
You just can't echo the addition directly. Use a bracket to surround them instead. It will work :-)
Like this;
echo '<td>'.($c+$r).'</td>';
Please let us know if it solved your problem.
Thanks,

Related

PHP/MySQL: Show only specific columns within a loop?

I am programming a voting system where users can upload up to 20 images per project (see attached image). Currently I am displaying per Row the project ID, an image and a text (text is the same in the same project). When the user adds 20 Images to the project the text will be shown 20 times. Now I am trying to rebuild the display part (see the attached Image) - instead of 20 rows, there should only be one row per user and project, but I have no clue how to achieve this. Thank you for your help.
This is my SQL syntax:
SELECT * from wp_awa_upload WHERE wp_awa_upload.uid = '$_SESSION[id]' and stat < 3 order by parent_cat, id asc
And this is the Part where I print the result of the table:
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
<? $i = 0; ?>
<? while ($show = mysql_fetch_array($abfrage)) { ?>
<form action="" method="POST">
<td><? echo $show['parent_cat'];?><br><input name="uid" type="#hidden" class="style13" value="<?php echo $show['uid']; ?>" id="uid" style="width: 40px" readonly /></td>
<td align="center"><? echo $show['file_name']; ?><br><div class="center-cropped"><img src="<? echo $show['url'] ?>" border="0"><br></div><br>©: <input name="copyright" type="text" class="style13" value="<?php if ($rolle <> '1') {echo $show['copyright'];} ?>" id="copyright" style="width: 140px" /><br><? if ( $show['youtube'] <> '') { echo 'Youtube'; }?></td>
<td><textarea name ='project' rows='16' cols='30' style="resize: none;"><?php if ($i == 0) { echo $show['project']; } ?></textarea></td>
<td><textarea name ='testimonial' rows='16' cols='30' style="resize: none;"><?php echo $show['testimonial']; ?></textarea><br>
<? if ($show['parent_cat'] == "Bester Styled Shoot") { ?>Styled Shoot Teilnehmer<br>
<textarea name ='styled' rows='8' cols='30' style="resize: none;"><?php echo $show['styled']; } ?></textarea></td>
<td><textarea name ='beschreibung' rows='16' cols='30' style="resize: none;"><?php echo $show['beschreibung']; ?></textarea></td>
<td><? if ($rolle <> 1 && $show['stat'] == 0 ) { echo '<button type="submit" value="' . $show['id']. '" name="change">Speichern?</button>'; ?><br><? echo '<button type="submit" value="' . $show['id']. '" name="delete">Löschen?</button>'; } ?></td>
<td><? if ($rolle == '1') { ?>
<select name="rating" class="style28" style="width: 120px" id="rating">
<option value="0">0</option>
<option value="5">5</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
<option value="60">60</option>
<option value="65">65</option>
<option value="70">70</option>
<option value="75">75</option>
<option value="80">80</option>
<option value="85">85</option>
<option value="90">90</option>
<option value="95">95</option>
<option value="100">100</option>
</select> <? echo '<button type="submit" value="' . $show['id']. '" name="submit">Bewerten?</button>'; }
elseif ($rolle == 9) {
echo '<button type="freigeben" value="' . $show['id']. '" name="freigeben">Freigeben?</button>';
echo '<button type="verwerfen" value="' . $show['id']. '" name="verwerfen">Verwerfen?</button><br><br>';
echo '<hr><br>'; ?>
E-Mail an Kunden schreiben:<br><textarea name ='informed' rows='8' cols='30' style="resize: none;"></textarea><br>
<? echo '<button type="informieren" value="' . $show['id']. '" name="informieren">Informieren?</button>';
}?></td>
</form>
I tried with an $i = 0; and then with a "if $i == 0 {} but, this is not working if a user has more than 1 project (the other project will not be displayed)
Thank you for your help!
Kind Regards, Stefan
I guess you want to display common text for all similar project images and id with It's related id & image.
You can do this by creating sub-array for managing image & id & main array for text description. Whenever same description received it'll break the loop & sub sequent data will be added as sub-array of main array. Try this. Hope it helps
$mainArry = array();
foreach($arrData as $item){
$idx = 0;
foreach($mainArry as $cat){
if($cat['text_description'] == $item['text_description']){
break;
}
$idx++;
}
$itemArry = array('id'=> ,'images' => );
if($idx >= 0){
$mainArry[$idx]['text_description'] = $item['text_description'];
$mainArry[$idx]['items'][] = $itemArry;
}
}
You should be doing a preprocessing of data (data massaging) before you use it for your template.
You can start by grouping rows with similar project
Example:
$processed_data = array();
foreach($rows as $row){
$processed_data[$row["project_id"][] = $row;
}
In this way, you can have something like this;
[
1 => [
[project 1, image1...]
[project 1, image2...]
[project 1, image3...]
],
2 => [
[project 2, image1...]
[project 2, image2...]
.....
]
]
Then in your template, it will be easier for you to display data with similar group
There are two methods I have mentioned below.
Please note that, these solutions are given based on following assumtions
1. You are using PHP
2. Database field names are userid, prjoject, text, image
3. You have stored imge src in the database.
Method 1:
SELECT userid, project,
GROUP_CONCAT(image separator ',') as images_list,
GROUP_CONCAT(image separator ',') as text_list
FROM your_table
GROUP BY userid,project
Above query returns data as you expected. Now you just need to populate those data into a html table.
Method 2:
Group data by user and project in your php code using a associative array.
$sql = "SELECT * FROM your_table";
Get query result to a PHP associative array. I assume the variable name is $data
$result = array();
foreach($data as $row) {
$userid = $row['userid'];
$project = $row['project'];
$result[$userid][$project]['text'][] = array('text' => $row['text'], 'image' => $row['image']);
}
HTML:
<table>
<?php foreach($result as $key_1 => $projects) { //userid holds $key_1 ?>
<?php foreach($projects as $key_2 => row ) {?>
<tr>
<td><?php echo $key_1; ?></td>
<td><?php echo $key_2; ?></td>
<td>
<table>
<tr><td><?php echo $row['text']?></td><tr>
<tr><td><img src="<?php echo $row['image']?>"></td><tr>
</table>
</td>
</tr>
<?php }?>
<?php } ?>
</table>
Following links may be helpful for more understanding.
MySql GROUP_CONCAT
PHP Associative array.
Hi thank you for your answers so far. I am nearly at the point where I could say "solved" :-)
$sql = "SELECT * from wp_awa_upload where stat = '0' order by parent_cat asc";
if (!$result = $mysqli->query($sql)) {
// Oh no! The query failed.
echo "Sorry, the website is experiencing problems.";
// Again, do not do this on a public site, but we'll show you how
// to get the error information
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
$last_entry = null;
echo "<table width='100%' border='1' align='center'><tr>";
echo "<tr>";
echo "<td align='center'>Projektnamen</td>";
echo "<td align='center'>UID</td>";
echo "<td align='center'>Kategorie</td>";
echo "<td align='center'>Projektbeschreibung</td>";
echo "<td align='center'>Beschreibung</td>";
echo "<td align='center'>Copyright</td>";
echo "<td align='center'>Tools</td>";
echo "</tr>";
while ($row = $result->fetch_object()) {
echo "<tr>";
if ($last_entry != $row->project) {
echo "<td align='center'><textarea name ='project' rows='16' cols='30' style='resize: none'>".$row->project."</textarea></td>";
echo "<td align='center'>".$row->uid."</td>";
echo "<td align='center'>".$row->parent_cat."</td>";
echo "<td align='center'><textarea name ='project_desc' rows='16' cols='30' style='resize: none'>".$row->project_desc."</textarea></td>";
echo "<td align='center'><textarea name ='beschreibung' rows='16' cols='30' style='resize: none'>".$row->beschreibung."</textarea></td>";
echo "<td align='center'>".$row->copyright."</td>";
echo "<td align='center'>Buttons</td>";
echo "</tr>";
echo "<tr>";
$last_entry = $row->project;
}
//echo "<td align='center' colspan='6'><img src=".$row->url."<border='0'> </td>";
echo "<td align='center' colspan='7'>";
echo "<a href='".$row->url."' data-lightbox='".$row->file_name."' data-title='".$row->file_name."'><br><div class='center-cropped'><img src='".$row->url."' border='0'></a></div>";
}
echo "</tr>";
echo "</table>";
The Description, Category,... will be displayed only once, the images will be displayed, but not side by side - they will be displayed with a line break.
My Question: How can I display the images side by side (4 in a row?) As I could have up to 20 Images in a project, it would be nice to show them in a pack of 4 and a max. of 5 Rows.
Thank you for you help and assistance.
Kind Regards,
Stefan

value is not appearing in the drop downlist

I have a php code that add/delete new row (tr)to the html table. One column(td) has text field and second column(td) has a drop down. Dropdown source is mysql table.
First row(tr) value are coming correctly. But dropdonwn list is null when I add new row(tr)
code is:
<html>
<head>
<title></title>
<style type="text/css">
input {
display: block; /* makes one <input> per line */
}
</style>
</head>
<?php
include 'dbconnection\dbconnection.php'; // Database connection
$count=1;
if(isset($_POST['btnadd']) == "ADD NEW ITEM") {
// add 1 to the row counter
if (isset($_POST['count'])) $count = $_POST['count'] + 1;
// set value for first load
else $count = 1;
}
if(isset($_POST['btnremove']) == "DELTE ITEM") {
// decrement the row counter
$count = $_POST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<body>
<!-- call same file on submit -->
<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<table>';
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr> <td> <input type="text" name="text'.$i.'" align="center"> </td>';
echo '<td> <select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
//if($id==#$basicgroup){
echo "<option selected value=$id>
$cdTitle
</option>";
/*}
else{echo "<option value=$id>
$cdTitle
</option>""."<BR>";;*/
}
echo '</select></td></tr> ';
}
?>
</table>
<!-- you only need one set of buttons and only one counter for this script
because every button would do the same -->
<input type="submit" name="btnadd" id="btnadd" value="ADD NEW ITEM" >
<input type="submit" name="btnremove" id="btnremove" value="DELETE ITEM">
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
</body>
</html>
After you executed MySQL query:
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
Save the results in the array like:
$categories = [];
while ($row=mysqli_fetch_array($result2)) {
$categories[$row["cmc_id"]] = $row["cmc_name"]
}
Then in your main loop:
for ($i=1; $i<=$count; $i++) {
echo ' <tr> <td> <input type="text" name="text'.$i.'" align="center"> </td>';
echo '<td> <select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
foreach ($categories as $id => $cdTitle) {
echo "<option selected value=$id>
$cdTitle
</option>";
}
echo '</select></td></tr>';
}
You current problem is on the first iteration you exhausting your MySQL query with mysqli_fetch_array and second select is empty
Change
<?php
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<table>';
for ($i=1; $i<=$count; $i++) {
echo '<tr><td><input type="text" name="text'.$i.'" align="center"></td>';
echo '<td><select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
echo "<option selected value=$id>$cdTitle</option>";
}
echo '</select></td></tr>';
}
?>
</table>
To
<?php
echo '<table>';
for ($i=1; $i<=$count; $i++) {
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
echo '<tr><td><input type="text" name="text'.$i.'" align="center"></td>';
echo '<td><select id="basicgroup'.$i.'" name="basicgroup'.$i.'" onchange=reload(this.form)>';
echo '<option value=0>Select</option>';
while ($row=mysqli_fetch_array($result2)) {
$cdTitle=$row["cmc_name"];
$id=$row["cmc_id"];
echo "<option selected value=$id>$cdTitle</option>";
}
echo '</select></td></tr>';
}
?>
</table>
Put,
$cdquery="select cmc_id,cmc_name from cat_main_category";
$result2=mysqli_query($conn,$cdquery) or die(mysqli_error());
inside for loop.

PHP(simple): Trouble with loops

I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.
<form name="calculator" action="" method="post">
From: <input class="inputbox" type="number" name="one" value="" /><br />
<p>to</p><br>
To: <input class="inputbox" type="number" name="two" value="" /><br />
<input type="submit" class="submit" name="submit" value="Get Conversions!" />
</form>
<br>
<table border='1' cellpadding='5px'>
<tr>
<th>Degrees Celsius</th>
<?php
if ($_POST['submit']) {
$one = $_POST['one'];
$two = $_POST['two'];
}
if ($two < $one) {
echo "<p>Please put the lowest number in the first input box.</p>";
} else if ($one < (-273) OR $two < (-273)) {
echo "<p> Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
} else {
$c = $one - 1;
do {
$c++;
echo "<td>" . $c . "</td>";
} while ($c < $two);
}
?>
</tr>
<tr>
<th>Degrees Fahrenheit</th>
<?php
$f = (1.8 * $c) + 32;
do {
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
</table>
Your have 2 problems in your code.
One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.
you should "reset" C after The first while loop like here:
<th>Degrees Fahrenheit</th>
<?php
$c = $one - 1;
Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this
Degrees Fahrenheit
<?php
$c = $one - 1;
do {
$f = (1.8 * $c) + 32;
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>

Making addition and multiplication tables in php.

I need help creating tables in php. Here is the code that I have right now. I have the correct html code I just need help with my php code.
<html>
<head/>
<body>
<form method="POST" action="<?= $_SERVER['PHP_SELF'] ?>" >
<table border="1">
<tr><td>Number of Rows:</td><td><input type="text" name="rows" /></td></tr>
<tr><td>Number of Columns:</td><td><select name="columns">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
</td></tr>
Operation:Multiplication
Addition
<?php
if(isset($_POST["submit"])){
//check to see if num of rows is numeric
if ( isset($_POST["rows"]) && is_numeric($_POST["rows"])){
//check to see if rows is a positive number
if($_POST["rows"] > 0){
//if the multiplication button is checked do this
if(isset($_POST) && $_POST["operation"] == "multiplication"){
//start the table and post what type of table it is
echo 'This is a '. $_POST["rows"] . ' x ' . $_POST["columns"] .'multiplication table';
echo "<table border=1";
echo'<tr>';
//post the first row
for($b = 0; $b <= $_POST["columns"];$b++){
echo '<th>'.$b.'</th>';}
echo '</tr>';
//nested for loops to finish the table
for($r = 1; $r <= $_POST["rows"]; $r++){
echo'<tr><th>'.$r.'</th>';
for($c = 1; $c <= $_POST["columns"]; $c++){
echo '<td>' .$c*$r. '</td>';
You have to run the query using a function like mysqli::query($query);. This function returns some content which you can then fetch/display with some other functions. But remember to connect to the database first and that stuff...
I'd recommend you to read some guides about mysqli first ;) Then it's quite self-explanating.

Output a php multi-dimensional array to a html table

I have a form that has 8 columns and a variable number of rows which I need to email to the client in a nicely formatted email. The form submits the needed fields as a multidimensional array. Rough example is below:
<input name="order[0][topdiameter]" type="text" id="topdiameter0" value="1" size="5" />
<input name="order[0][bottomdiameter]" type="text" id="bottomdiameter0" value="1" size="5" />
<input name="order[0][slantheight]" type="text" id="slantheight0" value="1" size="5" />
<select name="order[0][fittertype]" id="fittertype0">
<option value="harp">Harp</option>
<option value="euro">Euro</option>
<option value="bulbclip">Regular</option>
</select>
<input name="order[0][washerdrop]" type="text" id="washerdrop0" value="1" size="5" />
<select name="order[0][fabrictype]" id="fabrictype">
<option value="linen">Linen</option>
<option value="pleated">Pleated</option>
</select>
<select name="order[0][colours]" id="colours0">
<option value="beige">Beige</option>
<option value="white">White</option>
<option value="eggshell">Eggshell</option>
<option value="parchment">Parchment</option>
</select>
<input name="order[0][quantity]" type="text" id="quantity0" value="1" size="5" />
This form is formatted in a table, and rows can be added to it dynamically. What I've been unable to do is get a properly formatted table out of the array.
This is what I'm using now (grabbed from the net).
<?php
if (isset($_POST["submit"])) {
$arr= $_POST['order']
echo '<table>';
foreach($arr as $arrs)
{
echo '<tr>';
foreach($arrs as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
echo '</table>;
};
?>
This works perfectly for a single row of data. If I try submitting 2 or more rows from the form then one of the columns disappears. I'd like the table to be formatted as:
| top | Bottom | Slant | Fitter | Washer | Fabric | Colours | Quantity |
------------------------------------------------------------------------
|value| value | value | value | value | value | value | value |
with additional rows as needed. But, I can't find any examples that will generate that type of table!
It seems like this should be something fairly straightforward, but I just can't locate an example that works the way I need it too.
How about this?
$keys = array_keys($_POST['order'][0]);
echo "<table><tr><th>".implode("</th><th>", $keys)."</th></tr>";
foreach ($_POST['order'] as $order) {
if (!is_array($order))
continue;
echo "<tr><td>".implode("</td><td>", $order )."</td></tr>";
}
echo "</table>
A Table class I wrote some time ago
<?php
class Table {
protected $opentable = "\n<table cellspacing=\"0\" cellpadding=\"0\">\n";
protected $closetable = "</table>\n";
protected $openrow = "\t<tr>\n";
protected $closerow = "\t</tr>\n";
function __construct($data) {
$this->string = $this->opentable;
foreach ($data as $row) {
$this->string .= $this->buildrow($row);
}
$this->string .= $this->closetable;
}
function addfield($field, $style = "null") {
if ($style == "null") {
$html = "\t\t<td>" . $field . "</td>\n";
} else {
$html = "\t\t<td class=\"" . $style . "\">" . $field . "</td>\n";
}
return $html;
}
function buildrow($row) {
$html .= $this->openrow;
foreach ($row as $field) {
$html .= $this->addfield($field);
}
$html .= $this->closerow;
return $html;
}
function draw() {
echo $this->string;
}
}
?>
To be used like this :
<body>
<?php
$multiDimArray = []; # Turn the form array into a matrix
for ($i = 0; $i < count($_POST['order']); $i++) {
$multiDimArray[] = [];
foreach ($_POST['order'][$i] as $key=>$value) {
if ($i == 0) {
$multiDimArray[$i][] = $key;
}
$multiDimArray[$i][] = $value;
}
}
$table = new Table($multiDimArray); # Create and draw the table
$table->draw();
?>
</body>
In your HTML, try something like this:
<table>
<tr>
<th>Bottom</th>
<th>Slant</th>
<th>Fitter</th>
</tr>
<?php foreach ($_POST['order'] as $order): ?>
<tr>
<td><?php echo $order['bottomdiameter'] ?></td>
<td><?php echo $order['slantheight'] ?></td>
<td><?php echo $order['fittertype'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Obviously, I'm not including all your attributes there, but hopefully you get the idea.

Categories