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>";
Related
<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $arr1 => $value) {
echo '<td>';
echo $value;
echo '</td>';
if (key($value)==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
This is code I am using but the key function is not returning anything.
I want to get return the position of $value and want to excecute some code if it is divisible by 6.
How can I get the position of $value in $arr1?
You must use a different variable than $arr1 to store the key.
Use this instead, where $key will be the key:
<?php
$arr1 = preg_split("/[;]+/", $data);
foreach ($arr1 as $key => $value) {
echo '<td>';
echo $value;
echo '</td>';
if ($key==6)
{
echo '</tr>';
echo '<tr>';
}
}
?>
You can following example to get position of particular $value in array.
<?php
$value = 'green';
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key_of_particular_value = array_search($value, array);
//After perform above operation $key_of_particular_value = 2
//Below statement will check whether key is divided by 6 or not
if(($key_of_particular_value % 6) == 0)
{
//perform your operation
}
?>
To know more please refer following link-
http://php.net/manual/en/function.array-search.php
You have used same variable $arr1 for key and original array, try using another variable to get key(index)
foreach ($arr1 as $key => $value) {
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 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>;
I am trying to display the array data in a specific way in a table, so I am needing to format my array like this:
Leafy Life - Aspley - All Green
Callistemon1 - 33 - -
Callistemon2 - - - 3.59
Acronychia - - 22.5 -
I have tried many times, but my knowledge is limited and would really appreciate it if anyone can point me in the right direction, or even if you have time to adjust my code that would be very much appreciated.
<?php
$options = array(
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "A Leafy Life","price" => "33"),
array("plant" => "Callistemon Kings Park Special 300 mm","nursery" => "Alpine Nurseries Alstonville","price" => "23"),
array("plant" => "Callistemon Kings Park Special 140 mm","nursery" => "All Green Nursery","price" => "3.59"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "Aspley Nursery","price" => "22.5"),
array("plant" => "Acronychia imperforata 300 mm","nursery" => "All Green Nursery","price" => "23"),
array("plant" => "Metrosideros collina Little Dugald 140 mm","nursery" => "Accent Plants","price" => "5.25"),
);
$newOptions = array();
foreach ($options as $option) {
$plant = $option['plant'];
$nursery = $option['nursery'];
$price = $option['price'];
$newOptions[$plant][$nursery] = $price;
}
print_r($newOptions);
echo "<table cellpadding='0' cellspacing='0' border='2'>";
echo "<thead>";
echo "<th></th>";
foreach($newOptions as $key => $row)
{
foreach($row as $k => $v)
{
print_r($k);
echo "<th>";
echo $k;
echo "</th>";
}
}
echo "</thead>";
echo "<tbody>";
$i=0;
foreach($newOptions as $keys => $rows)
{
echo "<tr>";
echo "<td>";
echo $keys;
echo "</td>";
foreach($rows as $v)
{
echo "<td>";
echo " ". $i;
echo "</td>";
$i++;
echo "<td>";
echo $v;
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
We can change structure of the array like this -
$Headers = array();
$headers[1] = "Leafy Life";
$headers[2] = "Aspley";
$headers[3] = "All Green";
$firstCloumnEntries = array();
$firstCloumnEntries[1] = "Callistemon1";
$firstCloumnEntries[2] = "Callistemon2";
$firstCloumnEntries[3] = "Acronychia";
$values = array();
$values[1] = array(); // this will have all values for first row;
$values[1][1] = "33";
$values[2] = array(); // this will have all values for second row;
$values[2][3] = "3.59";
$values[1] = array(); // this will have all values for third row;
$values[3][2] = "22.5";
Now while displaying - first add everything from all column "header" array.
Then for each row - use "firstCloumnEntries" array to get first value using index of the loop.
Write an inner loop to print corresponding row values. Before printing that value make use of isset().
Index "0" is purposely eliminated as we do not have any value in 0th column 0th row.
Index in the header array or in firstCloumnEntries array will help us to understand ID of the element inside it, e.g 1 is ID for "Leafy Life", 2 is for "Aspley" and so on.. using this we can add more headers with new IDs and more firstCloumnEntries with more IDs.
This will help in adding dynamic content and makes looping easy.
Using PHP I echo out table rows in a loop like this:
<?php
/* SQL STUFF */
while ($row = mysql_fetch_array($select_courseelements)) {
echo "<tr>\n";
echo "<td>".$row['scpe_name']."</td>\n";
echo "<td>".$row['scpe_days']."</td>\n";
echo "</tr>\n";
}
Now I would like to include a <select> element with 5 predefined <option> values inside a <td> running with the loop. The option values will be 1 to 5.
There is also a column inside the $row loop that holds a value of 1 to 5 ($row['scpe_grades_status']).
Each time this value is equal to the one in the <select> I want it to change it to selected='selected'.
Would this be possible?
My <select> will look something like this when it's being run in the loop:
echo "<td>\n";
echo "<select id='elements_grade'>\n";
echo "<option value='1'>Registrerad</option>\n";
echo "<option value='2'>Ej påbörjad</option>\n";
echo "<option value='3'>Pågående</option>\n";
echo "<option value='4'>Godkänd</option>\n";
echo "<option value='5'>Deltagit</option>\n";
echo "<option value='6'>Ej deltagit</option>\n";
echo "</select>\n";
echo "</td>\n";
Sure, build the values from a loop. and you can compare the values from that part.
for($i = 1; $i<=5; $i++) {
echo "<option value='$i'";
echo ($row['scpe_grades_status'] == $i) ? " selected='selected'": "";
echo ">...."</option>"
}
$array = array('Registrerad' => 1, 'Ej påbörjad' => 2, 'Pågående' => 3, 'Godkänd' => 4, 'Deltagit' => 5, 'Ej deltagit' => 6);
foreach ($array as $key=>$value) {
if ($value == $row['scpe_grades_status'])
echo '<option value="'.$value.'" selected>'.$key.'</option>';
else
echo '<option value="'.$value.'">'.$key.'</option>';
}
Something like that?