Help With Php Site...

Soldato
Joined
11 Apr 2003
Posts
4,210
Location
Notts
Hi all, as most of you know my site is www.jcb33.co.uk Im just wondering if its possible to do the following, and if someone could help me with the impementation please!

Instead of posting the full blog post on the main page, I want to post, for example a 100 word preview of it, then have "... Click Here For Full Post" after it, with a hyperlink that will load the full post from the database into a page.

On that page I would also like it to have a comment section, where people can post scomments about the blog post.

Finaly I would like an update section on the tool bar that will display the latest 5 blog entries.

Thanks all :)
 
Code:
<?php

if(strlen($tmp) > 100)
 echo substr($tmp, 0, 100), '&hellip; <a href="full.php?id=blah" title="View post in full">Click Here For Full Post</a>';
else
 echo $tmp;

?>
Etc.

Edit for other questions: comments will require a slight overhaul, you'll need to make another database table with a key relative to the post so you can draw the relevant comments. The updates bar is simply a matter of selecting X amount of records from the database.
 
I'm not sure how much you know about php, but I'll presume you know how to use it and just need some pointers;

For a 100 word preview, instead you could use substr($string, 0, 600) for a 600 character preview. Could do this;

Code:
$textLen=strLen($text);
if($textLen>600){
    echo substr($text,0,600)." ...";
}else{
    echo $text;
}
echo " <a href=\"viewblog.php?blogid=".$entry['ID']."\">[View Full Blog]</a>";

Then you'd need each entry to link to a "viewblog" page which would load the full blog. You'd use $_GET['blog'] to get the blogs ID from the url.

For a comments section, you could just take the code from your guestbook, and add an "OnBlog" column which would contain the blogs ID. So in your comment database you'd have;

ID | Title | Content | PostedBy | Date | OnBlog

Then you can do something like "SELECT etc FROM table WHERE `OnBlog`=6" where 6 is the blogs ID.

To select the 5 latest blogs it'd simply be "SELECT `ID`,`Title` FROM `Blogs` ORDER BY `ID` DESC LIMIT 5" then you have the 5 blogs with the highest ID, presuming your ID field is on auto_increment :)


If you don't already know a bit about php/mysql, I suggest you work on & learn each problem individually :cool:
 
All of the above are suggesting to do a character limit, however this isn't what has been asked for. Some looking on php.net has shown a user submitted function to limit words in a string which works like a charm

Code:
<?php

function word_limiter($str, $limit = 100, $end_char = '…') {
   
    if (trim($str) == '')
        return $str;
   
    preg_match('/^\s*(?:\S+\s*){1,'. (int) $limit .'}/', $str, $matches);

    if (strlen($matches[0]) == strlen($str))
        $end_char = '';

    return rtrim($matches[0]) . $end_char;
}

$longString = "This is a fairly long string, well it isn't really but it is longer than the word limit which I've specified so can be used as a proof of concept.";

$wordLimit = 10;

if(str_word_count($longString) > $wordLimit){
	//need to trim this string a little before showing
	echo word_limiter($longString, $wordLimit);
}else{
	echo $longString;
}

?>


The function mentioned can be found at http://uk3.php.net/manual/en/function.str-word-count.php and with a suitable end being passed to the function as the third argument it would be easy to make a for full text click here type affair.

Hope that helps?
 
psyr33n said:
Code:
<?php

if(strlen($tmp) > 100)
 echo substr($tmp, 0, 100), '&hellip; <a href="full.php?id=blah" title="View post in full">Click Here For Full Post</a>';
else
 echo $tmp;

?>
Etc.

Edit for other questions: comments will require a slight overhaul, you'll need to make another database table with a key relative to the post so you can draw the relevant comments. The updates bar is simply a matter of selecting X amount of records from the database.

Thanks everyone, so far I have:

