How do you fetch data from the database? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
<div id="report-tbl-holder">
<table id="report-tbl" class="table table-stripped table-bordered">
<thead>
<tr>
<th>#</th>
<th>Date/Time</th>
<th>Person's Code/Name</th>
<th>Establishment's/Barangay Code/Name</th>
<th>Temperature</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$where = ($eid > 0 && is_numeric($eid)) ? " and e.id = {$eid} " : "";
$tracks = $conn->query("SELECT t.*,Concat(p.firstname,' ',p.middlename,' ',p.lastname)as pname,p.code as pcode, e.name as ename,e.code as ecode from tracks t inner join people p on p.id=t.person_id inner join establishment e on. e.id = t.establishment_id where date_format(t.date_added,'%Y-%m-%d') BETWEEN '{$date_start}' and '{$date_end}' $where order by date(t.date_added) asc");
while($row=$tracks->fetch_assoc()):
?>
<tr>
<td class="text-center"><?php echo $i++; ?></td>
<td><?php echo date("M d, Y h:i A",strtotime($row['date_added'])) ?></td>
<td><?php echo $row['pcode'] . ' - ' . (ucwords($row['pname'])) ?></td>
<td><?php echo $row['ecode'] . ' - ' . (ucwords($row['ename'])) ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I created a row in my database named "temperature" and wanted to include it in a table on the webpage I have created but I don't know how to call the contents of the newly added row (temperature) to display. It did add a new row because I added "Temperature" in the table header but I don't know how to properly call the values for temperature from the database.

I think that there's a confusion... Here "Temperature" is not a row, but a column.
You can try to add the following code line inside your loop :
<td><?php echo $row['body_temp'] ?></td>

You can simply get the data from database in same manner as you are getting in last table columns . you have to give just newly added column name into $rows array.
<tr>
<td class="text-center"><?php echo $i++; ?></td>
<td><?php echo date("M d, Y h:i A",strtotime($row['date_added'])) ?></td>
<td><?php echo $row['pcode'] . ' - ' . (ucwords($row['pname'])) ?></td>
<td><?php echo $row['ecode'] . ' - ' . (ucwords($row['ename'])) ?></td>
<td><?=$row['body_temp'] ?></td>
</tr>

Related

display data from two table by matching id codeigniter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
customer table
enter image description here
siteproducts table
enter image description here
I want to display the records of currently logged in users product deatils
ex: customer no 15 is logged in then records should displayed only of him
Model
public function customerparcelstatus()
{
$this->db->select('*');
$this->db->from('siteproducts');
$this->db->join('customer', 'siteproducts.customer_id=customer.customer_id');
//$this->db->order_by('customer.customer_id');
return $this->db->get()->result_array();
}
Controller
public function custparcelstatus()
{
$this->load->model('orbitcustomer_model');
$data['parcellistview'] = $this->orbitcustomer_model->customerparcelstatus();
$this->load->view('customer/customer_dashboard', $data);
}
view
<tbody>
<?php if (!empty($parcellistview)) { foreach($parcellistview as $user) {?>
<tr>
<td><?php echo $user['customer_id']; ?></td>
<td><?php echo $user['customer_code']; ?></td>
<td><?php echo $user['product_code']; ?></td>
<td><?php echo $user['received_date']; ?></td>
<td><?php echo $user['received_from']; ?></td>
<td><?php echo $user['tracking_ref_no']; ?></td>
<td><?php echo $user['parcel_status']; ?></td>
<td><?php echo $user['parcel_note']; ?></td>
</tr>
<?php } } else { ?>
<tr>
<td>Norecords Found</td>
</tr>
<?php } ?>
You need to retrieve records only related to currently logged in customer by using where clause on query like below.
$this->db->where('customer_id', $customer); //currently logged in user

Mysqli Query not pulling all results? [duplicate]

