I am trying to display a list of all entries in the table people. I have the list of entries generating correctly as a html table but I want to ad a row at the top of the table with the table field names. I could do this manually but figure there must be a way to do it dynamically in case I add or remove fields later on.
here is the controller function
public function peopleDisplay() {
$this->set('people', $this->people->find('all'));
}
Are the field names already in the array that generates? If so how do I reference them? If not how do I get them to be?
Here is the view so far
<table>
<tr>
***were I want the row of field names to go***
</tr>
<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>
<?php endforeach; ?>
</table>
The column names are the keys in your array. For instance, 'id', 'firstName', and 'secondName' are the column names.
Another way to approach would be to get the column names as a separate array in your controller and then output them in your view.
$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));
Then in your view:
foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }
Related
Working on 2 tables (Homework & Deliveries). I am able to fetch the value of "homework_code" from Deliveries Table. Now same field is there in the Homework table as well without primary key.
I am trying to fetch the value of "title" field from a table "homework". both tables has common value of student_id. The code is :
<?php
$invoices = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
foreach($invoices as $row2): ?>
<tr>
<td>
<?php echo $row2['homework_code'];?>
</td>
<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?> <?php echo $row2['title'];?>
</td>
</tr>
<?php endforeach;?>
From the Your Question above, if Your Database Structure is like that then use:-
Homework Table :-
title
student_id
Deliveries Table :-
homework_code
student_id
then Change this:-
<td>
<?php echo $this->db->get_where('homework' , array('homework_code'=>'class_id'))->row()->title; ?> <?php echo $row2['title'];?>
</td>
to this way:-
<td>
<?php
$student_id = $row2['student_id'];
$homework = $this->db->get_where('homework',array('student_id'=>$student_id))->result_array();
echo $homework['title'];
?>
</td>
Based by your code above, you can use Controller like this
public function get()
{
$data = $this->db->get_where('deliveries', array('student_id' => $row['student_id']))->result_array();
$fetch['fetchdata'] = $this->db->get_where('homework', array($data[0]['homework_code'] => 'class_id'))->result_array();
$this->load->view('yourview', $fetch);
}
and get the out put by putting this on View :
<?= $fetchdata[0][title] ?>
However the result_array() function will producing array for the result even it was only one though
I have 3 tables, one contains document list (id, document, url), one contains onboarding status (user_id, document_id, status) and one contains user list (id, name).
In my controller, I have declared the variables calling each one of the repositories created with Doctrine.
$this->data['document_collection'] = $this->em->getRepository('Entities\Documents')->findAll();
$current_user = $this->session->user_id;
$this->data['onboarding_data'] = $this->em->getRepository('Entities\Onboarding')->findAll();
Then on my views page, I want to show a loop where the documents name and url list from the document list table which I did just fine like this:
<tbody>
<?php
foreach($document_collection as $onboarding):
?>
<tr>
<td>
<?php
echo $onboarding->getDocument();
?>
</td>
<td align="center">
<?php
echo anchor($onboarding->getUrl(), "Download");
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
I want to add another including checkboxes in my loop that will show checked if my $current_user matches the user_id of the onboarding status table for the corresponding document that is showing.
Example:
I am trying to echo values from mysql database into a table but some values may be empty and I don't want the cell to output empty. Is there a way for me to show a message e.g "N/A" if a particular value is empty in the database?
<table><tr><td>Friends Name:</td><td><b>".$row['first_name']." ".$row['last_name']."</b></td></tr></table>
Something like
<table>
<tr>
<td>Friends Name:</td>
<?php if($row['first_name']): ?>
<td><b>".$row['first_name']." ".$row['last_name']."</b></td>
<?php else: ?>
<td><b>N/A</b></td>
<?php endif; ?>
</tr>
</table>
I have an array of options that is used in a form's drop down select input.
DATA.php file that holds the array info...
'options' => array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
Edit page that displays the form and calls the options for the drop down input..
echo '<option '.$selected.' value="'.$optionValue.'">'.$optionLabel.'</option>';
when the form is saved the array option position value is saved to my database. I have a table on another page that echos some of the information the user submitted. But because the information is being pulled from the database as is, the information shows up as either, 0, 1, 2, 3, etc. which is the arrays position value (example: 0=car, 1=Suv, 2=pickup, etc.)
this displays the table...
<tr>
<td><span class="nobr"><?php echo $_sublogin->getData('vehicle_type') ?>
</span></td>
</tr>
How do i display the information back, so the table knows if the value is 1 echo SUV, etc.
I'm pretty new at php and i feel this is something i should know how to do but i dont. please help!
thank you.
That is probably because $optionValue contains 0,1,2... Page submits value part of the dropdown list (value="'.$optionValue.'"). I think $optionLabel has the values car, SUV... etc. So what you can do is replace $optionValue with $optionLabel.
echo '<option '.$selected.' value="'.$optionLabel.'">'.$optionLabel.'</option>';
That will save the label value to the database.
if i understood your problem then you can use if/else condition for showing name against their value. like this-
<?php
if($_sublogin->getData('vehicle_type') == 1):
?>
<tr>
<td><span class="nobr"> SUB </span></td>
</tr>
<?php
elseif($_sublogin->getData('vehicle_type') == 2):
?>
<tr>
<td><span class="nobr"> CAR </span></td>
</tr>
.
.
.
.
<?php endif;?>
are you looking for this:
$arr['options']=array("Car", "SUV", "Pickup", "Van", "Bus", "Motorcycle");
echo $arr['options'][1];
//output: SUV
I Need to show three records of MySql table in three different column using php.My Query is
SELECT * FROM TABLE1 WHERE Id IN(1,2,3)
I want to show the result as here
How can i Write LOOP for it?like
while(loop condition)
{
//what will go here?
}
UPDATE: First row fields will show in first column of html table and second record fields will display in second column and so on...I am not asking only show three records
OP saying, it's not so simple. But it is.
So, you have 2 ways to do it.
First. In this case, you are loop through on the 3 columns. Fetch the first row. This put all the data into a div. Class name is column_1. Do it for the other 3. Then floating the divs to left to each other.
$i = 1;
while($row = $db->fetch_row()) {
?>
<div class="column_<?php echo $i; ?>">
<div class="picture">
<?php echo $row["image"]; ?>
</div>
<div class="description">
<?php echo $row["desc"]; ?>
</div>
... and so on...
</div>
<?php
$i++;
}
Second one, when you first collect the data about 3 rows, and then put them into a table rows by row.
<?php
while($row = $db->fetch_row()) {
$results[] = $row;
}
?>
<table>
<tr>
<td><?php echo $result[0]['image'] ?></td>
<td><?php echo $result[1]['image'] ?></td>
<td><?php echo $result[2]['image'] ?></td>
</tr>
<tr>
<td><?php echo $result[0]['desc'] ?></td>
<td><?php echo $result[1]['desc'] ?></td>
<td><?php echo $result[2]['desc'] ?></td>
</tr>
</table>
EDIT
I forgot that, there is a third solution. You can just build the table empty, and then you can update the cells with an ajax call with jQuery.
One way of looping through them is foreach()
assuming you have your results in $results array:
foreach($results as $result) {
//create your table
$result['id']; //has the item id
$result['title']; //has item title
//and so on...
}
HERE is a great tutorial on looping through mysql result sets :D (W3Schools)
Another one HERE
To provide a answer to your comment you must understand how HTML tables work...
<tr> = table row
<td> = table data
You are asking for an entire source code, and this is NOT that place, we don't do your job for you, but if you want, you will have to pay me :) and I am not sure that you agree with this :)
HERE is a good and easy to understand tutorial on HTML tables.
while ($data=mysql_fetch_array($rs)) {
}