So I have 3 tables I wish to join.
I am building an app i Codeigniter and I have 3 tables
Client:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id
Hospital
-id
-name
Testing_center
-id
-name
In the model,I have this:
public function get_clients()
{
if($slug === FALSE)
{
$this->db->select('clients.*');
$this->db->from('clients');
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('clients');
return $query->row_array();
}
In the view I have:
<tbody>
<?php foreach ($clients as $client_item): ?>
<tr>
<td><?php echo $client_item['phone_number'] ?></td>
<td><?php echo $client_item['smc_status'] ?></td>
<td><?php echo $client_item['hospital_id'] ?></td> //i wish to have the hospital name here
<td><?php echo $client_item['testing_center_id'] ?></td> //i wish to have the testing center name here
<td><?php echo $client_item['language'] ?></td>
<td>View</td>
</tr>
<?php endforeach ?>
</tbody>
But that is because I have failed to show the hospital name and the testing center name on the third and fourth td. How can I go about that? I tried a few techniques that just did not seem to work for some reason. Please advise
You're only selecting the values from the clients table. You need to select the columns from the other tables as well
$this->db->select('clients.id,
clients.phone_number,
clients.smc_status,
clients.language,
hospital.name AS hospital_name,
testing_center.name AS testing_center_name');
Then you can access them by
<?php echo $client_item['hospital_name'] ?>
<?php echo $client_item['testing_center_name'] ?>
EDIT: Also you shouldn't use SELECT *, which clients.* is doing. Updated my code.
What happens if you try this:
$this->db->join('hospital', 'hospital.id = clients.id');
$this->db->join('testing_center', 'testing_center.id = clients.id');
instead of this:
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
also check
client*s* and client if they are the same everywhere
And also change as Nerd proposed:
$this->db->select('clients.*');
to:
$this->db->select('*');
It sholud be like this
$this->db->join('hospital', 'clients.hospital_id = hospital.id');
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id');
Related
There are two tables 'student' and 'parent'. both tables have 'f_name' and 'l_name' columns. I used left join for those two tables.
I want to display data from those two tables. However, when I use the following code I get 'parent name' in the column where student name is supposed to be shown. I get that it happens because both tables have 'f_name' and 'l_name' columns. but How do you fix this?
Controller
function index()
{
$this->load->model('Tableview_model');
$student_data= $this->Tableview_model->fetch_data();
$data["student_data"] = $student_data;
$this->load->view('register_students', $data);
}
model
function fetch_data()
{
$this->db->select('s.student_code, s.f_name, s.l_name, s.tel, p.f_name, p.l_name');
$this->db->from('student as s');
$this->db->join('parent as p','s.p_id=p.p_id','Left');
$query=$this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return false;
}
}
view
<?php
foreach ($student_data AS $row) {
?>
<tr>
<td><?php echo $row->student_code; ?></td>
<td><?php echo $row->f_name; ?> <?php echo $row->l_name; ?></td> //I'm supposed to get student first name and last name here
<td><?php echo $row->tel; ?></td>
<td><?php echo $row->tel; ?></td>
<?php
if(isset($row->f_name) && isset($row->l_name)){ // using isset() because of LEFT JOIN
?>
<td><?php echo $row->f_name; ?> <?php echo $row->l_name; ?></td> //I'm supposed to get parent first name and last name here
<?php
}
}
?>
output
You can make alias of field name as below:
$this->db->select('s.student_code, s.f_name, s.l_name, s.tel, p.f_name as pf_name, p.l_name as pl_name');
$this->db->from('student as s');
$this->db->join('parent as p','s.p_id=p.p_id','Left');
$query=$this->db->get();
And can use pf_name and pl_name in your view. Hope it helps you.
So, I have an sql table that doesn't have a unique column. I have to display it in a table by it's ID. But, like I said, the ID is not unique
OBSĀ¹: Notice that it comes from a select statement
OBSĀ²: the user id can appear in the select from 0 up to 4 times (according to id_questionario)
So, the thing is, how am I going to make a table that will show only one time user_id but will display by it's side all the melhor_tentativa according to id_questionario (notice that id_questionario is not to be displayed, only so I can display the melhor_tentativa in the correct order)
What I understand is that I have lines that should be columns. I've been trying for 2 days to figure this out and turns out I can't. Thought that it might be miss in how I made my sql
This is how the table looks
This is how the sql table looks
And bellow is the code I made for it
<?php
$count = 0;
$nota1 = 0;
$nota2 = 0;
$nota3 = 0;
$nota4 = 0;
?>
<?php
foreach($notas as $nota):
$total = 0;
?>
<tr>
<td><?php echo $nota['user_id']?></td>
<td>NULL</td>
<?php
if($nota['id_questionario']==1){
$nota1 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota1 ?></td>
<?php
if($nota['id_questionario']==2){
$nota2 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota2 ?></td>
<?php
if($nota['id_questionario']==3){
$nota3 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota3 ?></td>
<?php
if($nota['id_questionario']==4){
$nota4 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota4 ?></td>
<td><?php $total = ($nota1 + $nota2 + $nota3 + $nota4)/4; echo $total; ?></td>
</tr>
<?php $count++; ?>
<?php
endforeach;
echo $count;
?>
</table>
P.S: I know that total is completly wrong ;)
After a bunch of work I figured this out. So what I did was the following. I had that table which would show 4 results for a one ID. I made a view out of this table
Then I did a subquery from that view, so I would be able to show all results in one line
Hope it helps anybody in the future :)
I am working in a school project using some CodeIgniter and I had a little problem while making a view.
Basically, I need to print a column value in the screen and, in order to get this value, I had to use an inner join.
form_label($this->db->query("SELECT DISTINCT(MATERIA.NOME) FROM MATERIA INNER JOIN TURMA_has_MATERIA ON
TURMA_has_MATERIA.MATERIA_idMATERIA = MATERIA.idMATERIA
WHERE MATERIA.idMATERIA = " . $thm->MATERIA_idMATERIA), "txt_1i")
The query works just fine when I use it in phpMyAdmin but it returns an array when used in a CodeIgniter view, resulting in an error. Is there any function that could help me to convert this value to something that could be printed? Thank you.
this is how I normally do joins and then print it in Codeigniter
[model]
function get_tutor_info($data) {
$this->db->select('t1.id, t1.name, t2.nationality, t6.qualification');
$this->db->from('user t1');
$this->db->join('nationality t2', 't2.id = t1.nationality_id', 'inner');;
$this->db->join('qualification t6', 't6.id = t4.qualification_id', 'inner');
$this->db->where('t1.id', $data['tutor_id']);
$query = $this->db->get();
return $query->result_array();
}
[NOTE] If you want to get an object instead of getting an array change the return at the top as such.
$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;
[view]
<?php foreach($query as $row): ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->nationality; ?></td>
<td><?php echo $row->qualification; ?></td>
</tr>
<?php endforeach; ?>
If you want to know more about how to manipulate the results please refer to the codeigniter documentation
Hope this helped.
I'm trying to delete a row from a MySQL table using PHP. I have visited many pages already but I can't find a reason why I can't make it work.
Contacts.php
require_once "functions.php";
$db = new DatabaseConnection();
$user = new AddressBook($db->pdo);
<table>
<tr>
<th>Nombre</th>
<th>Telefono</th>
<th>Direccion</th>
<th>Email</th>
</tr>
<?php
$i=0;
$contactos=$user->verContactos();
foreach($contactos as $conts){
$i++;
?>
<tr>
<td><?php echo $conts['nombre']; ?></td>
<td><?php echo $conts['telefono']; ?></td>
<td><?php echo $conts['direccion']; ?></td>
<td><?php echo $conts['email']; ?></td>
<td>Borrar</td></tr>
</tr>
<?php } ?>
</table>
delete.php
<?php
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$result = mysql_query("DELETE FROM contacts WHERE id=$id")
or die(mysql_error());
header("Location: contacts.php");
}
else
{
header("Location: contacts.php");
}
?>
I know you're gonna say mysql is not useful anymore. In my web system I have this part using a function and PDO extension.
It doesn't matter if I use this example or my version using PDO, I cannot get the id. I just get something like this:
http://localhost/agenda/borrar.php?id <- Missing ID
The id field in database is called 'id'. I think the problem may be the use of $_GET but I can't find it.
This:
<td>Borrar</td></tr>
Needs to be this:
<td>Borrar</td></tr>
And this:
$result = mysql_query("DELETE FROM contacts WHERE id=$id")
Needs to be this:
$result = mysql_query("DELETE FROM contacts WHERE id=" . intval($id))
please view the following code.
<?php foreach($rows as $row): ?>
<tr>
<td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8');?></td>
<td><?php echo htmlentities($row['id'], ENT_QUOTES, 'UTF-8');?></td>
<td><?php echo htmlentities($row['starthol'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['endhol'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['days'], ENT_QUOTES, 'UTF-8'); ?></td>
<td>Approve</td>
<td>Reject</td>
</tr>
This populate a table from a previously ran sql query. Basically, when approve is clicked,I want to run an SQL query that uses the id of the the row and updates a column in the table called "active".
UPDATE holidays
SET active = "active"
WHERE holid = getid
Any advice on how to do this?
I am a php novice, so please go easy on me. Thank you.
You should consider learning how to GET and POST
<td><a href="active.php?id=?"<?php echo $row['id']; ?> >Approve</a></td>
Server side code active.php(sample dont copy paste you must use post to update db state not get also mysql_* depricated)
$id = mysql_real_escape_string($_GET['id']);
mysql_query("UPDATE holidays
SET active = 'active'
WHERE holid = $id")or die(mysql_error());
header('location:listingpage.php?msg=approved');
Are you able to get your query to run and return data? Assuming you have data, you will want to update your foreach() loop. If you want the loop to work over more than one line, you'll have to use curly braces ({ })
So your code would look more like:
<?php foreach($rows as $row) { ?>
...
<?php } ?>