This question already has answers here:
MySQLi query returns only one row
(3 answers)
Closed 5 years ago.
So I had some mysql code that I've begun to rewrite into mysqli and have run into a problem with the query, and that is when I execute it, I only receive one set of results instead of the several that I know it should be. This is the new code I am using and was wondering whether anyone had any ideas on where I'm going wrong?
code:
<?php
if ($result = $link->query("SELECT SUM(step_count.steps) as total, leagues.league_id, leagues.league_name
FROM step_count INNER JOIN logins on step_count.unique_id = logins.unique_id INNER JOIN leagues ON leagues.unique_id = logins.unique_id GROUP BY leagues.league_id, leagues.league_name ORDER BY `total`
DESC LIMIT 100 ", MYSQLI_USE_RESULT))
$rank = 1; {
$row = $result->fetch_assoc();
$result->close();
}
?>
<tr>
<td>
<?php echo $rank++; ?>
</td>
<td>
<?php echo $row['league_name']; ?>
</td>
<td>
<?php echo $row['total']; ?>
</td>
</tr>
</table>
<?php
mysqli_close($link);
?>
you have to use a while loop
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php } ?>
try like this.
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row->league_name; ?></td>
<td><?php echo $row->total; ?></td>
</tr>
<?php } ?>
you have to put a loop there.
you can replace this code and it will work
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php }
$result->close();
}
?>
Fetch assoc retrieves one row as an associative array.
So you must use a while loop to keep fetching the rows until there are no more. The first example clearly illustrates how. I modified your whole code so you can copy paste everything. Do read the example though.
<?php
$query = "SELECT SUM(step_count.steps) as total,
leagues.league_id, leagues.league_name
FROM step_count
INNER JOIN logins on
step_count.unique_id=logins.unique_id
INNER JOIN leagues ON
leagues.unique_id=logins.unique_id
GROUP BY leagues.league_id, leagues.league_name
ORDER BY `total` DESC LIMIT 100";
$rank = 1;
if ($result = $link->query($query, MYSQLI_USE_RESULT)) {
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php
}?>
</table>
<?php
}
mysqli_close($link);

I have a record Id that needs to be selected to be updated

I have a simple issue I cannot figure out. I am trying to get the id of the record setup as a link to go to a second page that updates the record. I have the update working when I click on update it takes me to the record. I want the id to do the same.
<html>
<?php
require_once("../db_connect.php");
$stmt = $db->prepare("SELECT * FROM Users");
$stmt->execute();
?>
<?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
<table bgcolor=F2F2F2 width=1080 border='2'table-layout: fixed >
<br>
<tr>
<th>Id</th>
<th>Update</th>
<th>First Name</th>
<th>Last name</th>
<th>Address</th>
<th>Bio</th>
</tr>
<tr>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>ID</a></td>"?>
<?php echo "<td>
<a href='../update.php?id=" . $row['id'] . "'>Update</a></td>"?>
<td><?php echo $row['First Name']; ?></td>
<td><?php echo $row['Last Name']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Bio']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
In general, it is a good practice to put duplicated content into a function or variable and then call it when needed, to improve code readability & to save time/space.
I have also noticed many people struggling with new syntax so I have split the "one-liners" and left comments explaining how does new syntax works.
function col_gen($slug,$id=''){
return (!empty($id))? // If ID parameter exist and not empty generate column with a link
'<td>'.$slug.'</td>': //else
'<td>'.$slug.'</td>';
}
And then, in your case, you can run this function inside a loop:
....
foreach($row as $k=>$slug){
echo ($k==='id')? //if key equals "id"
col_gen($slug,$slug) // Output column for ID
.col_gen('Update',$slug) // Output column for keyword Update
:col_gen($slug); //else output just column with the slug
/**
* Learn one-line PHP, you will love it, once you understand it...
* Full Example Above:
* echo ($k==='id') ? col_gen($slug,$slug).col_gen('Update',$slug):col_gen($slug);
**/
}

