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";
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...
I use this code to put a picture from a table into a div animated with jquery as a slideshow but the problem that i need only on picture and not all pictures in the same time ? what can i do ?
the code :
<?php
require_once 'includes/connect.php';
$stmt = $bdd->prepare('SELECT userPic FROM AddOffer WHERE id_user=:id ORDER BY id_user DESC');
$stmt->bindParam(':id',$id);
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetchColumn())
{
extract($row);
?>
<img src="user_offers/<?php echo $row['userPic']; ?>" class="img-roundeds" width="200px" height="200px" />
<?php
}
}
else
{
?>
<span> Ajouter une Photo à votre Profil</span>
<?php
}
?>
Add LIMIT 1 to your SQL
$stmt = $bdd->prepare('SELECT userPic FROM AddOffer WHERE id_user=:id ORDER BY id_user DESC LIMIT 1');
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
I've been trying to add a section called: "related games", in it there is a script (related.php) that will fetch 5 random related games of the same category that the online game is displayed on.
I tried this (related.php):
<?php
if(isset($_GET['genre'])){
$game_category = $_GET['genre'];
$select_games = "SELECT * FROM games ORDER BY rand() LIMIT 0,5";
$run_games = mysql_query($select_games);
while($row = mysql_fetch_array($run_games)){
$game_id = $row['game_id'];
$game_name = $row['game_name'];
$game_image = $row['game_image'];
?>
<table>
<tr>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span></a>
</div>
<tr>
</table>
<?php } } ?>
This is the "related.php" file and I tried to implement it in the following file called: "game_page.php" which works perfectly...
For some reason no random game is showing under the current played game...
Any idea?
I fixed it xD, I simply removed the
if(isset($_GET['genre'])){
$game_category = $_GET['genre'];
strings because they are already included in the "game_page.php" file :)