sorting data/articles by date and time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stipek
Forum Newbie
Posts: 1
Joined: Wed Oct 16, 2013 2:51 am

sorting data/articles by date and time

Post by stipek »

Hello,

I'm newbie in PHP. I would like to ask you guys for some advices if you are willing to help.


So I have created code. In it I additionaly added new column in database called articledate and I echo time of each article.
My question is how to display the articles by date and title in a list sorted by date and how to display the article content and clicking on the title?

Any help is very welcome.

Thanks

Code: Select all

listitems.php

<body bgcolor="#225599" text=" #ffffff">
<font size="6">
<h3 align='center'>

<?php

include "mysql.php";
 echo "Maviance GmbH newsletter:\n" . "<br/>";

$query = 'SELECT * FROM blogtable';
if(!$result = $db->query($query)){
    die('There was an error running the query [' . $db->error . ']');
}

// Printing results in HTML

while($row = $result->fetch_assoc()){
    echo $row['title'] . '
    <br />';
}
$result->free();
$db->close();
?>

<form name="newarticle" action="newarticle.php" method="post">
<input type="submit" value="New article" style="height: 60; width: 170"/>

<h3>

<a href="content.php" style="font-size: 20px; text-decoration: none; color: #FFFFFF ">Read more about us here</a>
</body>
</html>






</form>
</body>

 </html>

newarticle.php

<html>
<head> 

</head>
<body bgcolor="#225599" text="#ffffff">
<h3>

<form action="insert.php" method="post">
Title: <input type="text" name="title" ><br>
Summary: <textarea rows="10" cols="60" name="summary" wrap="physical">Here goes your summary!</textarea>:<br /><br>
Content: <textarea rows="20" cols="150" name="content" wrap="physical">Hey Buddy! Enter text in our blog!</textarea>:<br />
<input type="submit" value="submit" name="submit" align=right>


<?php
//connect to db
include "mysql.php";


mysqli_query($db,"INSERT INTO blogtable (title, summary, content)
VALUES ('', '',)");


mysqli_close($db);
?>
</form>
<h3>
</body>
</html>

insert.php for the newarticle.php

<?php
include "mysql.php";

$sql="INSERT INTO blogtable (title, summary, content)
VALUES
('$_POST[title]','$_POST[summary]','$_POST[content]')";

if (!mysqli_query($db,$sql))
  {
  die('Error: ' . mysqli_error($db));
  }
echo "1 record added";

mysqli_close($db);

?>

content.php

<head> 

</head>
<body bgcolor="#225599" text="#ffffff">
<h3>


<?php
//connect to db
include "mysql.php";

$query = 'SELECT * FROM blogtable';
if(!$result = $db->query($query)){
    die('There was an error running the query [' . $db->error . ']');
}

// Printing results in HTML

while($row = $result->fetch_assoc()){
    echo $row['title'] . '
    <br /><br />';
    echo $row['summary'] . '

    <br /><br /><br />';
    echo $row['content'] . '

    <br /><br /><br />';
}
$result->free();
$db->close();
echo"radi!!!!!!";
?>
</form>
<h3>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: sorting data/articles by date and time

Post by Celauran »

Add an ORDER BY clause to your query.

Code: Select all

SELECT field1, field2 FROM tablename WHERE whatever ORDER BY createdate DESC
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: sorting data/articles by date and time

Post by priyankagound »

just add an extra ORDER BY clause:

SELECT ...
FROM status
ORDER BY `date` DESC, `time` DESC

Hope this helps.
Post Reply