Sort Table with PHP but no sort by number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to create database, all I need is perfect (adding data, edit, and delete) in php. I just learn php yesterday but my teacher give me task that is crazy bcz deadline is tonight. My database table cannot sorted by number after adding data :( I create this using xampp and notepad ++, please help me for fix the result that is sorted the result after adding data on the table...
<html>
<head>
<title>Data Perikanan TPI Pondokdadap, Sendangbiru, Malang</title>
</head>
<body>
<?php
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_ikan") or die ("Gagal membuka database.");
?>
<table border="3">
</select>
<tr>
<th>Nomor</th>
<th>Tanggal Input</th>
<th>Pukul</th>
<th>Nama Kapal</th>
<th>Nama Nelayan</th>
<th>Bobot Tangkapan</th>
<th>Jenis Tangkapan</th>
<th>Nama Penginput</th>
<th colspan="3">Aksi</th>
</tr>
<?php
$query = "select * from ikan";
$result = mysql_query($query, $sambung);
//$no = 0;
while ($buff = mysql_fetch_array($result)){
//$no++;
?>
<tr>
<td><?php echo $buff['nomor']; ?></td>
<td><?php echo $buff['tanggal']; ?></td>
<td><?php echo $buff['pukul']; ?></td>
<td><?php echo $buff['namakapal']; ?></td>
<td><?php echo $buff['namanelayan']; ?></td>
<td><?php echo $buff['bobottangkapan']; ?></td>
<td><?php echo $buff['jenistangkapan']; ?></td>
<td><?php echo $buff['namapenginput']; ?></td>
<td>Edit</td>
<td>Hapus</td>
</tr>
<?php
}
mysql_close($sambung);
?>
</table>
<p align="left">Tambah Data</p>
</body>
</html>
Use MySQL's ORDER BY clause to sort data. Change
$query = "select * from ikan";
to
$query = "select * from ikan order by nomor";
Or with direction specified:
$query = "select * from ikan order by nomor asc";
$query = "select * from ikan order by nomor desc";

grouping class names together in my CMS php

I am done with a CMS school system which i created from scratch for practice in php. My question is for example I have Accounting 101, Computer Science 101, however there must multiple times for Accounting 101. For example: Ticket 1035, 1036 are both Accounting 101 and they should appear in the same table, but in my code it shows them in different classes. Here is my code.
if(isset($_GET['id']))
{
$category = $_GET['id'];
$sql = "SELECT * FROM classes WHERE category_id = " . $category;
$query2 = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query2))
{
?>
<center><h3><?php echo $row->class_name . '-' . $row->units; ?> </h3></center>
<table border ="0" wdith="100%">
<tr>
<td>
<strong>Description: </strong>
<?php echo $row->class_description; ?>
</tr>
</td>
</table>
<br/>
<table border="1" width="44%">
<tr>
<td width="60"><b>Ticket</b> </td>
<td width="123"><b>Days</b></td>
<td width="120"><b>Hours</b></td>
<td width="64"><b>Room</b></td>
<td><b>Instructor</b></td>
</tr>
<tr>
<td width="60"> <?php echo $row->ticket; ?> </td>
<td width="123"><?php echo $row->days; ?></td>
<td width="120"><?php echo $row->start_hours . $row->time_format . '-' . $row->end_hours . $row->time_format2 ; ?> </td>
<td width="64"> <?php echo $row->room_number; ?></td>
<td><?php echo $row->instructor_name; ?></td>
</tr>
}//end while
}//end if
Its showing Accounting 101 with different tickets in different tables, but it should be all in 1 table. Thanks.
You need a double loop if you're trying to get records inside of records. For example:
while ($row1 = mysql_fetch_object($query1))
{
echo $row1->ParentName;
$query2 = 'select * from `mytable` where `myForeignKey` = ' . $row1->ParentId;
while ($row2 = mysql_fetch_object($query2))
{
echo $row2->ChildName;
}
}
You could also do a left join. Let me know if you need a sample of that.
Edit:
The left join would be done like this:
$sql = "select * from `classes` as a where category_id = '{$category}' left join `tickets` as b on a.id = b.class_id"
Ref. http://www.w3schools.com/sql/sql_join_left.asp

Categories