if statement to execute when a link is clicked

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
angrypenguin
Forum Newbie
Posts: 5
Joined: Sun Dec 03, 2006 3:12 pm

if statement to execute when a link is clicked

Post by angrypenguin »

Hi all, I'm trying to create a kind of drop-down section added to one of my blogs. I want it so that people can click on the 'COMMENTS' link and the code inside an if statement will be executed to display comments from my database. Does anyone know how to do this?

Thanks very much
Andy
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I don't understand what you want it do. Please add more explanation and some code.
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

you make a link after each blog-story like this:

<a href='comments.php?blog=$blog'>View comments of this story</a>

where $blog has to be the id of the blog story.
on the comments page you make a mysql query:

SELECT * FROM comments WHERE blog_id = '$blog'

here the $blog is the $_GET['blog'] from your url.

hope that can help you...
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Are you looking for the code to be executed without another page load? If so, that means you'll have to use some sort of JavaScript.

Possibly something like:

Code: Select all

<?php
// Whatever code you have

echo '<a href="blah.com/" onclick="someFunction()">';
// Not tested at all.  Just a general concept.
?>
And use the JS function to call info from the DB and echo it to the page.

klarinetking
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Bevibed.be wrote:you make a link after each blog-story like this:

<a href='comments.php?blog=$blog'>View comments of this story</a>

where $blog has to be the id of the blog story.
on the comments page you make a mysql query:

SELECT * FROM comments WHERE blog_id = '$blog'

here the $blog is the $_GET['blog'] from your url.

hope that can help you...
As I understand what you want to do, you have it right there. On the first page, the viewer clicks on the link and the new page is generated by comments.php. What's your question?

Also, what does an if statement have to do with that?
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Hi,

Note that the first method posted will mean that users will be forced to go to another page to view comments. If you use JavaScript, the results can just appear when users click a link (kind of like GMail). It will take a lot more work, and will be more complicated than the first method. It is also possible to disable JavaScript entirely as well. In the end, it's your choice on how you want the feature to work.

klarinetking
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

klarinetking wrote:Hi,

Note that the first method posted will mean that users will be forced to go to another page to view comments. If you use JavaScript, the results can just appear when users click a link (kind of like GMail). It will take a lot more work, and will be more complicated than the first method. It is also possible to disable JavaScript entirely as well. In the end, it's your choice on how you want the feature to work.

klarinetking
GMail uses AJAX http://developer.mozilla.org/en/docs/AJ ... ng_Started to connect to the server, get information from the DB and display it.
angrypenguin
Forum Newbie
Posts: 5
Joined: Sun Dec 03, 2006 3:12 pm

Post by angrypenguin »

wow, that's a hell of a lot to think about - thanks everyone. I really like the idea of using AJAX/javascript and I think that's the best way to go for what I want to achieve. Does anyone have a book on Javascript?... ;)

Andy
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You can learn from the web:
http://www.w3schools.com/js/default.asp - JavaScript tutorial
http://developer.mozilla.org/en/docs/JavaScript - JS basics
http://developer.mozilla.org/en/docs/AJ ... ng_Started - AJAX basics
http://script.aculo.us - AJAX framework
http://prototype.conio.net/ - Another AJAX framework
http://ajaxpatterns.org/PHP_Ajax_Frameworks - Lots of AJAX framework
viewtopic.php?t=54934 - This topic discusses AJAX frameworks
Post Reply