SUM and GROUP BY and Difference of colums - sql

i have a table t1
ag_name ag_start ag_end
a 10 20
c 30 50
a 60 70
c 70 75
i want to have this :
ag_name numberOfCards
a 20
c 25
which means a has (20-10) + (70-60) = 20 Cards.
then please help me with the QUERY ??

select ag_name, sum(ag_end - ag_start)
from the_unknown_table
group by ag_name

Please try:
select
ag_name,
sum(ag_end-ag_start) NoOfCards
From t1
group by ag_name

Select SUM(ag_end - ag_start) as numberofcards, ag_name From table
Group by ag_name

the query works fine when i click on go in DirectAdmin , but when i write the code to show the recordset it shows different result .
my model :
public function getAgents(){
$db =JFactory::getDBO();
$query_Recordset1 = "SELECT *,SUM(ag_end - ag_start) AS ag_num FROM #__basak_agent group by ag_fname";
$db->setQuery($query_Recordset1);
return $db->loadAssoc();
}
and this is my .php code :
</thead> <?php
$count=0;
$result=$this->get('Agents');
foreach( $result as $row_Recordset1 )
{
$count++;
?>
<tr>
<td> <?php echo $count ?></td>
<td> <?php echo $row_Recordset1['ag_fname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_lname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_num'] ; ?></td>
</tr>
<?php } ?>
</table>
but in code shows :
1 a a a
2 a a a
3 2 2 2
there is no field like this in table , i dont know where it comes from ??!
anybody have suggestion , where i have to look for it ? what is wrong with my code ?

Related

Start Order by data from table that is found in another table

My question may be not easy to explain but I am going to do my best.
I have 3 tables :
medicine
patient
patient_medicine
the third table contains 3 columns :
auto increment (id)
medicine_id
patient_id
I make a query that display all data in medicine table and if there is a patient id found in patient_medicine table I check it and those that are not found are not checked.
A- controller
public function showPatientMedicine()
{
$data['patient_id']=$this->input->post('patient_id');
$data['medicine'] = $this->model_admin->medicine();
$this->load->view('admin/show-Patient-Medicine',$data);
}
B- VIEW
<table>
<thead>
<tr>
<th>List</th><th></th>
</tr>
</thead>
<tbody>
<?php
foreach($medicine as $row){
$patient_med=$this->db->select('medicine_id')
->where('patient_id',$patient_id)
->where('medicine_id',$row->id)
->get('patient_medicine')->row('medicine_id');
if($row->id==$patient_med){
$checked="checked";
} else {
$checked="";
}
?>
<tr>
<td>
</td>
<td>
<input type='checkbox' value="<?=$row->id?>" <?=$checked?> />
</td>
</tr>
<?php
}
?>
</tbody>
</table>
THIS QUERY SHOWS ME ALL MEDICINES AND CHECK THE MEDICINES THAT ARE FOUND IN PATIENT_MEDICINE TABLE
MY QUESTION IS :
HOW CAN I SHOW ALL MEDICINES THAT ARE CHECKED IN THE FIRST POSITION OR HOW TO ORDER BY CHECKED CHECKBOX ?
THANK YOU IN ADVANCE.
Try this,
SELECT * FROM `medicine` as a left join (select medicine_id,patient_id from patient_medicine where patient_id = 2) as b on a.id = b.medicine_id
Put the above query in your controller, it will automatically sort the result based on patient id, if patient details found. it will return null for all other details in rows except medicine details.
So you can just
<?php foreach($medicine as $row){ ?>
<input type='checkbox' value="<?=$row->id?>" <?php echo !is_null($row->patient_id)?'checked':''; ?> />
<?php } ?>

How to display user_nicename and usermeta values by custom query in Wordpress?

I am trying to display user_nicename and one of usermeta values where meta_key is description. My query is given below:
<?php
$querystr = "
SELECT *
FROM $wpdb->users, $wpdb->usermeta
WHERE $wpdb->users.ID = $wpdb->usermeta.user_id
AND $wpdb->usrmeta.meta_key = 'description'
";
$retrieve_data = $wpdb->get_results($querystr, OBJECT);
?>
<?php global $retrieved_data; foreach ($retrieve_data as $retrieved_data){ ?>
<table>
<tr>
<th>Customer Name</th>
<th>Customer Details</th>
</tr>
<tr>
<td><?php echo $retrieved_data->user_nicename;?></td>
<td><?php echo $retrieved_data->meta_value;?></td>
</tr>
</table>
<?php } ?>
My final output should be displayed inside a table which will look something like below:
But I am in confusion how to do that. I am facing problem for both sql syntax and displaying data inside the table. Sorry for this easy question, I am still a learner. Can anyone help me in here? Thanks

Auto navigate (scroll) to certain table row

