Hi I'm looking for suggestions on how best to handle the submission of a database table.
So far, I've generated the repeating rows data like so:
<table border=1>
<tr>
<th>Phone Number</th>
<th>Prefix CallerID with</th>
<th>Seconds to ring before<br/>reaching voicemail</th>
<th>Voice mailbox<br/>extension number</th>
<th>Notes</th>
</tr>
<?php
$id = 1;
foreach ( $line_detail as $line ){
echo '<tr>';
echo '<td>'.form::input($id.'_did', $line->did).'</td>';
echo '<td>'.form::input($id.'_cid_prefix', $line->cid_prefix).'</td>';
echo '<td>'.form::input(array($id.'_dial_timeput', 'size'=>15, 'maxlength'=>3), $line->dial_timeout).'</td>';
echo '<td>'.form::dropdown($id.'_extension', $phones, $line->voicemail).'</td>';
echo '<td>'.form::input($id.'_notes', $line->notes).'</td>';
echo "</tr>";
$id++;
}
?>
</table>
And when I submit this I have repeating data like this:
(array) Array
(
[1_did] => 64123456789
[1_cid_prefix] => DDI-
[1_extension] => 800
[1_notes] =>
[2_did] => 645678903
[2_cid_prefix] => TEST-
[2_extension] => 820
[2_notes] =>
[submit] => Save
)
Ok, so now I can group them by id and submit an update to the database.
But, I'm wondering if there is a better way of doing this.
PHP's array notation for input names is the way to go.
<input name="data[<?php echo $id;?>]['did'] />
<input name="data[<?php echo $id;?>]['cid_prefix'] />
<input name="data[<?php echo $id;?>]['extension']"/>
...
You'll end up, once the data are posted, with a nice two-dimensional associative array.
you can build form like this:
foreach ( $line_detail as $line ){
echo '<tr>';
echo '<td>'.form::input('did['.$id.']', $line->did).'</td>';
echo '<td>'.form::input('cid_prefix['.$id.']', $line->cid_prefix).'</td>';
echo '<td>'.form::input(array('dial_timeput['.$id.']', 'size'=>15, 'maxlength'=>3), $line->dial_timeout).'</td>';
echo '<td>'.form::dropdown('extension['.$id.']', $phones, $line->voicemail).'</td>';
echo '<td>'.form::input('notes['.$id.']', $line->notes).'</td>';
echo "</tr>";
$id++;
}
it will give you the following array:
(array) Array
(
[did] => array(
[1] => 64123456789,
[2] => 645678903,
)
[cid_prefix] => array(
[1] => 'DDI-',
[2] => 'TEST-',
)
[extension] => array(
[1] => 800,
[2] => 820,
)
[notes] => array(
[1] => '',
[2] => '',
)
[submit] => Save
)
it might be a little more convinient
Related
This question already has answers here:
Pivot a mysql result set and create html table/matrix
(4 answers)
Closed 1 year ago.
I have an array that looks like this:
Array (
Array ( [date] => 04/2021 [name] => Fred [value] => 12.00 )
Array ( [date] => 04/2021 [name] => Tom [value] => 160.00 )
Array ( [date] => 04/2021 [name] => Mike [value] => 9.00 )
Array ( [date] => 07/2021 [name] => Tony [value] => 200.00 )
Array ( [date] => 07/2021 [name] => Fred [value] => 43.00 )
Array ( [date] => 07/2021 [name] => Tom [value] => 114.00 )
Array ( [date] => 07/2021 [name] => Mike [value] => 28.00 )
)
I am trying to get the data into a simple HTML table where the name goes on the x axis, the date on the y axis and the values where they intersect.
For example:
Fred Tom Mike Tony
04/2021 12.00 160.00 9.00 0
07/2021 43.00 114.00 28.00 200.00
I do not know if I am over thinking this but I cannot get this to work - I can get the x axis to work by pulling out the unique values for name but then when it comes to the y axis and the intersections I cannot get the data to display how I need.
I either end up with repetition of columns or a single column and so on. This is where I have gotten up to but still scratching my head how to do this:
<?php
$unique_names = array_unique(array_map(function($elem){
return $elem['name'];
}, $data));
?>
<thead>
<tr>
<th> </th>
<?php foreach ($unique_names as $i) { ?>
<th><?php print $i;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $row) {
for ($i=0; $i<=count($unique_names);$i++) {
if ($i==0) {
print "<tr><td>".$row['date']."<td>";
} else {
print "<td>".$row['value']."<td>";
}
if ($i==count($unique_dates)) {
print "</tr>";
}
}
}
?>
</tbody>
First, reorganize your data into an array that uses the date as key on the first level, and the name on the second. Collect the names at the same time:
$input = [
['date' => '04/2021', 'name' => 'Fred', 'value' => '12.00'],
['date' => '04/2021', 'name' => 'Tom', 'value' => '160.00'],
['date' => '04/2021', 'name' => 'Mike', 'value' => '9.00'],
['date' => '07/2021', 'name' => 'Tony', 'value' => '200.00'],
['date' => '07/2021', 'name' => 'Fred', 'value' => '43.00'],
['date' => '07/2021', 'name' => 'Tom', 'value' => '114.00'],
['date' => '07/2021', 'name' => 'Mike', 'value' => '28.00'],
];
$data = $names = [];
foreach($input as $item) {
$data[$item['date']][$item['name']] = $item['value'];
$names[] = $item['name'];
}
$names = array_unique($names);
Then create your table. Loop over the names to create the header row first.
Then loop over the reorganized data array (to create one row for each date), and inside that loop over your names again - if you find an entry matching the name in the data array, then output the value, otherwise nothing.
<table>
<tr>
<th></th>
<?php foreach($names as $name): ?>
<th><?php echo $name; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach($data as $date => $userData): ?>
<tr>
<td><?php echo $date; ?></td>
<?php foreach($names as $name): ?>
<td><?php echo $userData[$name]??'-'; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Result:
<table>
<tr>
<th></th>
<th>Fred</th>
<th>Tom</th>
<th>Mike</th>
<th>Tony</th>
</tr>
<tr>
<td>04/2021</td>
<td>12.00</td>
<td>160.00</td>
<td>9.00</td>
<td>-</td>
</tr>
<tr>
<td>07/2021</td>
<td>43.00</td>
<td>114.00</td>
<td>28.00</td>
<td>200.00</td>
</tr>
</table>
Here is your answer, great exercise.
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$data = Array (
Array ( 'date' => '04/2021' ,'name' => 'Fred' ,'value' => '12.00' ),
Array ( 'date' => '04/2021' ,'name' => 'Tom' ,'value' => '160.00' ),
Array ( 'date' => '04/2021' ,'name' => 'Mike', 'value' => '9.00' ),
Array ( 'date' => '07/2021' ,'name' => 'Tony' ,'value' => '200.00' ),
Array ( 'date' => '07/2021' ,'name' => 'Fred' ,'value' => '43.00' ),
Array ( 'date' => '07/2021' ,'name' => 'Tom' ,'value' => '114.00' ),
Array ( 'date' => '07/2021' ,'name' => 'Mike' ,'value' => '28.00' )
);
foreach ($data as $key => $value) {
$tmp[$value['name']][$value['date']] = $value;
}
$unique_names = array_unique(array_map(function($elem){
return $elem['name'];
}, $data));
$unique_dates = array_unique(array_map(function($elem){
return $elem['date'];
}, $data));
?>
<table>
<thead>
<tr>
<th>Dates</th>
<?php foreach ($unique_names as $i) { ?>
<th><?php print $i;?></th>
<?php } ?>
</tr>
</thead>
<?php
foreach ($unique_dates as $date)
{
echo "<tr>";
echo "<td>".$date."</td>";
foreach ($unique_names as $name) {
if(array_key_exists($date,$tmp[$name]))
echo "<td>".$tmp[$name][$date]['value']."</td>";
else
echo "<td>0</td>";
}
echo "</tr>";
}
?>
<tbody>
</tbody>
</table>
</body>
</html>
I have two arrays, one with quantities called aantal and one with products called producten.
Producten:
Array
(
[0] => Array
(
[0] => 2
[1] => Tegel zwart
[2] => Zwarte tegel 2x2m
[3] => 47,5
[4] => 25
)
[1] => Array
(
[0] => 4
[1] => Tegel lichtgrijs
[2] => Lichtgrijze tegel 2x2m
[3] => 40,5
[4] => 25
)
[2] => Array
(
[0] => 2
[1] => Tegel zwart
[2] => Zwarte tegel 2x2m
[3] => 47,5
[4] => 25
)
[3] => Array
(
[0] => 4
[1] => Tegel lichtgrijs
[2] => Lichtgrijze tegel 2x2m
[3] => 40,5
[4] => 25
)
)
Aantal:
Array
(
[0] => 20
[1] => 20
[2] => 27
[3] => 25
)
I want to update each quantity according to the $_GET values from the url with the following code:
$sum = 0;
foreach($_SESSION['producten'] as $key => $product){
//$_SESSION['producten'][$key][4] = '';
$number = str_replace(',', '.', $product[3]);
$sum+= $number;
if(!empty($_GET['aantal'])){
foreach($_GET['aantal'] as $keyaantal => $aantal){
$_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
}
}else{
$_SESSION['producten'][$key][4] = '1';
}
}
This is the form with the html:
<form action="cart.php">
<?php foreach ( $_SESSION['producten'] as $row){
if(!empty($_GET['aantal'])){
$aantalwaarde = $row[4];
}else{
$aantalwaarde = 1;
}
// if($row != ){
// }
?>
<tr>
<td><?php echo $row[0] // ID; ?></td>
<td><?php echo $row[1] // Product_naam; ?></td>
<td><?php echo $row[2] // Beschrijving; ?></td>
<td><input name="aantal[]" type="number" value="<?PHP echo $aantalwaarde; ?>"></td>
<td>€<?php echo $row[3] // Prijs; ?></td>
<?php $total = $total + intval($row[3]); ?>
</tr>
<?php } ?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="Updaten"></td>
<td></td>
<td>Totaalprijs</td>
<td></td>
<td>€<?php echo $sum // Prijs; ?></td>
</tr>
</form>
Why if I click the update button, it updates the last inserted values for all products? For example if I set the last quantity input at 25 and the one before it at 10, all products have a quantity for 25.
How can I fix that?
Your inner foreach loop is the problem. You're going through all the quantities instead of picking the related one. So when the loop ends you're always left with the last one being added to your session.
I can't test right now but I think you should change
foreach($_GET['aantal'] as $keyaantal => $aantal){
$_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
}
to
$_SESSION['producten'][$key][4] = $_GET['aantal'][$key];
That way, you select the matching key for both arrays.
You code has a big flaw. First, you iterate through the entire producten array. Within this array, you loop through the aantal array and update the values. That really doesn't make sense, you don't need that second loop at all. You can replace this entire part ...
foreach($_GET['aantal'] as $keyaantal => $aantal){
$_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
}
... with this ...
$_SESSION['producten'][$key][4] = $_GET['aantal'][$key];
That should do the trick.
The problem with your loop was is that you iterated through the array and updated the value of your ['producten'][$key][4] with each iteration. That means the last value you're stuck with is always the last one.
I've this column in database table:
value=[
{"srno":1,
"name":"Gaspari Aminolast ",
"quantity":"2",
"price":"2920.0000",
"total_bill":5840
},
{"srno":2,
"name":"Gaspari Amino Max ",
"quantity":"2",
"price":"2640.0000",
"total_bill":5280
},
{"srno":3,
"name":"Myofusion 10Lbs",
"quantity":"2",
"price":"8400.0000",
"total_bill":16800}
]
And my php code is:
<?php
$getbill="select value from $tbl where bill_id='$_GET[id]' ";
$result=mysql_query($getbill)or die(mysql_error());
$results = array();
while($row=mysql_fetch_array($result))
{
$results[] = json_decode($row['value']);
}
print_r($results);
foreach($results as $key=>$value){
?>
<tbody>
<tr>
<td><?php echo $value['srno'];?></td>
<td><?php echo $value['name'];?></td>
<td><?php echo $value['quantity'];?></td>
<td><?php echo $value['price'];?></td>
<td ><?php echo $value['total_bill'];?></td>
</tr>
</tbody>
<?PHP
}
?>
I'm confused with how I loop through this and print all it contains.
print_r() :
Array (
[0] => Array (
[0] => stdClass Object (
[srno] => 1
[name] => Gaspari Aminolast
[quantity] => 2
[price] => 2920.0000
[total_bill] => 5840
)
[1] => stdClass Object (
[srno] => 2
[name] => Gaspari Amino Max
[quantity] => 2
[price] => 2640.0000
[total_bill] => 5280
)
[2] => stdClass Object (
[srno] => 3
[name] => Myofusion 10Lbs
[quantity] => 2
[price] => 8400.0000
[total_bill] => 16800
)
)
)
use second argument in json_decode function,set it to TRUE to get as array
$json='[{"srno":1,"name":"Gaspari Aminolast ","quantity":"2","price":"2920.0000","total_bill":5840},{"srno":2,"name":"Gaspari Amino Max ","quantity":"2","price":"2640.0000","total_bill":5280},{"srno":3,"name":"Myofusion 10Lbs","quantity":"2","price":"8400.0000","total_bill":16800}]';
echo '<pre>';print_r(json_decode($json,TRUE));
output:
Array
(
[0] => Array
(
[srno] => 1
[name] => Gaspari Aminolast
[quantity] => 2
[price] => 2920.0000
[total_bill] => 5840
)
[1] => Array
(
[srno] => 2
[name] => Gaspari Amino Max
[quantity] => 2
[price] => 2640.0000
[total_bill] => 5280
)
[2] => Array
(
[srno] => 3
[name] => Myofusion 10Lbs
[quantity] => 2
[price] => 8400.0000
[total_bill] => 16800
)
)
The array your trying to loop over seems to be in another array.
Try this or #RiggsFolly's answer
<table>
<?php
$getbill="select value from $tbl where bill_id='$_GET[id]' ";
$result=mysql_query($getbill)or die(mysql_error());
$results = array();
while($row=mysql_fetch_array($result))
{
$results = json_decode($row['value']);
}
print_r($results);
foreach($results as $key=>$value){
?>
<tr>
<td><?php echo $value['srno'];?></td>
<td><?php echo $value['name'];?></td>
<td><?php echo $value['quantity'];?></td>
<td><?php echo $value['price'];?></td>
<td ><?php echo $value['total_bill'];?></td>
</tr>
<?PHP
}
?>
</table>
If you notice, now I reformatted the value column, each value column contains a JSON string denoting an array of objects.
So for each value you store in the $results array you neeed to do another loop to examine the inner array of objects. Like so:
<?php
$getbill="select value from $tbl where bill_id='{$_GET[id]}' ";
$result=mysql_query($getbill)or die(mysql_error());
$results = array();
while($row=mysql_fetch_array($result))
{
$results[] = json_decode($row['value']);
}
print_r($results);
echo '<tbody>';
foreach($results as $value) :
foreach( $value as $object) :
?>
<tr>
<td><?php echo $object->srno;?></td>
<td><?php echo $object->name;?></td>
<td><?php echo $object->quantity;?></td>
<td><?php echo $object->price;?></td>
<td><?php echo $object->total_bill;?></td>
</tr>
<?php
endforeach;
endforeach;
?>
</tbody>
I also moved the <tbody> and </tbody> outside the loop as that would also have caused a problem with your layout.
As I am exploring arrays and how to use it
I encounter this unusual array.
I am trying to achieve this ouput using the array I have posted below.
How can I get this done?
<table>
<tr>
<th>DOGS</th>
<th>CATS</th>
<th>BIRDS</th>
</tr>
<tr>
<td><?php echo $dogs;?></td>
<td><?php echo $cats;?></td>
<td><?php echo $birds;?></td>
</tr>
</table>
Array
(
[cats] => Array
(
[0] => persian
[1] => ragdoll
[2] => siberian
[3] => savannah
)
[dogs] => Array
(
[0] => husky
[1] => bulldog
[2] => beagle
[3] => labrador
)
[birds] => Array
(
[0] => parrot
[1] => owl
[2] => eagle
[3] => love birds
)
)
This is the output I really need to get:
DOGS CATS BIRDS
husky persian parrot
bulldog ragdoll owl
beagle siberian eagle
labrador savannah love birds
You need to loop them first, you cannot outright echo arrays:
$animals = array(
'dogs' => array('husky', 'bulldog', 'beagle', 'labrador'),
'cats' => array('persian', 'ragdoll', 'siberian', 'savannah'),
'birds' => array('parrot', 'owl', 'eagle', 'love birds'),
);
?>
<table cellpadding="10">
<tr><th>DOGS</th><th>CATS</th><th>BIRDS</th></tr>
<?php
$types = array_keys($animals);
for($i = 0; $i < count($animals['cats']); $i++) {
echo '<tr>';
foreach($types as $type) {
echo '<td>' . $animals[$type][$i] . '</td>';
}
echo '</tr>';
}
?>
</table>
Sample Output
I have an array which looks like:
Array
(
[0] => Array
(
[total words] => 1476
)
[1] => Array
(
[keyword] => difference
[count] => 82
[percent] => 5.56
)
[2] => Array
(
[keyword] => 2010
[count] => 37
[percent] => 2.51
)
[3] => Array
(
[keyword] => very
[count] => 22
[percent] => 1.49
)
)
I want to show the array content in a table of three column and three rows. Each row contains keyword, count and percent as a column and Total. Words will be shown in table caption.
Please help me ! I'm trying to run a for loop but don't know how to show the array content, because it looks like a multi dimensional array. Please help me.
This should be what you're after.
print '<table>';
$headers = array_keys(reset($array));
print '<tr>';
foreach($headers as $header){
print '<th>'.$header.'</th>';
}
print '<tr>';
foreach($array as $row){
print '<tr>';
foreach($row as $col){
print '<td>'.$col.'</td>';
}
print '</tr>';
}
print '</table>';
Implode is your friend in this case:
$arrayLength = count($myArray);
echo '<table>';
for($i=0;$i<$arrayLength;$i++){
echo '<tr><td>'.
.implode('</td><td>',$myArray[$i])
.'</td></tr>';
}
echo '</table>';
http://us2.php.net/manual/en/function.implode.php
you can iterate through your array using a foreach($array => $value) loop.
this code should do the trick:
<table>
<tr>
<th>Keyword</th>
<th>Count</th>
<th>%</th>
</tr>
<?php foreach ( $data as $row ): ?>
<tr>
<td><?php echo $row['keyword']; ?></td>
<td><?php echo $row['count']; ?></td>
<td><?php echo $row['percent']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Lets say array is stored in variable $a.
foreach($item in $a) {
echo $item['keyword'] . " " . $item['count'] . " " . $item['percent'] . "<br>\n";
}
array_values — Return all the values of an array
print_r(array_values($array));
Further to Godea Andrei's answer above, if you are using print_r just use
print_r($array)
Calling array_values within the print_r call will strip off all associative naming of the array elements. If just using numerical array indexes it will still show the index value. E.g.
$a = array("red", "green", "blue");
print_r($a) will display
Array ( [0] => red [1] => green [2] => blue )
whereas
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
print_r($b) will display
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
If using array_values the output of the second example would be
Array ( [0] => 35 [1] => 37 [2] => 43 )