How to Show Latest Uploded Post first Using SQL and PHP? - php

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");

Related

PHP gallery album

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...

Show 5 random item and 5 item by brand mysql php

Hello please someone can help me with this code? I think there i can remove * and GET id.
<div class="col-md-4">
<h1>Sidebar</h1>
<?php
require_once 'connessione.php';
$id=$_GET['id'];
$query = "SELECT * FROM campi_name where id='$id'";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="..." alt="...">
</a>
</div>
</div>
<?php
}
?>
</div>
How can i show only item (from db) related to the Brand row from table campi_name?
and with code like that how i can show 5 random item from campi_name?
Thank you!
For the first question you can use
$query = "SELECT Brand FROM campi_name WHERE id='" . $id . "'";
I'm not sure because I haven't understand what you are asking. Do you want to select only Brand column or what? Do you want to select just the row that content some letters?
For the second question use
$query = "SELECT * FROM campi_name ORDER BY rand() LIMIT 5"

PHP Mysql ASC AND DESC Order Proper use?

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

PHP - display database values on every content box in PHP

<li class="list-group-item">
<a href="/user/Michael" class="thumb-sm pull-left m-r-sm">
<img src="http://www.gravatar.com/avatar/8b7a9ba3cbf958009080f6da12a55029?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a>
<a href="user/Michael" class="clear">
<strong class="block">
<?php include '/includes/connection.php';?>
<?php echo $products['Title'] ; ?>
</strong>
<?php include '/includes/connection.php';?>
<small><?php echo $products['Followers'] ; ?> Followers </small>
</a>
</li>
<li class="list-group-item">
<a href="/user/Steven" class="thumb-sm pull-left m-r-sm">
<img src="http://www.gravatar.com/avatar/a5fb2decd550cdf33cbb8ce7566ba772?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a>
<a href="/user/Steven" class="clear">
<strong class="block">
<?php include '/includes/connection.php';?>
<?php echo $products['Title'] ; ?>
</strong>
<small><?php echo $products['Followers'] ; ?> Followers</small>
</a>
</li>
do i need to manually insert for every content?
I have over 100 contents how can i automatically insert in every line like content 1 display row 1 followers and content box 2 display row 2 followers and so on
The trick here is to make a loop within the code. This means you have to generate all content from out of the database or it's not gonna work. Let me show you an example:
<ul>
<?php
$sql = "SELECT ProjectId, ProjectTitel, ProjectExpertise
FROM project";
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_execute($stmt1);
mysqli_stmt_bind_result($stmt1,$ProjectId,$ProjectTitel,$ProjectExpertise);
while (mysqli_stmt_fetch($stmt1)){
?>
<li class="wow fadeInLeft" data-wow-offset="30" data-wow-duration="1.5s" data-wow-delay="0.15s">
<a href="inc/elements/project.php?id=<?php echo $ProjectId; ?>" class="meer">
<img src="img/portfolio/<?php echo $ProjectId; ?>/thumbnail/1.jpg" alt="<?php echo $ProjectTitel; ?> project">
<div class="project-info">
<div class="project-details">
<h5 class="witte-text blauwe-streep-onder">
<?php echo $ProjectTitel; ?>
</h5>
<div class="details witte-text">
<?php echo $ProjectExpertise; ?>
</div>
</div>
</div>
</a>
</li>
<?php
}
?>
</ul>
I start an UL outside of the PHP code and then I start my PHP query, as you see, i select the items i need from the database. Here i create a while loop and run through my setup of the list element. As you see, i use my items within the project id link (the link is an ajax call to another page), the thumbnail needed to show the picture, the title and the expertise i used.
In your case, the while loop you need requires more than just the the title and followers. I think you need as well an userid / username so you can take in account which user it is. Your loop would now proces all on the same user, since its staticly defined in your code. Also the avatar picture is staticly defined. Let me try to resolve some for you.
<?php
include '/includes/connection.php';
$sql = "SELECT products.title, products.followers
FROM products"; // as example query
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_execute($stmt1);
mysqli_stmt_bind_result($stmt1,$title, $followers);
while (mysqli_stmt_fetch($stmt1)){
?>
<li class="list-group-item">
<a href="/user/Michael" class="thumb-sm pull-left m-r-sm"> <!-- make the username also to be pulled out of a database. -->
<img src="http://www.gravatar.com/avatar/8b7a9ba3cbf958009080f6da12a55029?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a> <!-- you should save the avatar link into a database as well -->
<a href="user/Michael" class="clear"> <!-- make the username also to be pulled out of a database. -->
<strong class="block">
<?php echo $title; ?>
</strong>
<small>
<?php echo $followers; ?>
Followers
</small>
</a>
</li>
<?php
}
?>
This as example. As you see, i added notitions behind some code, to explain this should also be dynamic, since it will help you Remember. being a programmer is to find shortcuts on how you display your information. Code that repeats itself constantly with a changing variable should always be looped, to make sure you dont type unneccesairy code.
Since I dont know how your database is made, i can only guess, Michaels name is probably linked to an ID in the same table your products are, which you can use then in to pull out of the database, by searching in your user table. I hope I make sense here. For instance, your products.userid should be as well in your user table as user.userid. Most likely the userid will have a name linked to it in the user table.
$sql = "SELECT products.title, products.followers, products.userid, user.userid, user.username
FROM products, user
WHERE user.userid = product.userid";
So now you have in each row as well the name of the person of who's title it is. And that you can echo out again in the code i put up. (make sure you bind the result in the same order as you pulled them up)
Writing code is all about making it easier to display your information. Loops is and stays the keyword here, as NadirDev explains.
I hope I helped you getting on the right track.

