Page 1 of 1

New to php and need help on first function ever!

Posted: Tue Oct 28, 2008 11:52 am
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.

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

Posted: Tue Oct 28, 2008 4:39 pm
by gmonk
any help would be awesome on this! :D

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

Posted: Wed Oct 29, 2008 10:39 pm
by gmonk
No one can help me? This is sad :/

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

Posted: Wed Oct 29, 2008 11:20 pm
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

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

Posted: Thu Oct 30, 2008 10:05 am
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.