PHP:
    				<?php
					}
					else
					{
						// get all entries
						while($row = mysql_fetch_array($result))
					{
					// list() is a convenient way of assign a list of variables
					// from an array values 
					list($id, $title, $post, $date) = $row;

					
					// print out the info

					echo"
					<div id=\"title$id\"><h3><b>$title</b></h3></div>
					<div id=\"date$id\"><h3><b>Posted On: </b>$date</h3></div>";

					$textLen=strLen($post);
					if($textLen>600){
    						echo substr($post,0,600)." ...";
					}else{
    						echo "<div id=\"post$id\">$post</div>";
					}
				
					// the below code displays the full post

					$postLink = '';

						// this page's path
						$self     = $_SERVER['PHP_SELF'];
	
						// we save each link in this array
						$postLink = array();
						$post = $id;
	
						// create the link to browse from index to post $maxPost
							$postLink[] =  "<br/><br/><b>To Leave Comments, Or View The Full Post:</b> <a href=\"$self?post=$post\">[Click Here]</a>";
	
						// join all the link using implode() 
						$postLink = "Post : " . implode(' &raquo; ', $postLink);
						echo "<p><?=$postLink</p>";	
						echo "<br/><hr/><br/>";			
					
				?>

However, clicking the link simply reloads the index page, and not takes you to the post, what am I doing wrong?!
 
Last edited:
Whoops, didn't realise what the OP was after concerning limits.

Anyway, is the link taking you to the correct URI (i.e. with the GET variable) or is it just not showing?

P.S. echo "<p><?=$postLink</p>"; - first of all short tags = no, second of all no need to embed a PHP echo in an echo... just echo it (so it becomes echo "<p>$postLink</p>";
 
psyr33n said:
Whoops, didn't realise what the OP was after concerning limits.

Anyway, is the link taking you to the correct URI (i.e. with the GET variable) or is it just not showing?

P.S. echo "<p><?=$postLink</p>"; - first of all short tags = no, second of all no need to embed a PHP echo in an echo... just echo it (so it becomes echo "<p>$postLink</p>";
It takes me for example to:

http://jcb33.co.uk/index.php?post=24

But still displays the home page, im trying to open it in the same page as the original post, or is it better to open it in say "indexpost.php" If so, what code would I use to call it from the previouse page so it knows which entry it is trying to show?
 
you could can point it at the index page still. but to retrieve and display the whole article you would have to use something like....

Code:
$post = $_GET['post']
//use is_numeric to make sure no-one is trying to mess with our query....
if(is_numeric($post)) {
     $result = mysql_query("SELECT * FROM tbl WHERE post = '$post'");
     if(mysql_num_rows($result) > 0) {
         //display article
     } else {
        //post does not exist. display a message and/or redirect page
     }
} else {
    //display index page as normal
}
 
marc2003 said:
you could can point it at the index page still. but to retrieve and display the whole article you would have to use something like....

Code:
$post = $_GET['post']
//use is_numeric to make sure no-one is trying to mess with our query....
if(is_numeric($post)) {
     $result = mysql_query("SELECT * FROM tbl WHERE post = '$post'");
     if(mysql_num_rows($result) > 0) {
         //display article
     } else {
        //post does not exist. display a message and/or redirect page
     }
} else {
    //display index page as normal
}
Thanks have tried to intergrate that, but not much luck so far. Will it be possible with this method to have the comment section apear as well when you click the link? Don't want to create a new page for every blog post :)
 
jcb33 said:
Don't want to create a new page for every blog post :)

well of course. that's the whole reasoning behind you using php/mysql.... :confused:

you need to think about what you're doing. not just the php/mysql but actually plan what you want to do with the page. how many different views are you going to use? showing summary, showing whole posts, viewing comments, adding comments.... if you find things getting a little complicated maybe thinkabout breaking it down into pages. it's all upto you. just sit down and think about it......

.....or just ditch the lot and install a cms/blog? :D
 
marc2003 said:
well of course. that's the whole reasoning behind you using php/mysql.... :confused:

you need to think about what you're doing. not just the php/mysql but actually plan what you want to do with the page. how many different views are you going to use? showing summary, showing whole posts, viewing comments, adding comments.... if you find things getting a little complicated maybe thinkabout breaking it down into pages. it's all upto you. just sit down and think about it......

.....or just ditch the lot and install a cms/blog? :D

Well basicaly I want:

Index Page, Blog preview upto 100 words.
Full blog view + comment posting + posted comments.

and nah, not giving up to use ready made stuff :D
 
that's all you want yet you can't use what i posted above? :confused:

you would structure the page something like this...

Code:
<?php
if($_POST['submit']) {
      //process comments and redirect to specific post 
}
?>
<html>
<head></head>
<body>
<?php
$post = $_GET['post']
//use is_numeric to make sure no-one is trying to mess with our query....
if(is_numeric($post)) {
     $result = mysql_query("SELECT * FROM tbl WHERE post = '$post'");
     if(mysql_num_rows($result) == 1) {
         //display full article
         //display form for people to add comments with a button named submit - we process that at the top of the page if clicked
         //display comments
     } else {
        //post does not exist. redirect to main index.php
     }
} else {
    //display post previews. maybe add some pagination into the mix if you have enough articles.
}
?>
</body>
</html>
 
As It is, im quite new to php, what I have so far is with help, I am learning slowly, but not enough to actualy do what I want yet, other than secure guestbooks and such from hacking :(
 
marc2003 said:
ah right. i thought you had everything done but just needed help incorporating it all into one page? :p
hehe not quite, I have the whole, displaying of posts working fine, as well as a comment system which I think im able to incorperate into posts, and the limiting of the word count + link to full post working.

But when I click the link to full post, while it gives the correct url, it just reloads the index page.....

I see what I need to do from your code, its basicaly:
If a specific blog is selected, load said blog + load commenting box + load comments.
Else load index page.

But I need to get the code working that makes it so when you click the link it loads the full blog instead of the index page, and loads the commenting boxes etc...

If you can understand that? I sure confused myself :p
 
jcb33 said:
But I need to get the code working that makes it so when you click the link it loads the full blog instead of the index page, and loads the commenting boxes etc...

If you can understand that? I sure confused myself :p

that's exactly what i've posted does - using the if...else...... :confused:

obviously your implementation is a little lacking. :D
 
marc2003 said:
that's exactly what i've posted does - using the if...else...... :confused:

obviously your implementation is a little lacking. :D
Yes :p I basicaly suck, I have tried several times to implementing it but I get error messages, so... *Shrugs* Does not look like im able to make it work :(
 
well errors are obviously just little typos you've made somewhere? i'm a complete php noob myself but you soon pick up what causes some errors.

you can post what you have so far and i'll take a look if you like. :)
 
marc2003 said:
well errors are obviously just little typos you've made somewhere? i'm a complete php noob myself but you soon pick up what causes some errors.

you can post what you have so far and i'll take a look if you like. :)
Thanks, will redo it as I deleted the edit *Hits self on head* then post in a bit, just going to play Basketball so will be back later, thanks for all the help your giving me btw :)
 
Back
Top Bottom