- This topic is empty.
-
AuthorPosts
-
November 18, 2010 at 7:23 am #30732
biscutty
ParticipantI’ve been trying to implement the ID the Body Based on URL wordprsess snippet found here Your text to link…
First issue seems to be a syntax error
function curr_virtdir($echo=true){
$url = explode('/',$_SERVER);
$dir = $url[1] ? $url[1] : 'home'; // defaults to this if in the root
$dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
if ($echo) echo $dir;
return echo $dir; // ie. curr_virtdir(false)
}
function get_curr_virtdir(){
curr_virtdir(false);
}I Get a syntax error on this line below
return echo $dir; // ie. curr_virtdir(false)
If i remove this line completly the script seems to work for in the following situation
although I am trying to use the same functions as a conditional for my hardcoded navigation but it doesnt seem to work – any ideas?
- ">Home
- ">Solutions
- ">About us
November 23, 2010 at 5:52 am #72560Rob MacKay
Participantyea you need to either return OR echo, you can’t return echo $dir.
Return is used in functions to pass a value back – but dosen’t need to echo it.
For example to display the text “Garry”:
function name() {
$e = "Garry";
return $e;
}
echo name(); //this would NEED echo because the return dosen't echo.
or
function name() {
$e = "Garry";
echo $e;
}
name(); //this would NOT need echo because the echo is already in the function.
The best practice is to return the value and then echo it out.
It might work after that because you are actually returning a value, where as before you were not.
Does that make sense?
November 26, 2010 at 4:42 am #72232biscutty
Participantthanks Robskiwarrior – i think I am getting it now!
November 26, 2010 at 4:44 am #72222Rob MacKay
ParticipantNo probs – it gets easier once your brain get’s into it :)
November 26, 2010 at 6:24 am #72173biscutty
Participantalthough, for the life of me I cant work out why the following doesnt work
essentially, what im trying to say is if the value is not one of these, home, solutions, about-us, contact then the value should be ‘navnewscurrent’ – i think the OR part of my code might be giving the problem, as the following words a treat?
November 26, 2010 at 7:53 am #72174Rob MacKay
ParticipantUsing OR statement there resets to a new instance of something for it to look at. So what you are saying is if get_curr_virtdir() == home or if solutions or if about-us or if partners .
Instead you should be asking the comparison over and over.
if get_curr_virtdir() == home or if get_curr_virtdir() == if solutions etc etc
|| get_curr_virtdir() == "solutions"
|| get_curr_virtdir() == "about-us"
|| get_curr_virtdir() == "partners"
|| get_curr_virtdir() == "contact-us" )
{
echo "navnews";
} else {
echo "navnewscurrent";
}?>
The reason it does that, although it seems long winded is what if you had another comparison?
get_curr_virtdir() == “home” || $garry == “Your Dad” || ….
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.