Print multiple arrays into html table - php

I have two arrays built from an XML response to an api. One array gives me the product info form one store and the other array gives me just the stock level in another. It looks like this:
foreach($filteredStock as $t=>$k){
$codeFirst[] = $k['code'];
echo '<tr><td>'.$t;
echo '</td><td>'.$k['desc'];
echo '</td><td>'.$k['family'];
echo '</td><td>'.$_POST['filterSelect'];
echo '</td><td>'.$k['onOrder'];
echo '</td><td>'.$k['cost'];
echo '</td><td>'.$k['sell'];
echo '</td><td>'.$k['invStore'];
echo '</td>';
}
$output = array();
$result = array_intersect_key($mainArray, array_flip($codeFirst));
foreach($result as $results=>$rValues){
echo '<td>'.$rValues['inv'];
echo '</td>';
echo '</tr>';
}
I want to display it so that it looks like this in the table:
code / desc / family / filteredClass / onOrder / cost / sell / invStore / invwarehouse
Because I need the second foreach loop to grab the values of the second array it causes a problem trying to get it to repeat on each line with the other array. Any suggestions?

Rather than echoing output each time the loop iterates, why don't you build up the table how you want it and then output it when you have it arranged properly?
It's not clear to me from the code exactly what you are trying to do, but if it's an organisation problem then why don't you store the values in seperate arrays and then combine then and arrange them appropriately before outputting them?
Using echo immediately for each loop iteration limits your options somewhat.

Related

Echo Array from While once

I'm trying to output the result of an array I fill from an SQL database.
<?php
if(mysqli_num_rows($Difference))
{
while($row = mysqli_fetch_assoc($Difference))
{
$block[] = $row["time"]." | ".$row["d_bl"];
}
}?>
When I "vardump" the array, it contains all the proper values I expect.
I want to output this array now in a simple list with breaks.
When I use "return" with the array, I only get one result due the automatic escape. When I print the array, I get 150 times the array instead of only the 150 once, due the loop.
Here is the solution from #Nigel Ren
After your while loop has finished -
echo implode("<br />", $block);
(Assuming web page output - replace "<br />" with PHP_EOL if not)

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

PHP display order for iterating through multiple arrays

In essence, I have 3 arrays. These are serialised, stored to the DB, un-serialised and then outputted to a page. myFirstArray, mySecondArray, & myThirdArray.
From what I gather - I need to be using a foreach loop, or a for loop with a counter, as the 3 arrays are all of (the same) unknown length. By that I mean one user may have stored 4 items into each of the 3 arrays, but another user might have stored 8 items into each of the 3 arrays.
I'm trying to get the output to look something like this:
myFirstArray[0], mySecondArray[0], myThirdArray[0]
myFirstArray[1], mySecondArray[1], myThirdArray[1]
myFirstArray[2], mySecondArray[2], myThirdArray[2]
The current code I have is as follows:
foreach ($myFirstArray as $value1){
echo $value1 . " ";
}
foreach ($mySecondArray as $value2){
echo $value2 . " ";
}
foreach ($myThirdArray as $value3){
echo $value3 . "<br>";
}
I am aware that this code is never going to output my arrays as I would like, but I'm having some difficulty with working out the logic behind what I need. I haven't rushed straight to StackOverflow to ask, but nothing else I've seen has been very helpful!
Since both arrays have the same length, I propose
$length = count($myFirstArray);
for($i = 0; $i <$length ; $i++) {
echo $myFirstArray[$i].','.$mySecondArray[$i].','.$myThirdArray[$i].'<br/>';
}
This will loop through all of your arrays at the same time :) .

PHP Store a list into array for generate report

I have a set of data from my survey result which look like below.
Member A
Service-1
Food-1
Like it?-1
Member B
Service-3
Food-2
Member C
Service-5
Food-4
Like it?-1
I wish to generate an array to store all the data above, and generate a report.
Example:
Food 1 1 Member Choose this
2 3 Member Choose this
4 0 Member Choose this
Please help......
Except array, do I have any other way to generate this?
I'm not sure to have exactly understood the question. Supposing that
results should be shown in a table form
the second column of results is service number
the third column is a simple text (not related to members id)
you can try this
/* Create sample data (you may use json_decode) */
$memberA = array('member'=>'A', 'service'=>'1', 'food'=>'1', 'like'=>'1');
$memberB = array('member'=>'B', 'service'=>'3', 'food'=>'2', 'like'=>'');
$memberC = array('member'=>'C', 'service'=>'5', 'food'=>'4', 'like'=>'3');
$members = array($memberA, $memberB, $memberC);
/* Build new array */
$results = array();
foreach($members as $member)
{
$item = array('food'=>$member['food'], 'service'=>$member['service']);
array_push($results, $item);
}
/* Output results */
echo '<table>';
echo '<tr><td>Food</td><td>Service</td><td>Notes</td></tr>';
foreach($results as $result)
{
echo '<tr>';
echo '<td>'. $result['food'] . '</td><td>' . $result['service'] . '</td><td>Member choose this</td>';
echo '</tr>';
}
echo '</table>';
In this example I've created sample data as an array for simplicity. If you have json source, you can transform it in a PHP variable with json_decode function.
Note
If your goal is to simply output data (you don't need to use $results array again), you can print them directly in the first foreach cycle, without creating $results variable.

Need help removing duplicate columns in a mysql result

I have a website that returns results by country. There is a query that returns this, then I use a loop to output the data. The only problem is that for one of these, there is a sub-50 column and the results are the same all the way to the sub-36 column. I need help removing the duplicate columns.
// By country
// sort countries by most subXs in descending order
arsort($country_sub{$dsub});
// print results
echo "<b><span style='font-size:20px;'>By Country</span></b>";
echo "<table><tr style='font-weight:bold;'><td style='width:100px;'>Country</td><td>sub".$dsub."</td>";
for($i=$dsub-1; $i>=$x; $i--){ echo "<td>sub".$i."</td>"; }
echo "</tr>";
foreach($country_sub{$dsub} as $country => $value){
echo "<tr><td>".$country."</td><td>".$value."</td>";
for($i=$dsub-1; $i>=$x; $i--){
if (isset($country_sub{$i}[$country])) {
echo "<td>".$country_sub{$i}[$country]."</td>";
} else{
echo "<td></td>";
}
echo "</tr>";
}
echo "</table><br/>";
The array $country_sub{$dsub} contains the number of people with that result or less. The foreach loop is what outputs the top numbers, and the for loop is what outputs the results itself.
You can see where this code is used at this link: http://cubingstats.netau.net/3bld/index.php. It's used in the "By Country" section. All I want is in that table, to show sub 36 and sub 27 (the ones listed above). Any help is extremely useful!
EDIT: Lines 26-32 (see comments below for link) show the addOne function, which is what adds it to the array. This is then used on lines 48-50 and 68-70 as the results are displayed.
In your SQL query, add DISTINCT just after the SELECT
SELECT DISTINCT fields,fields...fields
FROM...

Categories