I have 3 divisions in my table, first is the "ID", second is the "TOPIC", then lastly is the "DATE POSTED". I have this prob, I can't make the new topic be on top. it just goes on the last of the list. Now, how can I make the latest topic that I posted be on top of my lists?
my codes:
$query = mysql_query("SELECT * FROM PythoN_Blog")or die(mysql_error());
echo "<table border='0' width='700'>";
while($result = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td align='center' style='padding-left:30;'>".$result['id']."</td>";
echo "<td align='center' style='padding-left:10;'><a href='#'>".$result['topic']."</a></td>";
echo "<td align='center'>".$result['date']."</td>";
echo "</tr>";
}
echo "</table>";
Change your query to this
"SELECT * FROM PythoN_Blog order by DATE_POSTED DESC";
if your DATE_POSTED column is NOT datetime format (e.g. varchar) then you have to convert it:
"SELECT * FROM PythoN_Blog ORDER BY CONVERT(DateTime, DATE_POSTED,103) DESC"
if your DATE_POSTED column is datetime format then:
"SELECT * FROM PythoN_Blog ORDER BY 'DATE_POSTED' DESC";
Related
When I enter the vehicleID and Date, the records must be sorted by using both vehicle ID and Date, however it doesn't get sorted from the Date but only with the vehicleID. How can I achieve this ?
if ($vid != null && $datepicker != null) {
$conn = new Db();
$sql = "SELECT * FROM trip_details where vehicle_id = '".$vid."' AND date_t = '".$datepicker."'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> ". $row["trip_id"]."</td>";
echo "<td> ". $row["vehicle_id"]."</td>";
echo "<td> ". $row["total_trip_km"]."</td>";
echo "<td> ". $row["predict_fual"]."</td>";
echo "<td> ". $row["date_t"]."</td>";
// echo "<td><input type=\"submit\" value=\"view map\"></td>";
echo "</tr>";
}
}
SQL makes no guarantee on the order results are returned from a select query unless you explicitly add an order by clause, so the fact that you observe the returned records sorted by the vehicle_id is coincidental. You need to add an order by clause to the query:
ORDER BY vehicle_id, date_t
to sort you data by the columns you have define the name of columns in order by clause with order Acs(ascending) or Desc (descending)
e.g.
order by desc column1, asc column2
Order by needs to define to get the desired result.
try this
$sql="SELECT * FROM trip_details where
vehicle_id='".$vid."' AND date_t='".$datepicker."'
ORDER BY vehicle_id DESC,date_t DESC";
Hi am retreiving the distinct dates then am trying to retreive the rows with that dates but am getting them as zero..
Here is code
$sql = "SELECT DISTINCT date FROM video_data ORDER BY id DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['date'];
echo "<tr>";
echo "<td>$date</td>";
$query_order = "SELECT * FROM video_data WHERE date=$date";
$query_order_total = mysqli_query($connection,$query_order);
var_dump($query_order);
$total = mysqli_num_rows($query_order_total);
echo "<td>$total</td>";
echo"<tr>";
But when am selecting "SELECT * FROM video_data" its showing correct count
you are missing ' ' around $date
$query_order = "SELECT * FROM video_data WHERE date='$date'";
That said: when you have, let's say, 60 dates found in your first query, you will than fire 60 new queries to find the actual data.
how about:
$sql = "SELECT COUNT(*) total, date
FROM video_data
GROUP BY date
ORDER BY date DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>". $row['date'] ."</td>";
echo "<td>". $row['total']. "</td>";
echo"<tr>";
}
hi i already can make search. now how i want to put that data based on their role, based on this picture i want to make admin ,admin , user, user,user. need to use sort by ? i dont know.
and this my coding search . where should i put sort by ?
<?php
include 'config1.php';
if(isset($_POST['search'])){
$searchTerm = $_POST['search'];
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
echo "<table height = '30%'border='1'>";
if($count == 0)
{
echo "NO ID REGISTERED!";
}
else
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td width='5%'><b>USER ID:</b> {$row['userid']} </td>";
echo "<td width='5%'><b>USER NAME :</b> {$row['username']} </td>";
echo "<td width='5%'><b>USER EMAIL:</b> {$row['useremail']} </td>";
echo "<td width='5%'><b>USER ROLE:</b> {$row['userrole']} </td>";
echo "<td width='5%'><b>USER DIVISION:</b> {$row['userdiv']} </td>";
echo "<td width='5%'><b>USER DEPARTMENT:</b> {$row['userdepartment']} </td>";
echo "</tr>";
}
}
echo"</table>";
}
?>
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole";
Also can use ASC or DESC for displaying in ascending or descending order after ORDER BY.
eg.
Select * from users WHERE choice = 'PHP' ORDER BY id ASC;
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' order by fieldWhichYouWant";
OR
$query = "SELECT * FROM members WHERE userid LIKE '%$searchTerm%' ORDER BY userrole ASC";
or add ASC or DESC as Ascending or descending oder to sort.
Replace fieldWhichYouWant with your requirement.
I have multiple tables I am joining to use in results for a second query and nesting the second results inside the first results.
I am using the following code:
$result = mysqli_query($con,"SELECT info.lotto_id, info.name, info.number_balls, info.number_bonus_balls, info.db_name, country.name_eng AS country, currency.name AS currency, currency.symbol AS symbol, next.draw_date AS next_draw, next.jackpot AS next_jackpot
FROM info
LEFT JOIN country ON info.country_id = country.id_country
LEFT JOIN currency ON info.currency_id = currency.currency_id
LEFT JOIN next ON info.lotto_id = next.lotto_id
WHERE (info.active='1')
ORDER BY next_jackpot DESC");
while($lotto = mysqli_fetch_array($result))
{
echo "<table border='0' width='600px' align='center'>";
echo "<tr>";
echo "<td>";
echo "<h1>Results for:</h1>";
echo "</td>";
echo "<td align='right'>";
echo "<p><img src='images/". $lotto['lotto_id'] ."_big.png' alt='". $lotto['name'] ." Results'/></p>";
echo "</td>";
echo "</tr>";
echo "</table>";
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
while($draw = mysqli_fetch_array($result2))
{
echo "<table class='results' align='center'>";
echo "<tr>";
$draw['display_date'] = strtotime($draw['date']);
$lotto['cols'] = $lotto['number_balls'] + $lotto['number_bonus_balls'];
echo "<td class='date' colspan='".$lotto['cols']."'>".date('D M d, Y', $draw['display_date']). "</td>";
if ($draw[jp_code] < "1")
{
echo "<td class='winner' align='center'>Jackpot Amount</td>";
}
else
{
echo "<td class='rollover' align='center'>Rollover Amount</td>";
}
It is giving me the following error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/content/95/11798395/html/results/info_mysqli.php on line 59
This relates to my results2 query. Can somebody please suggest what I am doing wrong.
Thank you.
Change:
$result2 = mysqli_query($con,"SELECT * FROM" .$lotto['db_name'].
"ORDER BY date DESC
Limit 3");
to:
$result2 = mysqli_query($con, "SELECT * FROM {$lotto['db_name']} ORDER BY date DESC LIMIT 3");
if ($result === false) {
exit("Error: " . mysqli_error($con));
}
I have a database with a table named "photos". In that table i have a column "size"(which is the size of the photos i had upload). I want to get the sum of these sizes and print the sum in my php page. Can anyone give me an example code(sql code in php code)..??
i have try this:
$query = "SELECT SUM(ph_size) from photos";
$result= mysql_query($query,$con);
echo "<table border='1'>
<tr>
<th>SUM(ph_size)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//echo $row;
echo "<tr>";
echo "<td>" . $row['SUM(ph_size)'] . "</td>";
echo "</tr>";
}
echo "</table>";
but it doesnt work.
You can use the sum function
$result=mysql_query("SELECT SUM(p.size) AS sum_of_photos_sizes FROM `photos` p WHERE 1");
if (mysql_num_rows($result)){
$data=mysql_fetch_array($result);
echo 'Total size of photos: '.$data['sum_of_photos_sizes'];
}