[Solved] Help in displaying blog posts [PHP and MySQL]
Posted: Tue Oct 06, 2009 10:18 am
Hello 
I am somewhat new to PHP and MySQL so clearly I don't know all the commands and stuff by memory...
So my problem starts with that I made myself a simple blog, or something of that sort.
It was just a test of how MySQL and PHP work together, and it came out in a form of some kind of Guest Book.
But later on I ported it to my main site in the form of a Blog... but the list Descends and not Ascends, like this:
Post1
Text
Post2
Text
Post3
Text
Post4
Text
Which by how MySQL works means that the latest post is the bottom one, and the oldest is the first...
I want it to be Latest first and so on, I looked over the internet for a few hours, and didn't find anything of use...
?--------------?
My second problem is that if I input a text like this for example:
Hello! this
is
a
test
text.
It will post it without the newlines, like this [In the MySQL table it looks just as I imported it, so it has something to do with PHP or HTML]:
Hello! this is a test text.
But its not my major problem at the moment, I probably could find a solution, I just didn't look deep enough into it, I think I could like write some kind of script to add <br> after every dot or something else... though its totally inefficient, that's ok for the "beta"s of my site "^^
Oh, almost forgot ">_<
The code [only the blog part of course, if it looks familiar it should, I used a template ATM so I won't have too much stuff on my head like design
]
[Part of Index.php]
I don't think its important, but:
[The post composing page post.php]
Notes:
I am somewhat new to PHP and MySQL so clearly I don't know all the commands and stuff by memory...
So my problem starts with that I made myself a simple blog, or something of that sort.
It was just a test of how MySQL and PHP work together, and it came out in a form of some kind of Guest Book.
But later on I ported it to my main site in the form of a Blog... but the list Descends and not Ascends, like this:
Post1
Text
Post2
Text
Post3
Text
Post4
Text
Which by how MySQL works means that the latest post is the bottom one, and the oldest is the first...
I want it to be Latest first and so on, I looked over the internet for a few hours, and didn't find anything of use...
?--------------?
My second problem is that if I input a text like this for example:
Hello! this
is
a
test
text.
It will post it without the newlines, like this [In the MySQL table it looks just as I imported it, so it has something to do with PHP or HTML]:
Hello! this is a test text.
But its not my major problem at the moment, I probably could find a solution, I just didn't look deep enough into it, I think I could like write some kind of script to add <br> after every dot or something else... though its totally inefficient, that's ok for the "beta"s of my site "^^
Oh, almost forgot ">_<
The code [only the blog part of course, if it looks familiar it should, I used a template ATM so I won't have too much stuff on my head like design
[Part of Index.php]
Code: Select all
<div id="middle">
<?php
//INCLUDES
require "includes/sql_connect.php"; // PHP Database and Server connection function
sql_connect();
$sql_qrry = mysql_query("SELECT * FROM blog_posts") or die (errorMessage(3));
$sql_row = mysql_fetch_array($sql_qrry);
$num = mysql_num_rows($sql_qrry);
while ($num>0)
{
if ($num>4)
{
$sql_row = mysql_fetch_array($sql_qrry);
$num--;
}
else
{
echo ("<div class=\"post\">");
echo ("<div class=\"postheader\"><h1>" . $sql_row['post_title'] . "</h1></div>");
echo ("<div class=\"postcontent\"><p>" . $sql_row['post_text'] . "</p></div>");
echo ("<div class=\"postfooter\"></div>");
echo ("</div>");
$sql_row = mysql_fetch_array($sql_qrry);
$num--;
}
}
?>
</div>[The post composing page post.php]
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href="style.css" rel="stylesheet" type="text/css" /><title>Untitled Document</title></head><body><div id="middle"> <br /> <div class="post"> <div class="postheader"><h2> Leave us a message <!-- s:D --><img src=\"{SMILIES_PATH}/icon_biggrin.gif\" alt=\":D\" title=\"Very Happy\" /><!-- s:D --> </h2></div> <div class="postform"> <form enctype="multipart/form-data" action="post.php" method="post"> Title: <br> <input name="post_title" type="text" size="33" maxlength="33" /> <br> <br> Post: <br> <textarea rows="10" cols="30" name="post_text"></textarea> <br> <br> <input type="submit" value="Send" name="submit" /> </form> <?php //INCLUDES require "includes/sql_connect.php"; // PHP Database and Server connection function sql_connect(); if(isset($_POST['submit'])) { $sql_qrry = mysql_query("INSERT INTO `blog_posts` (`author_id`, `post_date`, `post_title`, `post_text`) VALUES ('0', CURDATE(), '" . $_POST[post_title] . "', '" . $_POST[post_text] . "');") or die (errorMessage(3)); mysql_close($sql_con); header( 'refresh: 3; url=/' ); echo '<p1>Post added, Sending you back to home page in 3 seconds...</p>'; } ?> </div> <div class="postfooter"></div> </div></div></body></html>Notes:
- sql_connect() is a function defined in sql_connect.php which is included at the start of every .php file that uses MySQL
- errorMessage(#) is a function I wrote to custom handle errors. its included in sql_connect.php because it uses it too, so I don't reinclude it, though I check if its already included incase its not "^^
- I got this:
I have no clue what this does, but it comes with new files in my Dreamweaver CS4, so I guess its important somehow...
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Sorry, I am not much into clean and efficient code at the moment, so I don't pay much of an attention to it "^^
I will read about it when I will have the time