/    Sign up×
Community /Pin to ProfileBookmark

simple if then else problem .. i hope!

I have a blog and want to solve the following issue. I am sure its quite basic but my php knowledge is lacking

I want the following html code to be written

<span class=”commentauthor”> <a href=”author’s url”>author’s name</a></span>

if comment_author_url() is not null and

<span class=”commentauthor”> author’s name </span>

if comment_author_url() is null.

How would i achieve that?

thanks

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ixxalnxxiAug 28.2007 — i was wondering if you can go into detail by explaining what you're looking for and what do you wish to achieve after finding the different scenarios.
Copy linkTweet thisAlerts:
@artemisAug 29.2007 — Something like..

[code=php]
if(!empty(comment_author_url()){
echo '<span class="commentauthor"> <a href="'.comment_author_url().'">author's name</a></span>';
}else{
echo '<span class="commentauthor"> author's name </span>';
}[/code]

?
Copy linkTweet thisAlerts:
@andy_bauthorAug 29.2007 — To answer ixxalnxxi, on my blog there is a page which displays the post and then all of the comments. here is the site for reference www.bellophoto.net

Go to view comments on any post.

You will see a list of names of the people who have made comments. Now when people enter a comment they have to enter their name and a comment. But they have the option to enter a website address too!

Now if someone enters an address i would like their name to link to the address. If that field is left blank - no address entered i just want the name to be listed without a link.

hope that clears it up a little.

Thanks artemis for the code. Couldn't get it to work. Is that valid php? How would the browser know that the if statement was code rather than just text to display?

cheers

Andy
Copy linkTweet thisAlerts:
@andy_bauthorAug 29.2007 — so i added this to my script

<?php
if(!empty(comment_author_url()))
echo '<span class="commentauthor"> <a href="comment_author_url()"> comment_author() </a></span>';
else
echo '<span class="commentauthor"> comment_author()</span>';
?>


but i get a parse error message:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'

Any ideas?

Andy
Copy linkTweet thisAlerts:
@artemisAug 29.2007 — Okay well i was just throwing out ideas to get your mind working, this code will do it:

[code=php]
function comment_author(){
return 'Robert';
}
function comment_author_url(){
return 'http://www.google.com';
}

$url=comment_author_url();
if(!empty($url))
echo '<span class="commentauthor"> <a href="'.$url.'">'.comment_author().'</a></span>';
else
echo '<span class="commentauthor">'.comment_author().'</span>';[/code]
Copy linkTweet thisAlerts:
@andy_bauthorAug 30.2007 — i can not get this to work.

This script is to be used to create a comments page in a blog.

When a user leaves a comment on my blog they have the option to also leave a website address also. I am trying to link this address to the commenters name.

When the user chooses to leave a comment a form is created which has these 2 input boxes relating to the users name and url:

<input type="text" name="author" id="author" class="textarea" value="<?php echo $comment_author; ?>" size="28" tabindex="1" />

and

<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="28" tabindex="3" />


Now i added the following code

<?php
$urlentered = comment_author_url();
if (!empty($urlentered))
echo '<span class="commentauthor"><a href="',comment_author_url(),'" target="_blank">', comment_author(), ' </a></span>';
else
echo '<span class="commentauthor">', comment_author(), ' </span>';
?>


in a page to display the comments . this piece of code appears in a loop.

The resulting html page has the url printed before the span tag!!!


Sorry for not explaining things better.

Does this help?

thanks again
Copy linkTweet thisAlerts:
@artemisAug 30.2007 — Do you have a link to the page in question?

Are you sure that you should be calling the function comment_author(); and not using the variable $comment_author as well?
Copy linkTweet thisAlerts:
@andy_bauthorAug 30.2007 — no, i am not sure.

However i tried

<?php echo $comment_author; ?>

in the loop and nothing was printed

but if i type <?php echo comment_author(); ?>

then the commenter's name is displayed

link http://bellophoto.net/2007/08/20/nicaragua-a-well-deserved-break/#comments
Copy linkTweet thisAlerts:
@andy_bauthorSep 04.2007 — any more comments anyone?

I am still stuck!
Copy linkTweet thisAlerts:
@andy_bauthorSep 05.2007 — ok so i solved it

turns out there is a built in function that does exactly this

comment_author_link()

sorry for wasting time
×

Success!

Help @andy_b spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...