New to php and need help on first function ever!

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
gmonk
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 11:46 am

New to php and need help on first function ever!

Post by gmonk »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


So i'm trying to construct a function that does two things. Basically i'm trying to streamline some code that worked earlier but made the code too long and made me feel weird. I thought i'd try to slim it down a little bit. I'll paste the function here and describe what i'm trying to do with it after the paste. Unless you can tell what i'm trying to do w/o the explanation:

Code: Select all

<?php
    $currentpage = basename($_SERVER['SCRIPT_NAME']);
    $selected = "<span style=\"color: #a8c26e;\">";
                
    if ($currentpage) {
        echo $selected;
    }
?>

Code: Select all

<a href="index.php" <?php $currentpage == ('index.php'); ?>>Home</a>
I'm trying to get the script to check for the page you're on, then i'm trying to color the link a hue of green. For the most part it works, but i somehow have to end that span class after the "home" link. Any help would be superly wonderful.


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
gmonk
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 11:46 am

Re: New to php and need help on first function ever!

Post by gmonk »

any help would be awesome on this! :D
gmonk
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 11:46 am

Re: New to php and need help on first function ever!

Post by gmonk »

No one can help me? This is sad :/
aaj
Forum Newbie
Posts: 4
Joined: Wed Oct 29, 2008 10:20 pm

Re: New to php and need help on first function ever!

Post by aaj »

I'm a tad unclear as to what $selected even does, other than display out a half constructed span tag when $_SERVER['SCRIPT_NAME'] isset (which is almost always bound to be always).

What you have for your a tag doesn't render anything in the PHP part, it just performs a boolean check on currentpage and index.php and ends, there is no echo to the browser of any results. anything between <?php and ?> will never push anything down to the browser unless you use one of the built in statements to get the output there (echo, print etc.)

Other than that, what I think you are trying to accomplish would be better written something like this:

Code: Select all

 
<?php
$currentpage = basename($_SERVER['SCRIPT_NAME']);
 
if ($currentpage == 'index.php') {
   $selected = 'style="color: #a8c26e;"';
}
?>
 
<a href="index.php" <?php echo $selected; ?>>Home</a>
 
Hope that helps,
Aaj
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: New to php and need help on first function ever!

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply