How to Flight_Name instead of Flight_ID? - php

Sir I have two tables.. (i-e)
(1) fare(Fare_ID, Flight_ID, Departure_Arrival_Station, Fare
(2) flights(Flight_ID, Flight_Name, No_Of_Seats.
I have foreach all data from *fare table and it gives me Flight_ID, which is correct. Now how should i will display the flight_Name instead of Flight_ID on the screen.. I try on it, this is my code. Thank you..
My Model
function getFlightName()
{
$this->db->select("flights.Flight_ID, flights.Flight_Name");
$this->db->from('flights');
$this->db->join('fare', 'fare.Flight_ID = flights.Flight_ID');
$query = $this->db->get();
return $query->result();
}
My Controller
public function fare()
{
$data['tt'] = $this->db->get('fare')->result();
$this->load->model('time_tbl_model');
$data['flt_nm'] = $this->time_tbl_model->getFlightName();
$this->load->view('front/fare', $data);
}
My View
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="center">Fare_ID</th>
<th class="center">Flight Name</th>
<th class="center">Departure_Arrival_Station</th>
<th class="center">Fare</th>
</tr>
</thead>
<tbody>
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td>
<td><?php echo $s->Flight_ID; ?></td>
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td>
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
This is output on the screen, when i executed it.
snag.gy/zLt2d.jpg

I saw your edited post and your image and I believe what you are trying to achieve is like the example of result in the end of this post.
Besides other problems, you're creating some messy stuff when using model code inside controller function. You should separate that, otherwise you're not correctly using MVC pattern.
That said, here's an example of what I would do:
Your model
public function getFlights()
{
$this->db->select('f.*, GROUP_CONCAT(fl.Flight_Name SEPARATOR "<br/>") AS `flight_names`')
->from('fare AS f')
->join('flights AS fl', 'fl.Flight_ID = f.Flight_ID', 'INNER')
->group_by('f.Flight_ID');
return $this->db->get()->result();
}
Your controller
$this->data['flights'] = $this->your_model->getFlights();
Your view
<?php foreach($flights as $flight) { ?>
<td><?php echo $flight->Fare_ID; ?></td>
<td><?php echo $flight->flight_names; ?></td>
<td><?php echo $flight->Departure_Arrival_Station; ?></td>
<td><?php echo $flight->Fare; ?></td>
<?php } ?>
This will reproduce something like:
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Fare_ID | Flight Names | Departure Arrival Station | Fare
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Flight Names

as i understood
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td> //Fare_ID
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td> //Flight Name
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach;

Related

Showing one value outside the table from joined tables in Codeigniter

I have this three tables that i joined, those tables are: attendance, employee, and division
this is my model to show the data
function lihatData()
{
$this->db->select('attendance.emp_code, attendance.emp_name,division.name_division,attendance.date,attendance.time_in, attendance.time_out');
$this->db->from('attendance');
$this->db->join('employee', 'employee.emp_code = attendance.emp_code');
$this->db->join('division', 'employee.id_division = divisiob.id_division');
$this->db->where_in('division.id_division',$this->session->userdata('id_division')
}
this is my view
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DIVISION</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
if(is_array($data)){
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo $row->name_division; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php } }?>
</tbody>
</table>
This is what i got from my code:
So what i need to change the results into something like this:
Division : Redaksi (based on user's division)
-------------------------------------------------------------------
| emp code | name | date | time in | time out |
-------------------------------------------------------------------
UPDATE: I already managed to group the division into a table, and here's the result:
As you can see, its already show datas of user's division. but i need to remove the division table, and change it into some kind of table's name or put the division's name at the top of the table based on user's name
a possible solution would be to extract data from your first array Item, something like the following should work
<?php
if (is_array($data) && count($data) > 0)
{
?>
<h1><?=$data[0]->nama_division; ?></h1>
<?php
}
?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
if(is_array($data))
{
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
function lihatData()
{
$this->db->from('attendance');
$this->db->join('employee', 'employee.emp_code = attendance.emp_code');
$this->db->join('division', 'employee.id_division = divisiob.id_division');
$this->db->where_in('division.id_division',$division);
return $this->db->get()->result();
}
try this
The key is how to group result by division. I think you can sort the result array by division, or group it as follow:
$sortedResult = [];
foreach ($data as $row) {
$sortedResult[$row->name_division] = $row;
}
//Render
<?php foreach ($sortedResult as $devision => $data) { ?>
<div><?php echo $devision; ?></div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>No</center></th>
<th><center>EMP CODE</center></th>
<th><center>NAME</center></th>
<th><center>DATE</center></th>
<th><center>TIME IN</center></th>
<th><center>TIME OUT</center></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach($data as $row){
?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $row->emp_code; ?></td>
<td><?php echo $row->emp_name; ?></td>
<td><?php echo date("d-m-Y", strtotime($row->date))?></td>
<td><?php echo $row->time_in; ?></td>
<td><?php echo $row->time_out; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php } ?>

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

My full data is not able to display on my web page from database

I wrote code to retrieve data from database and to print in form of tables on my web. I have stored 4 columns of data in my database but while retrieving it's only showing one column.
My database image
My webpage
My code:
<?php
$con = mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT name, stay, food, travel,
SUM(stay + food + travel) AS totalamount,doj
FROM placedetails ");
?>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tbody>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
</div>
Can anyone can tell where the mistake is?
And one more question: I want to diplay only the recent uploaded data on my web page. Suppose I have 4 names as mumbai, but uploaded at different times, I want to display the most recently added mumbai name on my web page
Can anyone help me out in this matter? I will be very thankful..
update your code as move tbody from php loop
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
1) <tbody> should be outside of while loop
2) if you want show only one record means no need to use while loop
3) if you want show recent record means just do descending sort by date column or id column
Query :
SELECT name, stay, food, travel, SUM(stay + food + travel) AS totalamount,doj FROM placedetails order by doj desc limit 1;
Table :
<tbody>
<?php
$row = mysqli_fetch_assoc($result); //fetch first record set only
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food']; ?></td>
<td><?php echo $row['travel']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
</tbody>

Multiple SELECT variables are not displaying

Having issues displaying all of the selected data entries inside SELECT. Right now the only one that is displaying is the recipe_direct data. Each table data should list in one row the name, ingred, direct, auth name and auth email.
<div class="starter-template">
<h1>Recipes</h1>
</div>
<?php
try
{
$sql = 'SELECT recipe_id, recipe_name, recipe_ingred, recipe_direct, author_name, author_email FROM recipes';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching recipes: ' . $e->getMessage();
}
while ($row = $result->fetch())
{
$recipes[$row['recipe_id']] = $row;
}
?>
<p>Add a Recipe</p>
<?php foreach ($recipes as $id => $recipe): ?>
<blockquote>
<table class="table table-striped">
<tr>
<td><?php echo $recipe['recipe_name']; ?></td>
<td><?php echo $recipe['recipe_ingred']; ?></td>
<td><?php echo $recipe['recipe_direct']; ?></td>
<td><?php echo $recipe['author_name']; ?></td>
<td><?php echo $recipe['author_email']; ?></td>
</tr>
<?php htmlout($recipe_html); ?> |
edit |
delete
</table>
</blockquote>
<?php endforeach; ?>
You overwrite $recipe_html each time you assign a value to it
why not just echo out the table rows:
<table>
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?= $recipe['recipe_name']; ?></td>
<td><?= $recipe['recipe_ingred']; ?></td>
<td><?= $recipe['recipe_direct']; ?></td>
<td><?= $recipe['author_name']; ?></td>
<td><?= $recipe['author_email']; ?></td>
</tr>
<?php endforeach; ?>
</table>
EdIt as #Fred -ii- states, the table tags should be outside the loop
Everything looks correct up until your foreach.
<blockquote>
<table class="table table-striped">
<?php foreach ($recipes as $id => $recipe): ?>
<tr>
<td><?php $recipe_html = $recipe['recipe_name']; ?></td>
<td><?php $recipe_html = $recipe['recipe_ingred']; ?></td>
<td><?php $recipe_html = $recipe['recipe_direct']; ?></td>
<td><?php $recipe['author_name']; ?></td>
<td><?php $recipe['author_email']; ?></td>
</tr>
<p> //seems you were missing opening <p> tag//
<?php htmlout($recipe_html); ?> |
edit | //can use shorthand here//
delete //can use shorthand here//
</p>
<?php endforeach; ?>
</table>
</blockquote>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

Categories