Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
help with some blog code (php)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Sun Aug 29, 2004 11:51 pm    Post subject: help with some blog code (php) Reply with quote

Basically what I want to do is set up so that the main content box @ http://shadowprofiles.net has a summary (like the first paragraph and ending in '...') of a blog entry I'd make. The full thing would be found @ http://shadowprofiles.net/root.php .

I know a little php, so I'm having a hard time comming up with an answer myself. Hell I'm having a hard time setting up a blog script to begin with >< . I can't seem to put it together in my head. I just need the basics for both the scripts (the summary to go in the main domain page and the full blog entry being posted). I do have access to 1 mysql database so if it would require the use of that its possable. Also a method to put the content into the blog via an input box on a seperate page.

Once I have the basics of those scripts I could hack in any other features I'd want, so basically just need a simple basic blog posting engine and a way to pull a limited amount of characters to display on the home page with a '...' at the end :) . Sorry for begging ,but I've been trying to do this thing for weeks and I just can't seem to get it together.
Back to top
View user's profile Send private message
oniq
Guru
Guru


Joined: 02 Sep 2002
Posts: 597
Location: Connecticut

PostPosted: Mon Aug 30, 2004 12:39 am    Post subject: Reply with quote

Why not use some of the already created blog posting programs such as Wordpress.

If you're really set on creating it yourself, the easiest way is combining PHP+MySQL. As for the summary (first X characters), look into the function substr() for PHP.

You'd need to learn the basics of MySQL, and I recommend just looking at the manual at mysql.com. Real simple commands that can be learned quickly.

For instance, you could set up a table called 'blog', with certain columns:
date | title | body | category

Then you can easily pull the data from your MySQL database after connecting with something like:

$result = mysql_query("SELECT * FROM blog ORDER BY date DESC",$db);

Next you need to loop through the blog entries to display them (pulling them into an array called entry):

while ($entry=mysql_fetch_array($result)) {

}

Within that loop you can use entry['date'], entry['title'], etc. And if you only want to display a portion of the body, as a summary, you'd use substr(entry['body'], 0, 50).

This should give you a basic idea of the setup, look into html FORMs on how to input the data into your database, as well as MySQL command INSERT.
_________________
open like a child's mind.
Back to top
View user's profile Send private message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Mon Aug 30, 2004 12:59 am    Post subject: Reply with quote

Thanks alot. Pretty much what I was looking for to help me out :) .

As for not using preexisting bloggers, I already am using wordpress, but I don't like not knowing :) . Sure I could read the code, but wordpress is full of a ton of features I don't use and thus has a whole lot of code to manually parse through. With my limited knowledge I'd just get more confused :? . Given a few basic steps and examples helps understand and adapt better to my wants.
Back to top
View user's profile Send private message
Roguelazer
Veteran
Veteran


Joined: 10 Feb 2003
Posts: 1233
Location: San Francisco, CA

PostPosted: Mon Aug 30, 2004 1:08 am    Post subject: Reply with quote

Use BLOG:CMS if you really want a nice blog. But if you're intent on doing it yourself, the php looks like this:

Code:

// Set $text equal to the content of the item up here and $id to the item's id
if (strlen($text) <= 200) //This checks for little entries, which don't need to be cut
{
    $newtext = substr($text, 0, 200) . "...<br /><a href=\"index.php?viewarticle=$id\">View the whole article</a>";
}


Here's an editted example from my own news display function (editted for all-in-oneness, of course):

Code:

$sql = new sqlconn();
$sql->connect();
$news = $sql->query("SELECT id, text, date FROM news ORDER BY date DESC");
while ($newsrow = mysql_fetch_assoc($news))
{
    $id = $newsrow['id'];
    $text = $newsrow['text'];
    $date = $newsrow['date'];
    if ($strlen($text) < 200)
    {
        $newtext = substr($text, 0, 200) . "... <br /><a href=\"index.php?action=viewnews&amp;nid=$id\">View the Whole Article</a>\n";
    }
    // Formatting and printing stuff goes here!
}


Of course, all of the html -should- be in a nice template file. But I'm much too lazy to do that sort of thing. 'specially since I don't have or plan to have multi-languagability or anything.
_________________
Registered Linux User #263260
Back to top
View user's profile Send private message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Mon Aug 30, 2004 1:24 am    Post subject: Reply with quote

For the $date variable and thus the data entered into the date column, would it work to use php's date function so the date could be formatted the way I would like and still be able to use it as a form of reference for traversing the data?
Ex:
Code:

//Prints something like: Mon 15 Jan 2003 05:51:38 (24 hour)
echo date("l d M Y H:i:s ");
Back to top
View user's profile Send private message
Roguelazer
Veteran
Veteran


