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 } ?>
Related
I tried to use the SQL Aggregate Function SUM () and it successfully but i can't seem to be able to display this in a table
Below is my php code i tried it with
`$sql = " SELECT product_name, sum(unit_price_before_discount)
FROM transaction_sell_lines
GROUP BY product_name";
<!-- TABLE CONSTRUCTION -->
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Current Value</th>
</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<?php
// LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!-- FETCHING DATA FROM EACH
ROW OF EVERY COLUMN -->
<td><?php echo $rows['product_name'];?></td>
<td><?php echo $rows['quantity'];?></td>
<td><?php echo $rows['unit_price_before_discount'];?></td>
<td><?php echo ($rows['unit_price_before_discount'])*$rows['quantity'];?></td>
</tr>
<?php
}
?>
</table>`
This is the result i get
And this is my database structure
What i want to do is to add all the values in quantity that has the same name together and display in a table
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
I have a page and all the function is working my only problem now is ciunying the record from the databse..
Class.user.php
Public function data($count)
{
$stmt=$this->db->prepare("SELECT COUNT(*) FROM login");
$result=$this->db->prepare($count);
$result->execute();
$number_of_rows=$result->fetchColumn();
}
Index.php
<table>
<thead>
<tr>
<th>2014</th>
</tr>
</thead>
<tbody>
<?php
$count="SELECT COUNT(*) FROM login";
$crud->data($count);
?>
The problem is that its not showing the count..
You're writing the same query as function argument, but also inside the function itself. And you're only really using one. This is nonsense. Dedicate your method to return the count, don't make the query a parameter.
prepareing the statement is pointless in this case, since you're neither reusing it nor are you binding any values. You can simply query() it directly.
The clincher: you're neither outputting nor returning the count, so it's very very expected that it doesn't show up anywhere.
Here's a sane version:
public function getCount() {
$result = $this->db->query('SELECT COUNT(*) FROM login');
return $result->fetchColumn();
}
<tbody>
<tr>
<td>
<?php echo $crud->getCount(); ?>
I think I usually do it like this link
$sql= "SELECT COUNT(*) FROM login";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row =$stmt->fetchObject();
then to output it you would have to ECHO
<table>
<tbody>
<tr>
<td>
<?php echo $row['count'];?>
</td>
</tr>
</tbody>
</table>
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
I have one file, then for every file there are multiple comments about that file. I was able to successfully join the two tables, comments and files, however when I try to view the page where it displays the file and its comments, the page displays the name of file n times (n corresponds to the number of records of comments of that certain file).
How will I be able to display just once the file name as well as displaying the comments?
<table width="40%" align="center">
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
</table>
<table width="40%" align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666">
<?php foreach($rows as $file):?>
<tr>
<td width="15%"></td>
<td width="45%" id="name">FILESIZE:</td>
<td width="40%" id="txt3"><?php echo $file->size; ?></td>
</tr>
<tr>
<td></td>
<td id="name">DATE UPLOADED:</td>
<td id="txt3"><?php echo $file->dateUploaded;?></td>
</tr>
<?php endforeach;?>
</table>
<!--COMMENTS -->
<table align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666" width="40%">
<tr>
<td><h3>Comments</h3></td>
</tr>
<tr><?php foreach($rows as $comment):?>
<td><?php echo $comment->comm;?></td>
</tr><?php endforeach;?>
</table>
You can use
GROUP_CONCAT()
in your join.
SELECT person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles
FROM person
LEFT OUTER JOIN person_role ON person.id=person_role.person_id
LEFT OUTER JOIN role ON person_role.role_id=role.id
GROUP BY id
ORDER BY id;
is an example from a tutorial at
http://dev-loki.blogspot.com/2008/01/aggregate-join-results-with-mysql-using.html
can't give much better without seeing your schema.
EDIT: trying anyway.
SELECT files.*, GROUP_CONCAT(comments.comment SEPARATOR ',') as comments_list
FROM files
LEFT JOIN comments on files.id = comments.id
GROUP BY id
ORDER BY id
Adding onto orbit, in codeigniter if your using activerecord it would be:
(Sorry, I couldn't format code in a comment)
$this->db->select('person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles',false);
$this->db->from('person');
$this->db->join('person_role', 'person.id = person_role.person_id', 'left outer');
$this->db->join('role', 'person_role.role_id = role.id', 'left outer');
$this->db->group_by('id');
$this->db->order_by('id'):
$this->db->get();
well it looks like you're echoing the filename for each row that is returned
your rows are getting returned like
name | size | comment1
name | size | comment2
so just don't do foreach ($rows as $file) that will print out the filename each time, just change
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
to
<tr>
<td width="70%" id="title"><?php echo $rows[0]->name; ?></td>
</tr>