how can I use the ASC AND DESC in php with mysql ? here's my code below.
<?php
$gallery_query = mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}'");
if(mysql_num_rows($gallery_query) == 0){
?>
<td align="center">
<p>No Photos Available</p>
</td>
<?php
} else{
while($photo = mysql_fetch_assoc($gallery_query)){
?>
<td align="center">
<a href="#" data-toggle="modal" data-target="#photo<?php echo $photo['id']?>">
<div class="popover_img">
<img src="<?php echo $photo['photo']?>">
</div>
</a>
</td>
and I want to do is Ascending the data from the latest photo to old photo
then add ORDER BY to query
mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}' ORDER BY date DESC");
use date (created date) column to order rather id or both
ORDER BY DESC :- means last to first(new to old) records
for more :- http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
its very simple
SELECT * FROM gallery WHERE control_id = 'YOUR ID' ORDER BY YOUR PHOTO ID COLUMN DESC
1)if you want to display your latest photo to old photo than you have to use in query "DESC" i.e descending order for e.g "select * from gallery where control_id='2' order by my_photo_id DESC
2)and if you want to display old photo to latest photo than you can use in query "ASC" i.e Ascending order and by default data will be display in ascending order. for e.g "select * from gallery where control_id='2' order by my_photo_id ASC
For more information you can learn from this link: http://www.w3schools.com/sql/sql_orderby.asp
And Yes I forgot I have Column in the table named post_date and here's the correct answer to my question
here's the code
<table style="width:100%;padding:0px;">
<tr>
<?php
$gallery_query = mysql_query("SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']}' ORDER by `post_date` DESC");
if(mysql_num_rows($gallery_query) == 0){
?>
<td align="center">
<p>No Photos Available</p>
</td>
<?php
} else{
while($photo = mysql_fetch_assoc($gallery_query)){
?>
<td align="center">
<a href="#" data-toggle="modal" data-target="#photo<?php echo $photo['id']?>">
<div class="popover_img">
<img src="<?php echo $photo['photo']?>">
</div>
</a>
</td>
<?php
}
}
?>
</tr>
</table>
you can change your sql query in php like this
SELECT * FROM `gallery` WHERE `control_id` = '{$row['control_id']} ORDER BY 'ColumnID' DESC
Related
I have a website where I show the post update on the database and we use PHP and SQL for that but currently oldest posts are showing first instead I want to show the latest uploaded Post first.
Here is My PHP code with SQL Query
$projectcat_query=mysql_query("select * from projectcat where id=3 ");
while($projectcat_data=mysql_fetch_assoc($projectcat_query))
{ $catid=$projectcat_data['id'];
$limit=3;
$project_query=mysql_query("select * from projects where catid=$catid and status=1 limit $limit ");
while($project_data=mysql_fetch_assoc($project_query)) { ?>
<div class="item <?php echo $projectcat_data['name']; ?>">
<div class="picframe">
<a class="" href="project/<?php echo str_replace(' ','-',$project_data['title']); ?>">
<span class="overlay">
<span class="pf_text">
<span class="project-name"> <?php echo $project_data['title']; ?></span>
</span>
</span>
</a>
<img src="images/services/<?php echo $project_data['image']; ?> ">
</div>
</div>
<?php } ?>
and here my DB table looks like
Change your 2nd query like this
$project_query=mysql_query("select * from projects where catid=$catid and status=1 order by id desc limit $limit ");
Just add ORDER BY statement to your query like this:
$projectcat_query=mysql_query("select * from projectcat where id=3 ORDER BY id DESC");
i am writing a php script that retrieves images from a database and shows them on page inside an album , each album contains several images...
here is an example for the code below :
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
<?php
$rs_result = mysql_query ($sql,$con);
while ($row = mysql_fetch_assoc($rs_result))
{
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
echo '<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">';
echo '<div class="hover ehover14">';
?>
<?php
echo"<a href='#details?d=$aid' onclick='showfunction()'>";
echo "<img src='admin/acatch/$aimage' class='img-responsive' alt='$aname' width='200' height='200' data-picture_id='$aid'>";
echo'</a>';
}
?>
this code shows all the albums contained in the database ,
but what i want to do is that when i click on the album cover , i want to display the images inside it on the same page , but i am not able to pass the album id to another php script on the same page to do this,
please if anyone can help
thank u in advance
Have a look at the following code:
<?php
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
$rs_result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($rs_result)):
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
?>
<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">
<div class="hover ehover14">
<a href='#details?d=$aid' onclick='showfunction()'>
<img src="admin/acatch/<?=$aimage?>" class="img-responsive" alt="<?=$aname?>" width="200" height="200" data-picture_id="<?=$aid?>">
</a>
</div>
</div>
<div class="gallery_album" id="gallery_album_<?=$row['albumid']?>">
<?php
/**
* now collect all images associated with each album by using $aid
*/
$sql_images = "SELECT * FROM tbl_album_images WHERE fk_album_id = $aid";
$images_result = mysql_query($sql_images, $con);
while($image = mysql_fetch_assoc($images_result)):
?>
<div class="gallery_album_image"><img src="<?=$image['dir_and_filename']?>"></div>
<?php
endwhile;
?>
</div>
<?php endwhile; ?>
What we are doing here is gettign all the albums as you allready does. But we introduce a subquery inside the first while-loop to fetch all associated images for each album.
We put these images inside new div-container with a unique album id you can use to show or hide the actual album with your showfunction()
Use CSS and javascript to show or hide accordingly.
OBS! WARNING!! Your mysql queries, and the one I provided, is very very BAD!
Have a look into prepared statement using php PDO or mysqli...
So I have this feature on my site that people can go to a top users tab and it will show the person with the most profit on my site.
Using this code
<?php
$rs1 = mysql_query("SELECT profit,steamid,name,avatar FROM `users` GROUP BY profit DESC LIMIT 1");
$row = mysql_fetch_row($rs1);
$profit = round($row[0],2);
$steamid = $row[1];
$name = $row[2];
$avatar = $row[3];
echo'
<div class="col-md-4 col-lg-4">
<div class="widget-bg-color-icon card-box">
<a href="profile.php?action=view&id='.$steamid.'" target="_BLANK"><img src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/e3/'.$avatar.'" width="100px" alt="user-img" class="img-circle hoverZoomLink">
<h2>'.$name.'</h2></a>
<p><font color="#01DF01">$'.$profit.'</font></p>
<div><font color="white">Most Profit</font></div>
</div>
</div>
';
But I can't figure out how to retrieve the information from the user with the second most profit.
Website: CSGOLog.com/topusers1.php
Limit can take two arguments
SELECT profit,steamid,name,avatar FROM `users` GROUP BY profit DESC LIMIT 1, 1
It the same query you used but with an additional parameter for limit.
According to my understanding you should use ORDER BY rather than GROUP BY here.
I have a php code which also includes pagination
<?php
//to make pagination
$statement = "`blog_posts`";
//show users
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");
while ($row = mysql_fetch_assoc($query)) {
echo '
<div class="box">
<a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
<hr/>
<p>'.$row['postDesc'].'</p>
<div class="clear"></div>
<hr/>
<div class="desc">
Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
<div class="underside">Read More</div>
</div>
</div>
';
}
echo pagination($statement,$limit,$page);
If I was to have three posts like so...
ID TITLE
-------------
1 Apples
2 Post 2
3 Grapes
My blog would spit them out like so...
Apples
Post 2
Grapes
This is bad because the newest posts are at the very bottom of the page (or at the last page)
I want it so that the newest articles appear first.
How would I do this. Please help explain why my current code isn't working correctly.
SELECT * FROM {$statement} ORDER BY `date` DESC LIMIT {$startpoint} , {$limit}
Where date should be the date, blog was saved
change your query like below and add order by
$query = mysql_query("SELECT * FROM {$statement} ORDER BY id desc LIMIT {$startpoint} , {$limit}");
Please, use order by column for sorting as per desc or asc order :
<?php
//to make pagination
$statement = "`blog_posts`";
//show users
$query = mysql_query("SELECT * FROM {$statement} ORDER BY date desc LIMIT {$startpoint} , {$limit}");
while ($row = mysql_fetch_assoc($query)) {
echo '
<div class="box">
<a class="title" href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a>
<hr/>
<p>'.$row['postDesc'].'</p>
<div class="clear"></div>
<hr/>
<div class="desc">
Posted on '.date("jS M Y H:i:s", strtotime($row["postDate"])).'
<div class="underside">Read More</div>
</div>
</div>
';
}
echo pagination($statement,$limit,$page);
Here is my code
#$sql="select * from mp_images where id='5' and status='0'";
#$query=mysql_query($sql);
while(#$row=mysql_fetch_array($query))
{
#$image=$row ['photo'];
?>
<img src="image/<?php echo $image; ?>" width="360" height="150">
<?php
}
?>
I want to display current latest images after I have added it into database
Just order them by id
#$sql="select * from mp_images where status='0' order by id desc limit 1";