MySQL select on PHP can't get it to work

I am having a problem retrieveing information from a MySQL Data Base. Could you help me out? This is the code I wrote:
<?php
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
while($nt = mysql_fetch_array($result)) {
?>
<div class="nuevos_1">
<div class="over_descripcion">
<div class="over_title">
<h3><?php echo $nt[titulo] ?></h3>
</div>
<div class="over_autor">
<span><b>Por: </b><?php print ucwords(str_replace("-", " ", $nt[autor])); ?> <?php echo $nt[fecha] ?></span>
</div>
<div class="over_texto">
<span><b><?php echo strtoupper(str_replace("-", " ", $nt[categoria])) ?></b> <?php echo substr(strip_tags($nt[texto]), 0, 240)."..." ?></span>
</div>
<div class="over_ver_mas">
<input type="button" value="Leer más" onclick="location.href='/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>'" />
</div>
</div>
<a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>">
<img src="http://<?php echo $nt[imagen] ?>" border="0" />
</a>
</div>
<?php }; ?>
Nothing inside the SELECT notas database is showing. It is simply gone...
Thanks!
Limit 1 has to go after the ORDER BY
change your query to
SELECT * FROM notas ORDER BY id DESC LIMIT 1
what does the error message say
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
if (mysql_error()) {
die(mysql_error());
}
The other answers above have it correct - you've got a syntax error in your query. If you'd done the basic step of having:
$result = mysql_query(..) or die(mysql_error());
you'd have seen the error message. Never EVER assume that a query succeeds. Always check for error conditions.
As well, you've got a minor syntax bug in your output:
... <a href="/nota/<?php echo $nt[fecha]."/".$nt[titulolower] ?>"> ...
^^^^^
Your array keys are not quoted, which means they're interpreted as define()'d constants. PHP is polite and will treat those constants as strings if there actually is not constant with that name, but it will issue a warning for every occurence. It should be:
<?php echo $nt['fecha'] ... ?>
I don't too well about PHP, but your select statement limits to just one result. You might want to change this line:
$result = mysql_query("SELECT * FROM notas LIMIT 1 ORDER BY id DESC");
You will not get more than one result as it is.

Categories