I know the basics of MySQL and PHP. I've set up a posting system that echoes the fields from my table.
The only problem is, since this is a news page, the newest posts need to be on top. And when the page echoes back the results, it puts the first entry I made (in the table) at the top.
This is my code:
// Selecting The Table
$result = mysql_query("SELECT * FROM posts")
or die(mysql_error());
// Connecting To The Rows, And Echoing Back The Results
while($row = mysql_fetch_array($result)) {
echo "<div class='post'>";
echo "Title: " . $row['title'];
echo "<br />";
echo $row['content'];
echo "</div>";
I am not sure how to make the latest post the first to show up.
I still want it to echo back the older entries though.
I can use phpMyAdmin if necessary.
You need to order your results.
SELECT * FROM posts ORDER BY date_posted DESC
Related
I have a posts table which contains a field for the posts. Let's say I have 10 posts that i want to show in 10 divs. How should I proceed in doing that? I've managed to get the full contents using a while loop, but that only shows the full contents in one place, and I want to have individual divs (so i can use different background colors) for each individual post.
Help me out please, I hope it makes sense. Just think at the way facebook displays posts for example. Each post has it's own box. I want something similar.
Snippet of the code I have to get the posts is available here:
<?php
require_once("includes/database.php"); // Get the database connection
$get_post = "SELECT full_post FROM posts";
$show_post = mysqli_query($connection, $get_post);
if (!$show_post) {
echo "Could not load post. " . "(" . mysqli_error($connection) . ")";
}
while ($post = mysqli_fetch_assoc($show_post)) {
echo $post["full_post"] . "<br />";
}
mysqli_free_result($show_post);
?>
The easiest method of doing this is creating the divs from the while function that's showing your posts and adding CSS classes to them.
Example:
while ($post = mysqli_fetch_assoc($show_post)) {
echo '<div class="blue">';
echo $post["full_post"] . "<br />";
echo '</div>';
}
I imagine you are looping through the individual posts using the while-loop. You can start a new div every round.
while($post = mysqli_fetch_assoc($result)) {
print '<div>';
print $post->content;
print '</div>';
}
Ok, so. I have asked about 5 questions on stackOverflow today, you've all been so helpful.
Now, i'm a designer, learning to code, so bear with me.
I have a mySQL table, with a small CMS/Blog im building.
I have it styled how I want to now.
This is the code for the page.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<h1>" . $row['Title'] . "</h1>";
echo "<br>";
echo "<h2>" . "Posted on: " . $row['Date'] . "</h2>";
echo "<br>";
echo "<img src='".$row['Image']."'/>";
echo "<br>";
echo $row['Message'];
echo "<br>";
echo "<br>";
}
I'm still working on it so its all good.
What I want to know is, this code is outputting my sql data into a page. Is there any way of telling the page what order to echo the data, for instance. in my SQL table i have:
2012-11-03 16:16:06 This is my First Blog Post This is the first message of the first blog post. ... http://blog.gettyimages.com/wp-content/uploads/201.
and next is
2012-11-03 16:17:29 This is my Second Blog Post This is the second message of the Second Post, You... http://www.aviation-images.com/user/zooms/118/451n...
How can i tell the page to Always display the most recent post at the top, and older ones below.?
Use order by in your query:
$result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
If your MySQL database schema uses DATETIME for the Date column, you can simply sort in the MySQL query with ORDER BY:
$result = mysql_query("SELECT * FROM Blog ORDER BY Date DESC");
If you have an auto-increment column like post_id, then you can use ORDER BY post_id DESC as well. :)
I have made a website in which the users can select a value from a dropdown menu and get some information from a database. I used ajax to send the request to the database (so the page doesn't get refreshed when I send the request). Here is the part of the jquery function:
$.ajax({
type:'POST',
url:'activities_code.php',
data: {datastr:datastr, datastr1:datastr1},
success:function(response){
$("#msg").html(response);
}});}); // there are other functions before..
The results appear on the main container of the webpage. They are composed of a title and some text. I echo the title in such a way so it is a link. I also give to each element an id and a class so I can call it later. Here is the corresponding code:
echo "<table id=\"container\">";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']."";
echo "<br>";
echo $row['PK'];
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
}
What I am trying to do now is: When I click on the title (which is a link), a new page to open in which a php script runs a query and show more information:
Here is what I have:
<?php
include('connect.php');
$query = "SELECT title,Information from activities where title='?????'";
$result = mysqli_query($dbcon, $query) or die('no available data');
echo "<table>";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']." ";
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
// Here I sum up the number of the results
$num_results=$num_results+1;
}
?>
I am trying to find a way to put in my query, in the where clause, the name of the title that I selected:
$query = "SELECT title,Information from activities where title='?????'";
Any help would be much appreciated. Let me know if everything is clear or I didn't explain some point clearly.
Thanks.
D.
You can get the title using $_GET global variable and URL parameter. Try changing this line:
echo "".$row['title']."";
to
echo "".$row['title']."";
then you can get the title with these code:
$title = $_GET['title'];
make sure you sanitize the value first. I hope this will help you.
link:
PHP $_GET
Ok, so I'm trying to make some simple code to display news articles from a MySQL server but all I get is a completely blank middle part of the page where the news articles are supposed to be. Here is the code:
<?
$query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<div class=\"newsItem\">";
echo "<h2>" . $row['header'] . "</h2>";
echo "<p>" . $row['content'] . "</p>";
echo "</div>";
}
?>
The problem seems to be with the while loop. If I write echo "WTF"; outside the loop it will show but if i write it inside it wont show. I'm not really good at PHP so I'm puzzled. ID is INT and Primary Key, header is VARCHAR(255) and content is TEXT. Any Ideas? Also the connect scrips works cuz I dont get error messages when it dies.
Try adding an error catch:
$query = "SELECT ID, header, content FROM news ORDER BY ID DESC";
$result = mysql_query($query) or die(mysql_error());
OR you have no results. so add somthing for that:
if(mysql_num_row($result) > 0){
while($row = mysql_fetch_array($result))
{
echo "<div class=\"newsItem\">";
echo "<h2>" . $row['header'] . "</h2>";
echo "<p>" . $row['content'] . "</p>";
echo "</div>";
}
}
else {echo 'no results';}
You either have no records in your news table or displaying warnings isn't enabled (slap)
There aren't any news in news table
One or more columns are missing
Table news does not exists
in your case, try to replace your 2nd line with
$query = "SELECT ID, content FROM news ORDER BY ID DESC";
You may have a database rights issue or your query may have an error but the error isn't being displayed.
If you don't have display_errors turned on in your php.ini, you should take a look in the web server error log file to see if an error is being logged when you connect to the MySql database.
You could also try:
ini_set('display_errors', 1);
At the top of your script which will force any database connection or query errors to be displayed in the resulting web page.
NOTE: This is a feature to support your development and should never be used on production systems.
I have a link/page 'myfiles.php' which shows the details of the file that a certain user uploaded. But after clicking again/entering the 'myfiles.php' into the address bar, the records are gone in the page. What's supposed to be the solution? Please help. Thanks. Here's my code:
while ($row=mysql_fetch_array($query)) {
$row1 = $row['name'];
$row2 = $row['size'];
$row3 = $row['type'];
$delfile = "<a href='deletefile.php?file=$row1'>Delete file</a>";
$dlfile = "<a href='download.php?file=$row1'>Download</a> ";
echo "<p>";
echo $row1;
echo "<br>";
echo $row2;
echo "<br>";
echo $row3;
echo "<br>";
echo $dlfile;
echo $delfile;
}
Are you using some sort of browsing accelerator and donĀ“t you have a deletion confirmation?
It seems your browser is requesting all links on your page and deleting your records.
If you want to delete, insert, update, etc. records in a database, it is a very good idea to use POST instead of GET (like a clickable link), so you would have to add a form around every entry that posts the data to the server. You can of course skip this, but then you definitely need a POST based deletion confirmation.
I am assuming that $usersess is not changing? If this is dependent on a session or cookie - you should check that it is not expiring or being destroyed.
$query = mysql_query("SELECT * FROM uploadedfiles WHERE username='$usersess' ");
while ($row = mysql_fetch_array($query)) {
$delfile = "Delete file";
$dlfile = "Download ";
echo "<p>{$row['name']}<br>
{$row['size']}<br>
{$row['type']}<br>
{$dlfile}{$delfile}</p>";
}