I am writing a word press plugin using short code.
I have a array like this:
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
.......................
);
How can I use foreach loop or any ways to return (not echo) a table like this:
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>
<tr>
<td>Show $data[0]['name']</td>
<td>Show $data[0]['birth']</td>
<td>Show $data[0]['number']</td>
</tr>
<tr>
Show all keys and all values of $data same above into each <tr> tags
</tr>
</table>
Thanks a lot!
You can do something like this
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
);
$str="<table><tr>";
$str.="<th>Name</th>";
$str.="<th>Birth</th>";
$str.="<th>Number</th></tr>";
foreach ($data as $key => $value) {
# code...
$str.="<tr>";
$str.="<td>Show $data['name']</td>";
$str.="<td>Show $data['birth']</td>";
$str.="<td>Show $data['number']</td></tr>";
}
return $str;
For me the best way is to put your table inside var and create it row by row, like this :
`
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10)
);
$myTable = "
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>";
foreach($data as $key => $value)
{
$myTable .= "
<td>Show ".$value['name']."</td>
<td>Show ".$value['birth']."</td>
<td>Show ".$value['number']."</td>
</tr>";
}
$myTable.="</table>";
echo $myTable; //You can also return it
`
Related
I want to get the unique columns of each arrays inside array.
My goal is to dynamically draw the columns depend on the array data.
Example:
$arr = array(
array('name' => 'Juan Dela Cruz', 'birthday' => '1988-07-11'),
array('name' => 'Will Smith', 'birthday' => '1996-02-22', 'age' => '23'),
array('name' => 'John Smith', 'birthday' => '1992-03-21', 'age' => '26', 'gender' => '<'),
);
So the column output must be;
name, birthday, age, gender.
And the table output must be like this.
I tried array_keys and doesn't work as I want to.
array_keys($arr)
You can do this using array_keys on the fully merged array to get all the keys, then forming an empty table row with those keys and merging each row of the array with that row for output:
$arr = array(
array('name' => 'Juan Dela Cruz', 'birthday' => '1988-07-11'),
array('name' => 'Will Smith', 'birthday' => '1996-02-22', 'age' => '23'),
array('name' => 'John Smith', 'birthday' => '1992-03-21', 'age' => '26', 'gender' => 'M'),
);
$keys = array_keys(array_merge(...$arr));
// header
echo '<table><thead><tr><th>' . implode('</th><th>', $keys) . '</th></tr></thead><tbody>';
// body
// make an empty row
$empty = array_fill_keys($keys, '');
foreach ($arr as $a) {
echo '<tr><td>' . implode('</td><td>', array_merge($empty, $a)) . '</td></tr>';
}
echo '</tbody></table>';
Output:
<table>
<thead>
<tr><th>name</th><th>birthday</th><th>age</th><th>gender</th></tr>
</thead>
<tbody>
<tr><td>Juan Dela Cruz</td><td>1988-07-11</td><td></td><td></td></tr>
<tr><td>Will Smith</td><td>1996-02-22</td><td>23</td><td></td></tr>
<tr><td>John Smith</td><td>1992-03-21</td><td>26</td><td>M</td></tr>
</tbody>
</table>
Demo on 3v4l.org
I have an array like this:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test',
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
Now I show this data in HTML table like this for user 'test' :-
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</thead>
<tbody>
<tr>
<td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['amount'];
echo "<br />";
}
}
?></td><td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['user'];
echo "<br />";
}
}
?></td>
</tr>
</tbody>
But now the problem is that when I use this code it shows the amount and user as a whole instead of two different rows. How can I fix this? Any help?
You just need to move your foreach loop outside of the <tr>...</tr> structure. This should work:
<?php foreach($str as $new_str){
if($new_str['user']=="test"){
echo "<tr><td>" . $new_str['amount'] . "</td><td>" . $new_str['user'] . "</td></tr>";
}
}
?>
Output (for your data)
<tr><td>0.9</td><td>test</td></tr>
<tr><td>1.4</td><td>test</td></tr>
Your tr was not repeating. output image I hope this will help.
<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
?>
<table>
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</tr>
</thead>
<tbody>
<?php foreach($str as $new_str) {
if($new_str['user'] == "test"){
echo '<tr>';
echo '<td>'.$new_str['amount'].'</td>';
echo '<td>'.$new_str['user'].'</td>';
echo '</tr>';
}
} ?>
</tbody>
</table>
Please help with this...
I want to write a query to display sum of all marks of each student ( OR each record of a table - 1st CA + 2ND CA + EXAM SCORE = TOTAL).
I can display the marks successfully but can't fill in the 'Total'.
Please view the picture below.
View table
Please find my "mark table" and "subject table" structure below...
View table structure
Using the MVC architecture...
Here is my Model
function get_obtained_first_ca( $exam_id , $class_id , $subject_id , $student_id) {
$marks = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks as $row) {
echo $row['first_ca'];
}}
function get_obtained_second_ca( $exam_id , $class_id , $subject_id , $student_id) {
$marks2 = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks2 as $row) {
echo $row['second_ca'];
}}
function get_obtained_exam_score( $exam_id , $class_id , $subject_id , $student_id) {
$marks3 = $this->db->get_where('mark' , array(
'subject_id' => $subject_id,
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id))->result_array();
foreach ($marks3 as $row) {
echo $row['exam_score'];
}
}
Here is my autoload.php file content
$autoload['model'] = array('crud_model'); // The crud_model is my main model file.
Here is my controller
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
Here is my view
<table>
<tbody>
<thead>
<tr>
<td style="text-align: center;">Subject</td>
<td style="text-align: center;">1st CA</td>
<td style="text-align: center;">2nd CA</td>
...
<td style="text-align: center;">Total</td>
</tr>
</thead>
<?php
$total_marks = 0;
$total_grade_point = 0;
$subjects = $this->db->get_where('subject' , array(
'class_id' => $class_id , 'year' => $running_year
))->result_array();
foreach ($subjects as $row3):
?>
<tr>
<td style="text-align: center;"><?php echo $row3['name'];?></td>
<td style="text-align: center;">
<?php
$obtained_mark_query = $this->db->get_where('mark' , array(
'subject_id' => $row3['subject_id'],
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id ,
'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
));
if($obtained_mark_query->num_rows() > 0){
$marks = $obtained_mark_query->result_array();
foreach ($marks as $row4) {
echo $row4['first_ca'];
$total_marks += $row4['first_ca'];
}
}
?>
</td>
<td style="text-align: center;"><?php
$obtained_mark_query = $this->db->get_where('mark' , array(
'subject_id' => $row3['subject_id'],
'exam_id' => $exam_id,
'class_id' => $class_id,
'student_id' => $student_id ,
'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
));
if($obtained_mark_query->num_rows() > 0){
$marks = $obtained_mark_query->result_array();
foreach ($marks as $row4) {
echo $row4['second_ca'];
$total_marks += $row4['second_ca'];
}
}
?></td>
</td>
<td style="text-align: center;">
<?php
echo $total_mark; // Here is where I need to fetch my Total
?>
</tr>
<?php endforeach;?>
Your effort to help me fill the total column is highly appreciated. Thanks in advance!
I am attempting to print out a table of the PHP multi-dimensional associative arrays. There are arrays for the class, assignments, students, scores.
I am familiar with MySQL queries, but I am not sure how to print out the table from a PHP multi-dimensional associative arrays. My thought process to access the score for the students' assignments for the class is similar to MySQL, but I know that doesn't work here. For a one dimensional, seems simple enough, but a multi-dimensional nested associative arrays I am not sure how to approach this?
Thanks in advance!
<?php
ini_set('display_errors', 'on');
$class=array (
'cat' =>
array (
2 =>
array (
'num' => '3',
'name' => 'Homework',
),
),
'assignments' =>
4 =>
array (
'clid' => '5000001001388',
'assnid' => '1',
'cat' => '3',
'due' => '20100802',
'points' => '5',
'title' => 'American Revolution',
),
),
'students' =>
array (
3 =>
array (
'stuid' => '460798', // stuid is the student's unique alphanumberic ID string
'num' => '4',
'first' => 'Thomas',
'last' => 'Jefferson',
'grade' => 'A', // these are summary statistics for the student for the class
'percent' => '94.7', // these are summary statistics for the student for the class
),
),
'scores' =>
array (
0 =>
array (
'assnid' => '1', // corresponds to assignment's 'assnid'
'stuid' => '460798', // corresponds to student's 'stuid'
'score' => '0', // this is the student's score
),
),
);
// display class properties
print($class["clid"]."<br>");
// display all class properties
foreach ($class["clid"] == $class["assignments"].["clid"] == $class["students"].["assnid"] as $property=>$value) {
print($property . " is " . $value . "<br>");
}
?>
So I used foreach to loop through students, assignments, and scores. I don't know if there's a better way of doing it.
echo <<<END
<body>
<table>
<thead>
<tr><th colspan="5" id="title">US History 2012</th></tr>
<tr>
<th>Students</th>
<th>ID</th>
<th>Grade</th>
<th>Percentage</th>
<th>American Revolution</th>
</tr>
</thead>
<tbody>
END;
foreach ($class["students"] as $students){
echo '<tr class="items">';
echo '<td>'.$students["first"].' '.$students["last"].'</td>';
echo '<td>'.$students["stuid"].'</td>';
echo '<td class="grade">'.$students["grade"].'</td>';
echo '<td class="perc">'.$students["percent"].'%</td>';
$i = 0;
$score4Avg = 0;
foreach ($class["assignments"] as $assignments){
foreach ($class["scores"] as $scores){
if ($scores["stuid"] == $students["stuid"] &&
$scores["assnid"] == $assignments["assnid"]){
echo '<td><input type="text" class="score'.$i.'" value="'.$scores["score"].'" onblur="recalculate();" tabindex="-1"></td>';
$i++;
}
if ($scores["assnid"] == $assignments["assnid"] &&
$assignments["title"] == "American Revolution"){
$score4Avg += $scores["score"];
}
}
}
echo '</tr>';
}
i have an array like that, i want to print out it in view file, but it's empty, array is like that
array(
(int) 0 => array(
'ProductsUserNode' => array(
'product_user_node_id' => '155',
'user_node_id' => '53',
'product_id' => '1',
'is_active' => '1',
'expiry' => '0000-00-00',
'created' => '2013-01-10 10:27:22',
'modified' => '2013-01-10 10:27:22',
'created_view' => '10:27 AM, Jan 10,2013',
'modified_view' => '10:27 AM, Jan 10,2013'
),
'UserNode' => array(
'user_node_id' => '53',
'division_id' => '28',
'role_id' => '4',
'user_id' => '56',
'created' => '2013-01-10 10:27:20',
'created_view' => '10:27 AM, Jan 10,2013'
),
'Product' => array(
'product_id' => '1',
'name' => 'Manager',
)
),
i am using this code in view file
foreach ($products as $products)
{
?>
<tr>
<td> <?php $products['ProductsUserNode']['product_id']?> </td>
<td> <?php $products['Product']['name']?> </td>
</tr>
<?php }?>
i have also set the variable in controllerlike that,
$this->set('products',$products);
but it is not working, what's the problem? Thanks in advance
Use the echo keyword to print your variables.
foreach ($products as $product)
{
?>
<tr>
<td> <?php echo $product['ProductsUserNode']['product_id']?> </td>
<td> <?php echo $product['Product']['name']?> </td>
</tr>
<?php }?>
foreach ($products as $products) // wrong
You need to iterate over it properly (basic php!):
<?php foreach ($products as $product) { ?>
<tr>
<td> <?php echo h($product['ProductsUserNode']['product_id']); ?> </td>
<td> <?php echo h($product['Product']['name']); ?> </td>
</tr>
<?php } ?>
Also note the h() to secure the view.
PS: You should "bake" your code. This way you would learn from it how do to it properly.