Joined: 10 Feb 2003
Posts: 1233
Location: San Francisco, CA

PostPosted: Mon Aug 30, 2004 1:38 am    Post subject: Reply with quote

You'd need to get it in a somewhat roundabout method, though, since PHP returns data as strings, not as dates. Something like this probably:

Code:

// Stuff goes here. :p
while ($newsrow = mysql_fetch_assoc($news))
{
    // cut
    $tempdate = $newsrow['date'];
    $date = date("l d M Y H:i:s ", strtotime("$tempdate"));
    //cut
}


Basically, you use strtotime to convert the string from mySQL (or PostgreSQL or whatever) to a UNIX timestamp, then you use date() to format that timestamp. Use this manual entry for guidance.
_________________
Registered Linux User #263260
Back to top
View user's profile Send private message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Mon Aug 30, 2004 1:54 am    Post subject: Reply with quote

Excellent, thanks. I'm trying to disect the data from your posts to come up with a simple break down of the script, will post it in a little bit to ask for critique on it :) .

[ edit ]

So far this is my basic breakdown of the script and how it'll run:
Code:

set a table called blog
columns:
date | title | body

connect to database:
$sqldb = mysql_connect('localhost', 'mysql_user', 'mysql_password');

check if connection was successful:
if (!$sqldb) {
   die('Unable to connect to database: ' . mysql_error());
}

pull results:
$blg_post = mysql_query("SELECT * FROM blog ORDER BY date DESC",$db);
if (!$blg_post) {
   die('Invalid query: ' . mysql_error());
}

traverse and parse the db entries:
while ($crnt_row = mysql_fetch_assoc($blg_post))
{
    $id = $crnt_row['id'];
    $content = $crnt_row['body'];
    $date = $crnt_row['date'];
    if ($strlen($content) < 200)
        {
   $summary_txt = substr($content, 0, 200) . "... <a href=\"root.php\">read more</a>\n";
        }
}


I'm going to leave out the date formatting for now until I get the basic structure running properly. I'm digging to find out how to give the data to the database through a form right now. I now how to make the form and how to have an action acted upon the data, but not what to put in the php script to have that action be putting the data into the appropiate fields, digging through that now :) . any crits on the flow of it so far or any mistakes?
Back to top
View user's profile Send private message
Roguelazer
Veteran
Veteran


Joined: 10 Feb 2003
Posts: 1233
Location: San Francisco, CA

PostPosted: Mon Aug 30, 2004 11:22 am    Post subject: Reply with quote

Instead of this:

Code:
connect to database:
$sqldb = mysql_connect('localhost', 'mysql_user', 'mysql_password');

check if connection was successful:
if (!$sqldb) {
   die('Unable to connect to database: ' . mysql_error());
}


try this:
Code:

//connect to database:
$sqldb = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Unable to connect to database; ' . mysql_error());


It's smaller and stuff. Really, you should have the sqldb in a try statement and the error handler in a catch statement. But that might be overkill. :P
Code:

try
{
    $sqldb = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    $blg_post = mysql_query("SELECT * FROM blog ORDER BY date DESC",$db);
}
catch
{
    die('MySQL code failed to execute. Error: ' . mysql_error());
}

That's an approximation, because I don't have my code in front of me right now and I don't use try...catch very often.

And if you really want to make it well, make a database abstraction class like I did. Mine allows only mysql databases so far, but adding another is easy. The class should do things such that calling its connect member, for instance, can read login information from a file (xml?), call the appropriate connection script, and return a connection variable thingy. It should also have query and fetch functions so that instead of mysql_query you can use $yourclass->query() and instead of mysql_fetch_assoc() you can use $yourclass->fetch_assoc(). Or maybe I should just give you a copy of my class. :P
_________________
Registered Linux User #263260
Back to top
View user's profile Send private message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Mon Aug 30, 2004 6:51 pm    Post subject: Reply with quote

Lol yar. A copy of your class would be appreciated ^^ . I'm still to new to it all to make a functioning class like you've described without breaking something or other :roll: ...

Thanks for all the help.
Back to top
View user's profile Send private message
Roguelazer
Veteran
Veteran


Joined: 10 Feb 2003
Posts: 1233
Location: San Francisco, CA

PostPosted: Mon Aug 30, 2004 7:14 pm    Post subject: Reply with quote

Okey. I gotta tidy it up a bit, remove anything that might be annoying and confusing. :p You'll hear from me.
_________________
Registered Linux User #263260
Back to top
View user's profile Send private message
Saa
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138

PostPosted: Tue Aug 31, 2004 3:54 am    Post subject: Reply with quote

Thanks again.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum