How to find out sum of particular field in view codeigniter - php

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']);
?>

Related

Trouble with displaying the result of a select in a table

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 :)

Convert inner join value to String - CodeIgniter

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.

How to print out a table from mySQL database inside a html/php page

i am trying to print out this table from phpmyadmin to my html/php page as a normal table. this is my coding for the page
Any help would be appreciated
Thanks
Looks like You were Using PDO , and all of a sudden you jump into old data fetching technique using mysql_*
My advice is to stick with PDO structure . SO you have to some little things
on line 6 use
$stmt->rowCount();
to get user row
then use
$data = $stmt->fetchAll();
to get database rows , You'll get an object .
Now You just need to loop through object for example
foreach ( $data as $rows ){
echo $rows->users;
}
Try this code:
<?php
$db = new mysqli("localhost", "root", "", "hangman");
?>
<table border="1">
<tr>
<td>User</td>
<td>Score</td>
</tr>
<tr>
<?php
$sql = "SELECT * FROM usernames";
$result = $db-query($sql);
while($row = mysqli_fetch_assoc($result)) {
?>
<td><?php echo $row['users']; ?></td>
<td><?php echo $row['Scores']; ?></td>
<?php
}
?>
</tr>
</table>
give me a comment if any errors.

running sql on link click

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 } ?>

Codeigniter: Join 3 tables and display data in view

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');

Categories