I have a table with few thousand records on few pages in a simple html table.. I made a search function that works fine apart from one thing... It displays only one result in a table (which is great cause it means it works!).But... I was wondering is there a way to display back the table with all records, with the one that i was searching for in the middle and highlighted? Here's a simplified table that I have :
<table class="nogap" cellpadding="1" bgcolor="#00000" cellspacing="1" style="margin:110px 0 0 5px; width:100%; border-color:#B6D6F6;" >
<tbody>
<?php include 'dbconn.php';?>
$con = mysqli_connect($host,$user,$pass,$db) or (header( 'Location: errorpage.php' ));
if (mysqli_connect_errno($con)) { header( 'Location: errorpage.php' ); }
$sql = "SELECT * FROM $tb1 ORDER BY (Serial_num +1) LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $sql) or (header( 'Location: errorpage.php' ));
$row = mysqli_num_rows($result);
while ($row = $result->fetch_assoc())
{
$product = $row['Prod_type'].$row['Serial_num'];
<tr id="mstrTable" class="lovelyrow">
<td width="5%"><?php echo $product;?></td>
<td width="5%"><?php echo $row['Customer'];?></td>
<td width="7%">
<a href="#"
onmouseover="ajax_showTooltip(window.event,'getptn.php?prd=<?php echo $p;?>',this);return false"
onmouseout="ajax_hideTooltip()">
<?php echo$row['Prod_info'];?>
</a>
</td>
</tr>
}
</table>
Thanks!
First of all don't give the same html id attribute to each row (mstrTable). Html Ids should be unique per page.
Instead mark table rows with unique ids, eg:
$html .= "<td id='row_".$row['id']."'>"
Then do a search query first, remember item id, figure out what page should be queried, query the whole page, attach classess 'greyedout' and 'highlighted' to rows accordingly and then you might try this javascript function to scroll down to the item:
https://developer.mozilla.org/en-US/docs/Web/API/element.scrollIntoView

CListView - adding serial number

<table border="1">
<tr>
<th width="35">S.No.</th>
<th align="left">Club Member</th>
<th width="155">Schedule</th>
<th width="155">Point</th>
</tr>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_reportSchedule',
)); ?>
</table>
how can i get serial number like 1,2,3, and so on ??
In the view file "_reportSchedule" use this:
<?php echo ++$index;?>
In your _reportSchedule.php partial view you can echo it out using:
echo $data->serial_number;
This assumes that serial_number is an attribute of whatever model you are using. As mnetioned by Sudhanshu Saxena, if you simply want the index (the number of the item in the set), use:
echo $index
This is a zero based numbering system (first record has an index of 0)
just use In the view file "_reportSchedule"
$widget->dataProvider->getPagination()->currentPage * $widget->dataProvider->getPagination()->pageSize + $index + 1;
$index indicates the number of the line starting at 0, is to display the correct number on all pages need to make calculations:
$widget->dataProvider->getPagination()->currentPage * $widget->dataProvider->getPagination()->pageSize + $index + 1

Show variable outside while loop

Hy. I know there are answers similar, but i can't get it going. Here is my cod:
<?php include 'header.php'; ?>
<?php include 'connect.php';
$q = mysqli_query($link, $aaa = 'SELECT * FROM agentii LEFT JOIN orase ON agentii.oras=orase.oras_id WHERE orase.oras_id = \'' .$_GET['id']. '\'') or die(mysqli_error($link));
$row = mysqli_fetch_assoc($q);
?>
<h2>Title <em><?php echo $row['oras'];?></em></h2>
<div class="div_view">
<table cellspacing="0" cellpadding="5" border="0" width="100%">
<tr>
<th>Agentie</th>
<th>Adresa</th>
</tr>
<?php while ($row = mysqli_fetch_assoc($q)) { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php }?>
</table>
</div>
<?php include 'footer.php'; ?>
This output(i can't put images because is a new account - so i put a link to a prt src):
http://postimage.org/image/w9zsexz91/
In my database i have 3 rows, and the code outputs only 2.
I know that is because of the 2 mysqli_fetch_assoc, but i want to show all the rows, and i want to show the title too (<?php echo $row['oras'];?>) outside the while.
Can someone please help me with this?
Thx in advance!
obligatory warning first: Remember little Bobby Tables!
(or: your code is vulnerable to sql injection attacks)
then: you get only two rows in the while loop, because you requested a row outside the loop first, thus increasing the internal cursor in the result.
If you want to get title and rows in one go, you need to cache the result, for example:
$resultArray = array();
while ($row = mysqli_fetch_assoc($q)) {
$resultArray[] = $row;
}
//....
?>
<h2><?php echo $resultArray[0]['oras']; ?></h2>
<?php // ... ?>
<?php foreach($resultArray as $row) {
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
} ?>
this is just a stub, of course you should implement checks if you got a row at all etc...
Try:
<?php do { ?>
<tr>
<td><?php echo $row['agentie'];?></td>
<td><?php echo $row['adresa'];?></td>
</tr>
<?php } while ($row = mysqli_fetch_assoc($q)); ?>
The first iteration of this loop will print the first fetched row. I have only changed the location of the while clause, forcing the code of the loop to be run before a new row is fetched.