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.
Related
I am having some trouble with php and mysql, I am even not sure how to properly ask the question, it seems very complex. Still if anyone can help me, i will be very thankful.
i have two tables
(allunit.sql)
id - unit_name
12 - MIS
14 - MIT
15 - ENG
when someone click enroll button from browser (unit_id) will store in enrollment table. if some one enroll into the unit, button will show (Already Enrolled), not not it will show "Enroll"
enrollment.sql
enroll_id - unit_id
1 - 12
2 - 14
I am using this query
$unit = SELECT * FROM allunit;
$enroll = SELECT * FROM enrollment;
$row_enroll = mysqli_fetch_assoc($enroll);
while($row = mysqli_fetch_assoc($unit)) {
if($row['id']==$row_enroll['unit_id']){
$button = 'Already enrolled';
}else{
$button = 'Enroll';
}
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['unit_name']; ?></td>
<td><?php echo $button; ?></td>
</tr>
<?php } ?>
if i add one unit button changes to "already Enrolled" for that unit, but if i add more than one, still only one button changes. other stays same "enroll".
I know my question is reallty messy, hope you will understand. Badly need help. Thank you
First, you have to tell the database to run your query, it is not enough to place a query in a text string. This is done, in this case using the query() method.
Second, as you want to process the Enrolments once for each of the Units, it would be useful to unload at least the Enrolment into an array so it is easily reusable
// assuming you have a connection and its in $con
$sql = 'SELECT * FROM allunit';
$units = $con->query($sql);
$sql = 'SELECT unit_id FROM enrollment';
$res2 = $con->query($sql);
// make an array of just the enrolment id's as that all you need
// so we can use in_array() later to do the test for are you already enrolled
$enrols = [];
while ($row = $res2->fetch_assoc()){
$enrols[] = $row['unit_id'];
}
while ($unit = $units->fetch_assoc() ) {
if ( in_array($unit['id'], $enrols) ) {
$button = 'Already enrolled';
}else{
$button = 'Enroll';
}
?>
<tr>
<td><?php echo $unit['id']; ?></td>
<td><?php echo $unit['unit_name']; ?></td>
<td><?php echo $button; ?></td>
</tr>
<?php
} // endwhile
?>
There are two problems I see in your code:
mysqli_fetch_assoc() is called on a MySQL result, not a query. You need to call mysqli_query() first. You can see an example in the docs: https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
When you get a result, such as $row_enroll, it's a collection of rows, so you can't use it with a column directly, i.e. $row_enroll['unit_id'] won't give you anything.
Finally, it doesn't appear that a comparison between two separate datasets like this is going to work well for you, at least with the current code. Consider using JOINs to return just one dataset.
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.
i want to display the total sum of a perticular field inside forloop which is coming from the database, here is my code inside view
<?php foreach($exptype as $exptypes) : ?>
<tr>
<td><?php echo $exptypes->expensestype; ?></td>
<?php
$this->db->select_sum('amount');
$this->db->from('westline_expenses');
$this->db->where('expensestype',$exptypes->expensestype);
$this->db->where('headofexpense','TAX');
$query = $this->db->get();
?>
<?php foreach($query as $taxexp) : ?>
<td><?php echo $taxexp; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
but the above code doesnt work, can anyone please help me in this regard. Thanks alot
Try
foreach ($query->result() as $taxexp){
echo $taxexp->amount;
}
You need to get the result set from the query you ran.
$results = $query->result_array()
You can then do your foreach on the result set.
As a side note, you will need to change echo $taxexp; to echo $taxexp[0];
Try This:
I think you are using this code in view. This code use in controller then assign in view.
<?php
$this->db->select_sum('amount');
$this->db->from('westline_expenses');
$this->db->where('expensestype',$exptypes->expensestype);
$this->db->where('headofexpense','TAX');
$query = $this->db->get();
$data = $query->result_array();
echo($data[0]['amount']);
?>
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');
I am trying to use PHP to return SQL values into an HTML table. I am able to get every column to populate without a problem except for the last column, "GROUP _ CONCAT (provision_id)."
Relevant code:
<?php
global $wpdb;
$wpdb->show_errors();
$contents = $wpdb->get_results( $wpdb->prepare("SELECT salaries.id, name, remaining, contract_value, GROUP_CONCAT( provision_id ) FROM salaries LEFT JOIN contracts ON contracts.id = salaries.id GROUP BY salaries.id"));
?>
[table header stuff...]
<?php
foreach ($contents as $content) {
?>
<tr>
<td><?php echo $content->name ?></td>
<td><?php echo $content->remaining ?></td>
<td><?php echo $content->contract_value ?></td>
<td><?php echo $content->GROUP_CONCAT(provision_id) ?></td>
<?php }; ?>
</tr>
Just echoing $content->provision-id doesn't work either.
Use an alias for the column.
GROUP_CONCAT( provision_id ) as pids
...
echo $content->pids
If you are fetching into objects, you should give your columns names that are legal class member identifiers in PHP (I'll link to the manual, although their description of valid variable names is horrible):
SELECT ... GROUP_CONCAT(provision_id) AS provisions