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.
Related
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 have multidimensional arrays with which I need to make a table in html. I have a problem with matching catalog numbers with dates in the table.
I created an array (little part of it below):
array cotains date => CatalogNo => [catalogNo],[count],[date]
Array
(
[2019/07/19] => Array
(
[71156] => Array
(
[catalogNo] => 71156
[count] => 22
[date] => 2019/07/19
)
[71157] => Array
(
[catalogNo] => 71157
[count] => 21
[date] => 2019/07/19
)
[71221] => Array
(
[catalogNo] => 71221
[count] => 217
[date] => 2019/07/19
)
)
[2019/07/18] => Array
(
[71156] => Array
(
[catalogNo] => 71156
[count] => 26
[date] => 2019/07/18
)
[71157] => Array
(
[catalogNo] => 71157
[count] => 25
[date] => 2019/07/19
)
[71221] => Array
(
[catalogNo] => 71221
[count] => 281
[date] => 2019/07/19
)
[71222] => Array
(
[catalogNo] => 71221
[count] => 173
[date] => 2019/07/19
)
...
I did something like this but this is just bad. I dont know how to do it actually.
$dane_arr <- this is the whole array
<table>
<tr>
<th>CatalogNo</th>
<?php
foreach( $dane_arr as $k => $v )
{
?>
<th><?php echo $k ?></th>
<?php
}
?>
</tr>
<?php
foreach( $dane_arr as $k_date => $date_ )
{
foreach( $date_ as $k_nrArt_ => $nrArt_ )
{ ?>
<tr>
<td><?php echo $nrArt_['catalogNo'] ?></td>
<td><?php echo $nrArt_['count'] ?></td>
</tr>
<?php
}
}
?>
`
There are duplicates in CatalogNo and of course dates doesn't fit.
I want to create table, something like that:
CatalogNo | 2019/07/18 | 2019/07/19 | ...
71156 26 22 ...
71157 25 21 ...
71221 281 217 ...
71222 173 0 ...
(Of course without duplicates in 'CatalogNo')
You would first need to rearrange your array. Then you get all dates, and create the table:
<?php
$catalogUsage = [];
foreach ($dane_arr as $date => $catalogs) {
foreach( $catalogs as $catalogNo => $catalog) {
$catalogUsage[$catalogNo][$date] = $catalog['count'];
}
}
$allDates = array_keys($dane_arr);
?>
<table>
<tr>
<th>CatalogNo</th>
<?php
foreach ($allDates as $date) {
echo "<th>$date</th>"
}
?>
</tr>
<?php
foreach($catalogUsage as $catalogNo => $counts)
{
echo "<tr><td>$catalogNo</td>";
foreach ($allDates as $date) {
echo "<td>" . (isset($counts[$date]) ? $counts[$date] : 0) . "</td>";
}
echo "</tr>";
}
?>
</table>
Code is untested, since I haven't got the original array, so bugs might exist. I just hope this gives you some new inspiration.
I generated an array within my function using this php code (part):
while (!$combined->EOF) {
$orders_id = $combined->fields['orders_id'];
$customers_name = $combined->fields['customers_name'];
$products_name = $combined->fields['products_name'];
$products_model = $combined->fields['products_model'];
$products_quantity = $combined->fields['products_quantity'];
$this_order_data = array(
'orders_id' => $orders_id,
'name' => $products_name,
'model' => $products_model,
'quantity' => $products_quantity,
);
if (isset($this->timeframe[$id]['combined'][$oID])) {
$this->timeframe[$id]['combined'][$oID]['products'][] = $this_order_data;
} else {
$this->timeframe[$id]['combined'][$oID]['order_id'] = $oID;
$this->timeframe[$id]['combined'][$oID]['customer_name'] = $customers_name;
$this->timeframe[$id]['combined'][$oID]['products'][] = $this_order_data;
}
$combined->MoveNext();
}
This gives an array like the following example:
[6] => Array
(
[order_id] => 1910
[customer_name] => Customer A
[products] => Array
(
[0] => Array
(
[orders_id] => 1910
[name] => Product 1
[model] => P1
[quantity] => 31
)
[1] => Array
(
[orders_id] => 1910
[name] => Product 2
[model] => P2
[quantity] => 50
)
[2] => Array
(
[orders_id] => 1910
[name] => Product 3
[model] => P3
[quantity] => 20
)
)
)
[7] => Array
(
[order_id] => 1911
[customer_name] => Customer B
[products] => Array
(
[0] => Array
(
[orders_id] => 1911
[name] => Product 2
[model] => P2
[quantity] => 75
)
[1] => Array
(
[orders_id] => 1911
[name] => Product 4
[model] => P4
[quantity] => 30
)
)
)
I then use this code to output the code for display on screen
<?php
$i = 0;
foreach($timeframe['combined'] as $key => $c_data) if ($c_data['order_id'] == $c_data['products'][$i]['orders_id']){
$items = count($c_data['products']);
?>
<tr class="lineItemRow" <?php echo $rollover; ?>>
<td class="lineItemContent" align="left"><?php echo $c_data['products'][$i]['name']; ?></td>
<td class="lineItemContent" align="left"><?php echo $c_data['products'][$i]['model']; ?></td>
<td class="lineItemContent" align="right"><?php echo $c_data['products'][$i]['quantity']; ?></td>
<td class="lineItemContent" align="right"><?php echo $c_data['products'][$i]['count']; ?></td>
</tr>
<?php
if ($i == $items){
$i= 0;
}else{
$i=$i+1;
}
}
Because I don't know how many products are going to be in each array, I assumed that I needed to get the count of $c_data['products'], set $i to 0 before the loop starts, and then increment it each time until it reached the value of the count.
Only it doesn't work like that. What I've noticed is that $i is only incremented each time it moves to a new order, not each new product. This obviously gives me incorrect data because $i is not looking at the correct part of the array each time.
How should I correctly loop through each item in the 'products' array?
Why you don't loop into $c_data['products'] ?
<?php
foreach($timeframe['combined'] as $key => $c_data)
foreach($c_data as $data)
foreach($data['products'] as $product)
if ($data['order_id'] == $product['orders_id']) { ?>
<tr class="lineItemRow" <?php echo $rollover; ?>>
<td class="lineItemContent" align="left"><?php echo
$product['name']; ?></td>
<td class="lineItemContent" align="left"><?php echo
$product['model']; ?></td>
<td class="lineItemContent" align="right"><?php echo
$product['quantity']; ?></td>
<td class="lineItemContent" align="right"><?php echo
$product['count']; ?></td>
</tr>
<?php } ?>
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 )