i have two table which is riders and teams, and i like to show my data in my table, but the problem is both of those table have the same column name which is name, here for example :
Table riders
Table Teams
and here is the view in my table :
so what im trying to do is to join both table and make alias for the id_team, so that i can get the team name instead of their id_team, but since they had the same column name, i dont know how to do it, here is my model :
public function show()
{
$this->db->select('team.id_team, team.name, rider.*', false);
$this->db->from('teams as team');
$this->db->join('riders as rider', 'team.id_team = rider.id_team');
$query = $this->db->get();
return $query->result();
}
here is my controller :
public function index()
{
$data['riders_data'] = $this->riders->show();
$this->load->view('back-end/template/header');
$this->load->view('back-end/master-rider', $data);
$this->load->view('back-end/template/footer');
}
and here is my table in my view :
<?php
$i = 1;
foreach($riders_data as $rider) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><img src="<?=base_url('/assets/riders/'.$rider->image); ?>" alt="" width="50px"></td>
<td><?php echo $rider->name; ?></td>
<td><?php echo $rider->number; ?></td>
<td><?php echo $rider->country; ?></td>
<td><?php echo $rider->id_team; ?></td> //team name
</tr>
<?php
$i++;
}
?>
any help is really appreciated, thank you!.
Related
I am trying to get the value of how many dishes are in each order of the logged-in user, by adding up the quantity of dishes group by order.
The normal query I run which is a success by assuming logged in user's userid is 35:
SELECT o.orderid, location, status, pay, total,
SUM(quantity) AS dishnum
FROM orders o
JOIN ordersdish od
ON od.orderid = o.orderid
WHERE userid = 35
GROUP BY o.orderid;
Result:
Codeigniter code I tried to run:
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','location','status','pay','total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}
Thank you.
Update: I changed a bit my code, it can return data but only the orderid.
Code i used in view:
<?php
if($orderdata->num_rows() > 0)
{
foreach($orderdata->result() as $row)
{
?>
<tr>
<td><?php echo $row->orderid; ?></td>
<td><?php echo $row->location; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->pay; ?></td>
<td><?php echo $row->total; ?></td>
<td><?php echo $row->quantity; ?><td>
<td><button type="button" href="#" class="delete_data delButton" orderid="<?php echo $row->orderid; ?>">Delete</button></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="7" align="center">No Data Founds</td>
</tr>
<?php
}
?>
Code I used in maim.php
public function vieworderself()
{
$this->load->view('header');
$this->model->caltotal();
$data["orderdata"]=$this->model->getorderself();
$this->load->view('/orders/vieworderself', $data);
$this->load->view('footer');
}
Try using table name before every column. Eg. orders.location
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','orders.location','orders.status','orders.pay','orders.total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('orders.userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}
I have two tables "personal_trainer" and "training_plan". PersonalTrainerID is a foreign key in training_plan. I want to display that when a trainer logs in with their email and password, only the training plans that apply to that ID appears.
However, I am having trouble understanding the logic. I have coded it so that all the information from the training_plan table appears, I cannot created it such that only the rows that apply to the ID are visible to the user. I have made this by the simple sql statement "SELECT * from training_plan". There is a filter textbox to search the table that doesn't effect the code if you're wondering.
I have commented the code to try make it easier to understand. Any help would be greatly appreciated!
<?php
if (isset($_POST['search'])) /*This code allows the filter textbox to search the db */
{
$valueToSearch = $_POST['ValueToSearch'];
$query = "select * from training_plan WHERE concat('trainingPlanID', `personalTrainerID`, `clientID`, `trainingType`, `exercise1`, `exercise2`, `exercise3`, `exercise4`, `exercise5`, `exercise6`, 'reps', 'sets', 'description')like'%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * from training_plan WHERE PersonalTrainerID= (SELECT personalTrainerID FROM personal_trainer WHERE email=$_SESSION['user'])"; /*The error that is displayed is 'syntax error, unexpected string after ['*/
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost:3308","root","","fypdatabase");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<?php /*This displays the data in a table but so far outputs all of the table data */
while($row = mysqli_fetch_array($search_result))
{
?>
<tr>
<td><?php echo $row["trainingPlanID"]; ?></td>
<td><?php echo $row["personalTrainerID"]; ?></td>
<td><?php echo $row["clientID"]; ?></td>
<td><?php echo $row["trainingType"]; ?></td>
<td><?php echo $row["exercise1"]; ?></td>
<td><?php echo $row["exercise2"]; ?></td>
<td><?php echo $row["exercise3"]; ?></td>
<td><?php echo $row["exercise4"]; ?></td>
<td><?php echo $row["exercise5"]; ?></td>
<td><?php echo $row["exercise6"]; ?></td>
<td><?php echo $row["reps"]; ?></td>
<td><?php echo $row["sets"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td>
Delete
</td>
<td>
Update
</td>
</tr>
<?php
Change your query to:
$query = "SELECT * from training_plan WHERE PersonalTrainerID = (SELECT personalTrainerID FROM personal_trainer WHERE email='".$_SESSION['user']."')";
I am trying to fetch multiple images from a single columns which belongs to the same category and I want this images to be lay side by side with their category. When I fetched the images with their category the category appeared number of times the images.
Take a look at the image below, ABUA appeared once in the user table but has three images in the upload table likewise Eksu which has two images. I do not want the display to be like this, I want the images to align side by side and the details appear once.
My sql query is below:
<?php
//Education
$e = mysqli_query($mysqli, "SELECT * FROM education where fid = '$id' group by schools ");
while($edu = mysqli_fetch_array($e)){
$eid = $edu['fid'];
$sc = $edu['schools'];
$now=mysqli_query($mysqli, "Select distinct * from uploads WHERE fid='$eid' AND category = 'education' and sch='$sc' order by '$sc' ");
while($resultsn=mysqli_fetch_array($now)){
$tempr = explode(',',$resultsn['img_name'] );
foreach($tempr as $imager){
$img = "<img src='../cert/".$imager."' class='img-thumbnail' width='50' height='50'/>";
?>
<tr>
<td><?php echo $img; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php
}
}
//}
}
?>
Try this:
while($resultsn=mysqli_fetch_array($now))
{
$tempr = explode(',',$resultsn['img_name'] );
$imgs = '';
foreach($tempr as $imager)
{
$imgs .= "<img src='../cert/".$imager."' class='img-thumbnail' style='display: inline-block;' width='50' height='50'/> ";
}
?>
<tr>
<td><?php echo $imgs; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php}?>
You're creating a table row for each one the entries in your second while loop.
If you want the images side by side, you should create the table row inside your first loop and have only image <td> elements added in the second while loop.
After that loop is closed, echo the remaining `' elements inside the initial while loop to display the other fields of your table. You'll have one row with all images for that category instead of multiple rows.
Try this one:
<?php
//Education
$e = mysqli_query($mysqli, "SELECT * FROM education where fid = '$id' group by schools ");
while($edu = mysqli_fetch_array($e))
{
$eid = $edu['fid'];
$sc = $edu['schools'];
$now=mysqli_query($mysqli, "Select group_concat(img_name) as img_name from uploads WHERE fid='$eid' AND category = 'education' and sch='$sc' order by '$sc' ");
while($resultsn=mysqli_fetch_array($now))
{
$tempr = explode(',',$resultsn['img_name'] );
foreach($tempr as $imager){
$imgs += "<img src='../cert/".$imager."' class='img-thumbnail' width='50' height='50' style="margin-right:10px;"/> ";
}
}
?>
<tr>
<td><?php echo $imgs; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php
}
?>
Multiple id in mysql table how to expload and get the particular id values in another mysql table for codeigniter,
Pages view images added,
View page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_list_id ?></td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
Controller Page
`public function company_settings() {
$data['company_branch'] = $this->settings_model->company_branch();
$this->load->view('settings/company_settings',$data);`
Model Page
`function company_branch(){
$this->db->select('company_branch.*,
company_departments.department_name as departments_name ')-
>from('company_branch');
$this->db- >join('company_departments','
company_branch.departments_list_id = company_departments.id');
$query = $this->db->get();
return $query->result();
}`
How to display the `departments_list_id ` to department names in view
page,
The image is company_branch and company_department mysql table view,
browser View page,
View Page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td>
<?php
foreach(explode(",",$loop->departments_list_id) as $department)
{ $depart=$this->db->query("select department_name from company_departments where id=".$department)->row()->department_name;
echo $depart.',<br />';
}
?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
You can't enter some id into 1 field. You can add some fields to another id. Example field: departement_id_1, departement_id_2, departement_id_4, departement_id_5 etc depending on need.
you can change your sql select to this :
select branc.*,
(select dept.departement_name as departement_name_1 from company_departement as dept where branc.departement_id_1=dept.id),
(select dept.departement_name as departement_name_2 from company_departement as dept where branc.departement_id_2=dept.id),
(select dept.departement_name as departement_name_3 from company_departement as dept where branc.departement_id_2=dept.id)
from company_branc as b
then you can use script from your form
<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_name_1 ?></br>
<?=$loop->departments_name_2 ?></br>
<?=$loop->departments_name_3 ?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>
i hope this helps
i have 2 table as follows
project
id | name | address
boq
id | project_id |item_no | description
project_id is the foreign key. then i have loaded the all data from project table.then i want to load all the data in to boq table which related to project id when click on the project name. here is my code.but it doesn't work.any one can plz help me.
controller
function show_boq() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['boq'] = $this->project_list_model->show_boq($project_id);
$this->load->view('boq_doc', $data);
}
model
function show_boq($id){
$this->db->select('*');
$this->db->from('boq');
$this->db->where('project_id',$data);
$this->db->join('project', 'project.id = boq.project_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
view
<?php foreach ($boq as $boq): ?>
<tr>
<td><?php echo $boq->id; ?></td>
<td><?php echo $boq->item_no; ?></td>
<td><?php echo $boq->description; ?> </td>
</tr>
<?php endforeach; ?>
Use Model like this. Use project table as first and boq table as second.
function show_boq($data){
$this->db->select('*');
$this->db->from('project');
$this->db->join('boq', 'project.id = boq.project_id');
$this->db->where('project.id',$id);
$query = $this->db->get();
$result = $query->result();
}
And view file is
<?php
if (is_array($boq) || is_object($boq))
{
foreach ($boq as $row): ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->item_no; ?></td>
<td><?php echo $row->description; ?> </td>
</tr>
<?php endforeach;
}
?>