Codeigniter double rows result when using mysql join in model - php

I'm working on codeigniter web app and I was doing some JOIN on model to show specific data from 3 tables but it gives me double row which should be only 1 row per data. It didn't happen when I only have 1 row data in my database but when I input the second data, it showed me 2 rows per data so I have 4 rows
The table name is siswa and I want to JOIN another table
This is the model for JOIN the database
function delete_siswa($id)
{
return $this->db->delete('siswa',array('id'=>$id));
}
function get_data_siswa()
{
$this->db->select('sw.id, sw.nama, sw.j_kelamin, sw.tmp_lahir, sw.tgl_lahir, sw.agama, sw.alamat, sw.no_tlp, sw.email, ot.n_ibu, ot.n_ayah, sk.nama_sek');
$this->db->from('siswa sw');
$this->db->join('orang_tua ot', 'ot.id=sw.id_orang_tua');
$this->db->join('sekolah sk', 'sk.id=sw.id_sekolah');
return $this->db->get('siswa')->result_array();
}
This is the controllers
function index()
{
$data['siswa'] = $this->Siswa_model->get_data_siswa();
$data['_view'] = 'siswa/index';
$this->load->view('layouts/main',$data);
}
This is the view
<tr>
<th>No</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Agama</th>
<th>Alamat</th>
<th>No. Telepon</th>
<th>Ibu</th>
<th>Ayah</th>
<th>Email</th>
<th>Nama Sekolah</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
foreach($siswa as $s){ ?>
<tr>
<td><?php echo $no++;?></td>
<td><?php echo $s['nama']; ?></td>
<td><?php echo $s['j_kelamin']; ?></td>
<td><?php echo $s['tmp_lahir']; ?></td>
<td><?php echo $s['tgl_lahir']; ?></td>
<td><?php echo $s['agama']; ?></td>
<td><?php echo $s['alamat']; ?></td>
<td><?php echo $s['no_tlp']; ?></td>
<td><?php echo $s['n_ibu']; ?></td>
<td><?php echo $s['n_ayah']; ?></td>
<td><?php echo $s['email']; ?></td>
<td><?php echo $s['nama_sek']; ?></td>
<td>
<a class="btn btn-warning" href="<?php echo site_url('siswa/edit/'.$s['id']); ?>">Edit</a> |
<a class="btn btn-danger" href="<?php echo site_url('siswa/remove/'.$s['id']); ?>">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
This is the screenshoot of this error
It should be only one row per data but I don't really know because I'm still new for codeigniter. No error in the log

can you take the distinct record from the table, i think the join query will duplicate the data
$this->db->distinct();
will work for this

Related

show multiple rows for each result value

I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>

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>

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