How to get 10 latest records from database - php

How to display 10 latest jobs from database like this. Please click here to see image
I have written following code so far not sure what to do next.
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($query) ) {
$job_title = row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$job_type = $row["job_type"];
$job_description = $row["job_description"];
$posting_date = date('d-m-y');
}
The $row returns all the columns from database. I want only four job title, company name, location and date

Use column names in select query.
$query = "SELECT job_title,company_name,location,posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
echo "<tr> <th>Job Title</th> <th>Company Name</th> <th>Location</th> <th>Date Posted</th> </tr>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($query) ) {
$job_title = $row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$posting_date = date('d-m-y', strtotime($row['posting_date']));
echo "<tr>";
echo "<td>".$job_title."</td>";
echo "<td>".$company_name."</td>";
echo "<td>".$location."</td>";
echo "<td>".$posting_date."</td>";
echo "<tr>";
}
echo "</tbody>";
echo "</table>";

Hmm change the query
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";

You can order by for rownum
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY rownum DESC LIMIT 10";

Here is an example how to build a htrml table with the 4 columns:
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
while ($row = mysqli_fetch_assoc($query) ) {
echo "<tr>";
echo "<td>$row['job_title']</td>";
echo "<td>$row['company_name']</td>";
echo "<td>$row['location']</td>";
echo "<td>$row['posting_date']</td>";
echo "<tr>";
}
echo "<table>";

Related

select maximum value of id from mysql table and save it as variable

Is it possible to extract highest id from table (in this case 9) and return it as variable $maximum, which I can use later as integer?
You can use MAX() function. Doc can be found here
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
You can try with following snippet:
$sql = "SELECT * FROM some_table where id = (select max(id) from some_table)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$id = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row["id"];
}
}
echo $id;

While loop showing only one record when i use nested while loop for fetch data from another table

I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}

SELECT COUNT for column in database?

How to Count number of rows in a table that matches to the related condition and echo that count out.
Code goes as follows::
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $result3['']; ?></small></p>
</div>
Any Suggestions will be appreciated..
Quotes missing
$sql3 = "SELECT * question_id FROM output WHERE question_id = '".$myid."'";
Secondly i dont see closing braces for while
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $rowCount['rowCount']; ?></small></p>
</div>
<?php }//while
} //if
?>
You need to use COUNT(question_id) function as below
$sql3 = "SELECT COUNT(question_id ) as TotalQuestions FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
Then to fetch result from above query use fetch_fieled()
while ($info = $result3->fetch_field()) {
$TotalCount = $info->TotalQuestions ;
}
And then display the value
<small><?php echo $TotalCount ; ?></small
try this
$sql3 = "SELECT COUNT(1) as row_count FROM output WHERE question_id = ".$myid."";
$result3 = $conn->query($sql3);
$row_count = $result3->fetch_assoc();
echo $row_count['row_count'];

How to sort in while loop

Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.
You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}

Get last 5 users data by desc

<?
require('../../members/inc/config.php');
require('../../lib/framework.php');
$sql = "SELECT * FROM members ";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );
?>
<table class="table">
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
</tr>
<?php foreach( $results as $row ){
echo "<tr><td>";
echo $row['memberID'];
echo "</td><td>";
echo $row['username'];
echo "</td><td>";
echo $row['email'];
echo "</td>";
echo "</tr>";
}
?>
</table>
I guys, I want get the latest 5 users of my table. I already tried do this way:
$sql = "SELECT * FROM members ORDER BY username DESC LIMIT5";
But.. don't retrieve any results..
The query should be
$sql = "SELECT * FROM members ORDER BY username DESC LIMIT 5";
Whitespace is required between LIMIT and 5
If you want last 5 order should be by id/creation_time
$sql = "SELECT * FROM members ORDER BY id DESC LIMIT 5";
$sql = "SELECT * FROM members ORDER BY creation_time DESC LIMIT 5";

Categories