I have the following code. How can I get my results ($x) for the foreach function to print into the table at the top, but in columns instead of a straight horizontal row? Is there a way to do this without just inserting each individual value into the HTML table? I need to do the same for my $employee['name'] variable but am not sure how I could get these values inserted into a table format without going one by one and entering the value myself.
Also, one value for $x at the end stays an integer and does not echo the string variable specified by the foreach function, is there a way I could fix this?
<!--4.3-->
<table>
<tr>
<td>Employee name</td><td></td><td></td><td></td><td></td><td></td>
<td>Type of Paying</td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td>
<td><?php echo $x;?></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</table>
</html>
<?php
foreach ($employees as $employee) {
$x = ($employee['wage'] * $employee['hrs']) * 4;
if ( 3000 <= $x ) {
echo "High paying";
} elseif (2000 <= $x && $x <= 2999) {
echo "Good paying";
} else {
echo "Low paying";
}
}
print_r ($x);
I inserted the foreach function into the table where I needed the results printed, then added tags within the ifelse statement after each parameter, and it worked like a charm.
My code for that table now looks like this:
<html>
<table>
<tr>
<td>Employee name</td><td></td><td></td><td></td><td></td><td></td>
<td>Type of Paying</td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td>
<td> <?php
foreach ($employees as $employee) {
$x = ($employee['wage'] * $employee['hrs']) * 4;
if ( 3000 <= $x ) {
echo "High paying"; echo "<br/>";
} elseif (2000 <= $x && $x <= 2999) {
echo "Good paying"; echo "<br/>";
} else {
echo "Low paying"; echo "<br/>";
}
} echo "<br/>" ; ?>
</td><td></td><td></td><td></td><td></td><td></td>
</tr>
</table>
The other section above the foreach function will hold employee names and is not yet filled out to match.
Hi guys i am trying to display the first td values as static so i have keep those values in one array and i try to increase the values and try to display but when i get it is displaying depending on foreach values if i have one record in foreach it is displaying one value and if i have 2 records it is displaying 2 values.
But i want to display all td value if i have values in foreach or not also.
Here is my code:
<tbody>
<?php
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion',3=>'a',4=>'b',5=>'c',6=>'d',7=>'e',8=>'f',9=>'g',10=>'h');
$i = 0;
foreach($getCourse as $report)
$status= $report->status;
{
?>
<tr>
<td><?php echo $arr[$i]; ?>
<?php $i++; ?></td>
<td><?php
if($status==1)
{
echo "On Hold";
}
elseif($status==2)
{
echo "Asset Incomplete";
}
elseif($status==3)
{
echo "Yet to Start";
}
elseif($status==4)
{
echo "SME Discussion";
}
elseif($status==5)
{
echo "Development";
}
elseif($status==6)
{
echo "PB Review";
}
elseif($status==7)
{
echo "PB Fixes";
}
elseif($status==8)
{
echo "PB2 Review";
}
elseif($status==9)
{
echo "PB2 Fixes";
}
elseif($status==10)
{
echo "Alpha Development";
}
elseif($status==11)
{
echo "Alpha Review";
}
elseif($status==12)
{
echo "Alpha Fixes";
}
elseif($status==13)
{
echo "Beta Review";
}
elseif($status==14)
{
echo "Beta Fixes";
}
elseif($status==15)
{
echo "Gamma";
}
?></td>
<td><?php echo $report->coursename; ?></td>
<td><?php echo $report->statuscount;?></td>
<td></td>
</tr>
<?php
}
?>
</tbody>
Here is my controller:
public function index()
{
if ($this->session->userdata('is_logged')) {
$data['getCourse']=$this->Report_model->getTopicReports();
$this->load->view('template/header');
$this->load->view('reports/report',$data);
$this->load->view('template/footer');
}
else {
redirect("Login");
}
}
Here is my model:
public function getTopicReports()
{
$this->db->select('count(t.status) as statuscount,t.topicName as topicname,c.coursename,t.status')
->from('topics t')
->join('course c', 't.courseId = c.id')
->where('t.courseid',1)
->where('t.status',5);
$query = $this->db->get();
return $query->result();
}
This is how i want to display here is my referrence image:
Can anyone help me how to do that thanks in advance.
FINAL UPDATED & WORKING VERSION
For me this was tricky. This turned out to be what I felt to be a awkward indexing issue.
The problem is that your data is not optimized very well to accommodate the task you are asking for. You have to make odd comparisons as you traverse the table. I was able to get it accomplished.
I would actually be very interested in seeing a more refined approach. But until that happens this code will dynamically generate a table that matches the output of the sample pictures that you posted in your question.
I have tested this code with some sample data that matches the array structure that you posted in your comments.
Here is the code:
//Create an array with your status values.
$rowName = array(
'On Hold',
'Asset Incomplete',
'Yet to Start',
'SME Discussion',
'Development',
'PB Review',
'PB Fixes',
'PB2 Review',
'PB2 Fixes',
'Alpha Development',
'Alpha Review',
'Alpha Fixes',
'Beta Review',
'Beta Fixes',
'Gamma'
);
//Generate a list of class names and get rid of any duplicates.
foreach($getCourse as $report){
$courseNames[] = $report->coursename;
}
$courseNames = array_unique($courseNames);
//Create the header.
echo
'<table>
<thead>
<tr>
<th>#</th>';
foreach($courseNames as $course){
echo '<th>' . $course . '</td>';
}
echo
'<th>Total</th>
</tr>
</thead>
<tbody>';
//Iterate across the array(list) of status values.
for($i = 0; $i < count($rowName); $i++){
echo
'<tr>';
echo '<td>' . $rowName[$i] . '</td>';
//Iterate through all combinations of class names and status values.
for($j = 0; $j < count($courseNames); $j++){
//Set flags and intial values.
$found = FALSE;
$total = 0;
$value = NULL;
for($k = 0; $k < count($getCourse); $k++){
//***Note - The ""$getCourse[$k]->status - 1" is because the status values in your data do not appear
//to start with an index of 0. Had to adjust for that.
//Sum up all the values for matching status values.
if(($getCourse[$k]->status - 1) == $i){
$total += $getCourse[$k]->statuscount;
}
//Here we are checking that status row and the status value match and that the class names match.
//If they do we set some values and generate a table cell value.
if(($getCourse[$k]->status - 1) == $i && $courseNames[$j] == $getCourse[$k]->coursename){
//Set flags and values for later.
$found = TRUE;
$value = $k;
}
}
//Use flags and values to generate your data onto the table.
if($found){
echo '<td>' . $getCourse[$value]->statuscount . '</td>';
}else{
echo '<td>' . '0' . '</td>';
}
}
echo '<td>' . $total . '</td>';
echo
'</tr>';
}
echo '</tbody>
</table>';
Try this. I am storing course status in $used_status then displaying the remaining status which are not displayed in foreach.
$arr = array ( '1' =>'On Hold', '2' => 'Asset Incomplete', '3' => 'SME Discussion', '4' => 'a', '5' => 'b', '6' => 'c', '7' =>'d', '8' => 'e', '9' => 'f', '10' => 'g', '11' => 'h' );
$used_status = array();
foreach($getCourse as $report) { ?>
<tr>
<td>
<?php $course_status = isset($arr[$report->status]) ? $arr[$report->status] : "-";
echo $course_status;
$used_status[] = $course_status; ?>
</td>
<td>
<?php echo $report->coursename; ?>
</td>
<td>
<?php echo $report->statuscount; ?>
</td>
</tr>
<?php }
foreach($arr as $status) {
if(!in_array($status, $used_status)) { ?>
<tr>
<td>
<?php echo $status; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
<td>
<?php echo "-"; ?>
</td>
</tr>
<?php }
} ?>
I am trying to combine the set variable $jstep_optionx with $count where x is the number within the count as $jstep_option.$count, however the only output is $count as the number.
Example as per below:
<?
$jstep_choice = 'Choice';
$jstep_options = '2';
$jstep_option1 = 'Option1';
$jstep_option2 = 'Option2';
?>
<? if ( $jstep_type == 'Choice' ) { $count = 1; $options = $jstep_options; ?>
<table class='choice'>
<tr>
<? while ( $count <= $options ) : $choice = $jstep_option.$count; print "<td align=center>".$choice."</td>"; $count++; endwhile; ?>
</tr>
</table>
<? } ?>
Output is: a TD with 1 and a TD with 2
The output should be: a TD with Option11 and a TD with Option22
Cheers.
Bellow are my code, i get four column from these, but the result shows every one data will loop in four time in one column then it appear again in new column with the same data.
<?php
$tampil_data = $this->in_elektronik_model->tampil_data(); //load data
foreach ($tampil_data as $tampildata) {
for ($i = 0; $i < 4; $i++) {
$table->addRow(500); //make new row
for ($j = 0; $j < 4; $j++) {
$table->addCell(2000)->addText(htmlspecialchars($tampildata->elektronik_nama)); //content of table
}
}
}
model
function tampil_data() {
$this->db->where('elektronik_status_aktif', 0);
$tampil = $this->db->get('in_elektronik');
if ($tampil->num_rows() > 0) {
return $tampil->result();
} else {
return array();
}
}
How to make table with four column, where the content of table will appear from right column to left and appear just one data in one time, after four column are full, it makes new row?
You can refer this code. This is the way i have done table in view page
<table border="1">
<tr role="row">
<th width="5%">Featured Id</th>
</tr>
<?php
if(!empty($results))
{
foreach ($results as $row) {
?><tr>
<td class=" "><?php echo $row->fet_id; ?></td>
</tr>
<?php }}
?>
</table>
I wrote some code to display a html table with PHP.
It reads data from a file and checks whether the value is true or false.
The problem is the ugly appearance of the table at the end of the function:
True False
blue
Red
Red
blue
I would like to display the table like this:
True False
blue Red
blue Red
Also it displays the outcome before the function is finished c.q flush();
Here is the code:
?>
<table>
<tr>
<th>True</th>
<th>False</th>
</tr>
<?php foreach ($trimmed_array as $value) { ?>
<tr>
<?php if (check($value) == 0) { ?>
<td><?php print $value ?></td>
<td> </td>
<?php
} else { ?>
<td> </td>
<td><?php print $value ?></td>
<?php
}
sleep(1);
ob_flush();
flush();
} ?>
</table>
I realize this code outputs the table like the first example but I can`t seem to figure out how to change it to fit the second example?
Your code creates one table row per item in the array, so there will always be one blank column per row.
In the 'table' that you would like to display, the items in each row aren't related to each other.
So really you're creating 2 lists here, not a table as such.
I'd do something like this
<?php
$all_values = array('true' => array(), 'false' = array());
foreach($trimmed_array as $value){
if (check($value) == 0){
$all_values['true'][] = $value;
}else{
$all_values['false'][] = $value;
}
}
foreach($all_values as $name => $values){
?>
<ul class="<?php print $name;?>">
<?php
foreach($values as $value){
?>
<li><?php print $value; ?></li>
<?php
}
?>
</ul>
<?php
}
?>
You can then arrange your lists side by side with CSS
$true = array();
$false = array();
foreach ($trimmed_array as $value) {
if (check($value) == 0) {
$true[] = $value;
} else {
$false[] = $value;
}
}
for ($i = 0; $i < max(count($true), count($false)); $i++) {
echo '<tr>
<td>' . (isset($true[$i]) ? $true[$i] : '') . '</td>
<td>' . (isset($false[$i]) ? $false[$i] : '') . '</td>
</tr>';
}