I have a multidimensional array where each key holds another array as value. I printed each key value pairs in separate tables. But I get unwanted space between tables (spacing between any two tables are not the same). How can I eliminate spacing as a whole?
foreach ($stockist as $key => $value)
{
echo "<table align='center' border='1'>";
echo "<tr><td align = 'center'> <font color = 'blue'> $key</td></tr>";
foreach($value as $key1 => $value1)
{
echo "<tr><td align ='center'>$value1</td></tr><br>";
}
echo "</table>";
}
This should get it all into a single table.
PHP
echo '<table>';
foreach ($stockist as $key => $value)
{
echo '<tr><td>'.$key.'</td></tr>';
foreach($value as $key1 => $value1)
{
echo '<tr><td>'.$value1.'</td></tr>';
}
}
echo '</table>;
Related
I was doing a simple PHP array table, but the result of my code is not what I've expected,
<?php
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo '<table border="1">';
foreach($names as $name => $indv_name) {
echo '<tr>';
echo "<th>$name</th>";
foreach($indv_name as $per_name) {
echo '<td>',$song, '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
The result of the array of this code is echoed horizontally, can anyone help me to echo it vertically?
Here's my output example:
firstname -> value, value, value
middlename -> value, value, value
lastname -> value, value, value
Output that I want to expect:
firstname - middlename - lastname
value - value - value
value - value - value
value - value - value
- value - value
- value
And whenever I add value in the array, it won't break the line.
Sorry for the late edit
A basic way
$names = array (
'First Name' =>
array('Alfre', 'Beka', 'Charlie'),
'Middle Name' =>
array('Brom', 'Tiv', 'Clore'),
'Last Name' =>
array('Adani', 'Filial', 'Chrome'),
);
echo "<table>";
echo "<thead>";
echo "<tr>";
foreach ($names as $key => $value) {
echo "<th>$key</th>";
}
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($names as $key => $value) {
echo "<tr>";
foreach ($value as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
I have a csv file that should be converted to HTML table based on column values , and I have to put the values into separate table based on first column of csv . And I think the problem is because of '\n' in first column.
So its like this:
Here in Result column, in one row I have three values separated using comma (W,M,P). In the code I wrote it is considered as separate table headers .
Can anyone please help me with this?
This is my code:
<?php
$csv="FinalResult.csv" ;
$csvcontents=file_get_contents($csv);
$csv_array = explode("\n", $csvcontents);
$tables = [];
foreach($csv_array as $key => $value) {
if ($key == 0) {
continue;
}
$line = explode(',', $value);
if (array_key_exists($line[0], $tables)) {
$tables[$line[0]][] = $line;
} else {
$tables[$line[0]] = [$line];
}
}
foreach ($tables as $key => $value) {
echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
echo "<table>";
echo '<tr>';
foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
if (in_array($keyHeader, [0, 1])) {
continue;
}
echo "<th>$valueHeader</th>";
}
echo '</tr>';
foreach ($value as $keyRow => $valueRow) {
echo '<tr>';
foreach ($valueRow as $keyValue => $valueValue) {
if (in_array($keyValue, [0, 1])) {
continue;
}
echo "<td>$valueValue</td>";
}
echo '</tr>';
}
echo '</table>';
}
?>
I refereed in stack overflow , but there they were giving only single values to a column and not providing multiple values .
But am getting output like this ,
so it takes the value of Result column after '-' as a new heading , i tried but am not able to solve this , can anyone really help me in this matter .
Here is how my output should look like:
I marked in yellow where all the data am getting in same column
This is my csv file :
Team,Date,Opponent,Result
MIN,May-03,UTA,a.b.c=d-e.f-g.h=log4j2-i.xml-j -k -a4j.k=tp_r-RR.xml -
MIN,May-04,SEA,"L,Q,J"
SAC,May-03,DAL,L
SAC,May-04,TOR,W
NYN,May-05,BAL,L
NYN,May-07,MIA,W
Here is the code
<table>
<?php
$csvValues= array_map('str_getcsv', file('FinalResult.csv'));
$counter = 0;
// Header
echo "<tr>";
foreach($csvValues[0] as $headers){
echo "<th>".$headers."</th>";
}
echo "</tr>";
// Content
foreach($csvValues as $values){
echo "<tr>";
if($counter >0){
foreach($values as $data){
echo "<td>".$data."</td>";
}
}
echo "</tr>";
$counter++;
}
?>
</table>
I'm using foreach loops to access records in a nested array:
while ($row = mysql_fetch_assoc($result)){
$test_groups[$row['group_name']][] = $row['lab_test'];
}
foreach($test_groups as $group_name => $tests){
echo "<tr><td><span class='test_group_name'>" . $group_name . "</span></td></tr>";
foreach($tests as $test){
echo "<tr><td>" . $test . "</td></tr>";
}
echo "<tr><td> </td></tr>";
}
echo '</table>';
This works OK. Now I need to add another level to the nested array, like:
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Would this work and how should I adjust the for each loops above to cater for this?
Assign to array should be with [] at the end
$departments[$row['department_name']][$row['group_name']][] = $row['lab_test'];
Foreach loop will be:
foreach ($departments as $dep_name => $groups) {
echo $dep_name;
foreach ($groups as $group_name => $tests) {
echo $group_name;
foreach ($tests as $test) {
echo $test;
}
}
}
It's just with echoes, make there HTML as you need.
I tried to create a table that calculating the data which stored in mutli-dimensional arrays' value and show the result by table format.
I don't know how to count the values which stored in the array's array(third level array's value.
Can I using the function to count values in the mutli-dimensional arrays' value? Hope anyone can teach me, thanks.
<?php
$itemList = array
("book"=>array("ID"=>"14567",
"name"=>array("History"=>array("American"=>12,"Europe"=>2), "SF"=>array("Space"=>32), "Chinese"=>array("kungFu"=>10))),
"stationary"=>array("ID"=>"24547", "name"=>array("HB"=>array("ABC"=>123, "MC"=>161,"GCD"=>26)))
);
$item = "<table border=1>";
$item .= "<td>item count(s)</td>";
foreach($itemList as $key => $value){
$item .= "<tr>";
$item .= "<td align=center>" .
/*count the categories of the items(e.g. American,Europe,Space,Kungfu show
the result: "4")*/
. "</td>";
$item .= "</tr>";
}
$item .= "</table>";
echo $item;
?>
foreach($itemList['book']['name'] as $key => $value)
{
foreach ($itemList['book']['name'][$key] as $key1 => $value1) {
$category[] = $key1;
$value2[] = $value1;
}
}
echo count($category);
here you got the total numebr of count of your category
I'm trying to store the values of the table in to a csv file for which I used fputcsv function. However it puts the elements into a single column. I need to store the values in three columns.
Code:
foreach ($total as $key => $value)
{
foreach ($final as $key1 => $value1)
{
if($value == $value1)
{
$total[$key] = null;
break;
}
}
}
foreach($total as $key =>$value)
{
if(($total[$key] != null) && (!preg_match("/^bb_demo_/",$total[$key])))
{
array_push($check, $value,$ext1[$key], $ext2[$key]);
echo "<table border='1' width='400px' cellpadding='1' cellspacing='1'>";
echo "<tr><td width='100px' align='center'><strong>$value</strong></td>";
echo "<td width='100px' align='center'><b>";
echo $key+1;
echo "</td>";
echo "<td width='100px' align='center'>";
echo $ext1[$key];
echo "</td>";
echo "<td width='100px' align='center'>";
echo $ext2[$key];
echo "</td>";
}
}
I tried to get values from a csv file perfom certain operations on it and displayed them as a table in html. Now I want these values stored in html table to be stored in a csv file So I created a new array $check and used the following code:
$file = fopen("contacts.csv","w");
foreach($check as $values)
{
fputcsv($file,preg_split('//',$values));
}
fclose($file);
which stores values into one column
Im thinking whether is there a way that I separate the first three elements separated by commas using 'preg_split' i.e. using preg_split('/,,,/)?
I know the above expression is incorrect
But I don't have a clue of doing it? Could someone help me with this? Thanks in advance
I considered three different loops to store the values of $value, e used a for loop as follows
Code for($i=0; $i < sizeof($check); $i++) {
fputcsv($file, array($check[$i], $check1[$i], $check2[$i])) ; }
fclose($file) ;
It works!