开发者

Using PHP to display button with hyperlink or greyed out button if no URL in database

开发者 https://www.devze.com 2022-12-26 03:45 出处:网络
I\'ve got a webpage that I\'m working on where you click on a letter or category and it displays records matching that query from a database.One of the things I want to display is a hyperlinked button

I've got a webpage that I'm working on where you click on a letter or category and it displays records matching that query from a database. One of the things I want to display is a hyperlinked button that says "Website" if the database record contains a URL in the 'URL' field, and if there is no value in that field, it will display a greyed out version of that button.

I tried using an if...else statement, but was absolutely unable to get the syntax correct trying to get php to call up the 'URL' value in the middle of an "echo "

So here's what I did:

<?php if($row_rsmemalpha['URL'] != NULL) ?><a
 href="http://<?php echo ($row_rsmema开发者_开发百科lpha['URL']);?>"><target
 ="_blank"><img src="web_button_on.gif"
 alt="Website" border="0" height="18" width="103" /></target></a>

<?php if($row_rsmemalpha['URL'] == NULL)
echo "<img src=\"web_button_off.gif\" alt=\"No Website Available\" height=\"18\" width=\"103\" />";
?>

If there is a URL available it shows the button properly. But if there isn't a URL in the database it shows both buttons.

I have spent a few days studying examples and tutorials on the web, but haven't found too much that helps. The buttons were completely non-functional when I started, so I'm pretty proud of getting this far with it! I've just run out of time and patience for more trial-and-error experimenting.

Any help is appreciated...

Diane


I'm not entirely sure about how PHP handles IF statements when going out of the PHP mode, but essentially your problem is the lack of curly braces. Both buttons are outputted because PHP doesn't recognize the exit from PHP mode as a statement that's related to the IF clause. Just wrap the contents of the if clause into curly braces and your code should work fine.

For example, this should work:

<?php
if($row_rsmemalpha['URL'] != NULL)
{
?><a
 href="http://<?php echo ($row_rsmemalpha['URL']);?>"><target
 ="_blank"><img src="web_button_on.gif"
 alt="Website" border="0" height="18" width="103" /></target></a>

<?php
}
if($row_rsmemalpha['URL'] == NULL)
{
    echo "<img src=\"web_button_off.gif\" alt=\"No Website Available\" height=\"18\" width=\"103\" />";
}
?>


I would recommend using empty() and !empty() for your checks.

if(!empty($row_rsmemalpha['URL'])) { /* etc */


Try something like:

    <?php
    if($row_rsemalpha['URL'] != NULL){
        $buttonCode = '<a href="http://'.$row_rsemalpha['URL'].'" target ="_blank"><img src="web_button_on.gif" alt="Website" border="0" height="18" width="103"></a>';  
}else{
    $buttonCode = '<img src="web_button_off.gif" alt="No Website Available" height="18" width="103"/>';
}
echo $buttonCode;
?>


Your logic is ok, I think it's just written sloppy. I recommend you try to write your code a little cleaner and you will run into way fewer problems. This is your code cleaned up:

$url = $row_rsmemalpha['URL'];

if($url != NULL){
    echo '<a href="http://'.$url.'">
            <target ="_blank"><img src="web_button_on.gif" alt="Website" border="0" height="18" width="103" />
            </target>
        </a>';
}

if($url == NULL){
    echo '
        <img src="web_button_off.gif" alt="No Website Available" height="18" width="103" />';
}

Now you can see that this should be an if/else, to make sure that only one of them gets output, and then you only have to fiddle with one test to see if the url is there.


here is my approach :)

<?php if($row_rsmemalpha['URL']): ?>
  <a href="http://<?php echo ($row_rsmemalpha['URL']);?>" target ="_blank">
    <img src="web_button_on.gif" alt="Website" border="0" height="18" width="103"/>
  </a>
<?php else: ?>
  <img src="web_button_off.gif" alt="No Website Available" height="18" width="103" />";
<?php endif ?>

I'd recommend to shorten $row_rsmemalpha variable name and use short open tags to make this template look real neat:

<? if($row['URL']): ?>
  <a href="<?=$row['URL']);?>" target ="_blank">
    <img src="web_button_on.gif" alt="Website" border="0" height="18" width="103"/>
  </a>
<? else: ?>
  <img src="web_button_off.gif" alt="No Website Available" height="18" width="103" />";
<? endif ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消