homepage Welcome to WebmasterWorld Guest from 23.22.217.122
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP within PHP
calling php when already within php tags
Sofia_A




msg:4230262
 8:28 pm on Nov 14, 2010 (gmt 0)

Hi,

I've been tearing my hair out trying to make this work.

I have a menu list in a php which is called up by the header.

The list is simple and looks like this:

<li class="artist"><a href="artist.php" ></a></li>

and I want to add this:

<?php if ( $thisPage == "Artist" ) {
echo '<img src="/images/50.png" width="50" height="20" />' ;
}
?>

to the href.

Both sets of code work on their own, but obviously I can't just add the second lines of code to the first because it's already within the php tags.

I know not to use echo and the <?php and ?>, but there's more to it and I can't work it out for myself. Something to do with .'?

Please help before I have no hair left. Thanks :)

 

Matthew1980




msg:4230269
 9:00 pm on Nov 14, 2010 (gmt 0)

Hi there Sofia_A,

Welcome to WebmasterWorld firstly!

<?php
if($thisPage == "Artist") {
echo '<img src="/images/50.png" width="50" height="20" />';
}
?>

I might not have understood you correctly, are you trying to merge the two sets of code there? This little excerpt that you have provided will work fine, so long as your variable is equal to 'Artist'.

Could you explain/clarify your dilemma in a little more detail so that we can help you.

[EDIT] Are you just wanting to have it so that the image is the actual link? And only of the page you are on is relevant to the link? Are you using query string for the pages? Sorry for the questions, I'm just struggling to see the issue here ;-p

Cheers,
MRb

Sofia_A




msg:4230273
 9:22 pm on Nov 14, 2010 (gmt 0)

Thanks for the welcome. I'm a noob to php too. Getting my head around it generally.

Okay.

I call up my menu in my header thus:

<?php include("menu3.php"); ?>


then in my menu.php I have this:



<ul class="cssmenu">
<li class="artist"><a href="artist.php" ></a></li>
<li class="writer"><a href="writer.php"></a></li>
<li class="musician"><a href="musician.php"></a></li>

</ul>


I want to insert

<?php if ( $thisPage == "Artist" ) {
echo '<img src="/images/50.png" width="50" height="20" />' ;
}
?>

into each line ($thisPage will vary to 'writer' and 'musician' etc...) so that when the menu is being displayed on the page in question an image is shown (different for each line) instead of the 'a' or 'a:hover' images.

Is this clearer?

ie my thought was to do this:

<li class="artist"><a href="artist.php" <?php if ( $thisPage == "Artist" ) {echo '<img src="/images/50.png" width="50" height="20"/>';}?> ></a></li>

But I already know that won't work because I'm within a php call already.

Clear as mud, right? ;)

Matthew1980




msg:4230311
 10:42 pm on Nov 14, 2010 (gmt 0)

Hi there Sophia_A,

Cool! Yep, I see where you are now!

Ok, bear with me. If your already calling the include file and the menu is in there, that's fine, you just need to know how you are going to know what page your on. This is typically done via the $_GET global; but from what I can see you are just wanting to know how to 'fuse' your codes together.

If the <li> tags and <a href> tags are being generated by an echo then something like this would be fine:-

example

<?php

echo '<li class="artist"><a href="artist.php">'.(($thisPage == "Artist") ? '<img src="/images/50.png" width="50" height="20"/>' : '').'></a></li>';

?>

or with the "" double quote approach:-

<?php

echo "<li class=\"artist\"><a href=\"artist.php\">".(($thisPage == "Artist") ? '<img src=\"/images/50.png\" width=\"50\" height=\"20\" />' : '')."></a></li>";

?>

Something along that line should do what you want it to, but to simplify what point I have tried to convey is this:-

<?php

echo "Hello I am a string";

?>

A normal echo (double quotes) now to make that conditional, I do this:-

<?php

$day_of_the_week = date("l");//lowercase l for the day of the week!

echo "Todays day is ".$day_of_the_week." and this is a good day!";

//that's to include a var into the string

echo "Today is ".($day_of_the_week == "Friday") ? 'definitely' : 'definitely not')." friday";
?>

Using the ternary operator there within an echo is a normal way of dynamically changing the text depending on what's being evaluated.

echo ($var ? 'case true': 'case false');

the ? makes the evaluation, then left of the colon is 'case true' and right of the colon is 'case false'

I hope that makes thing's a little clearer, and I also hope that I have read your problem correctly too.

Good luck!

Cheers,
MRb

Sofia_A




msg:4230314
 11:11 pm on Nov 14, 2010 (gmt 0)

Thanks MRb! That's *almost* perfect, check it out:

[fia.me.uk...]

it probably would be perfect if I could get the style to work instead. How do I removed the awkward '>' that seems to be something to do with the code?

:)

Sofia_A




msg:4230318
 11:26 pm on Nov 14, 2010 (gmt 0)

Oooh... forget that... I made the class work! So how do I make it change class instead of placing the object?

I had a go, but it of course not correct:

<?php
echo '<li class="artist"><a href="artist.php">'.(($thisPage == "Artist") ? class="selected" : '').'></a></li>';
?>

missing ' I imagine... really have to study this php lark. Apols. And thanks for all the help so far!

Sofia_A




msg:4230320
 11:37 pm on Nov 14, 2010 (gmt 0)

Wait a minute... maybe I AM getting the hang of this... it starting to make some sort of sense....

NOW check it :)

[fia.me.uk...]

THANK YOU!

Matthew1980




msg:4230392
 8:13 am on Nov 15, 2010 (gmt 0)

Hi there Sophia_A,

css class reference's go inside the object/element that you want to apply the style to, so that little excerpt that you have there is SO close! You just needed to move the entire php section back a couple of chars and then you would have had that correct.

Have fun!

Cheers,
MRb

Sofia_A




msg:4230504
 2:30 pm on Nov 15, 2010 (gmt 0)

Hi Matthew,

That's exactly what I had done.

My final code is this:

<?php
echo '<li class="artist"><a href="artist.php" '.(($thisPage == "Artist") ? 'class=selected' : '').'></a></li>';
echo '<li class="writer"><a href="writer.php" '.(($thisPage == "Writer") ? 'class=selected' : '').'></a></li>';
echo '<li class="musician"><a href="musician.php" '.(($thisPage == "Musician") ? 'class=selected' : '').'></a></li>';
?>

Thanks so much for your advice! :)

cmnetworx




msg:4235538
 6:21 am on Nov 27, 2010 (gmt 0)

Sweet, I didn't know you could abbreviate the query like that, I was using

echo "<li><a href='/' class='mainlevel-nav '"; if($tab == 'home') { echo "id='active_menu-nav"; } echo "class='mainlevel-nav'>Home</a></li>";

Gonna go clean my code up some more now..

Matthew1980




msg:4235608
 10:50 am on Nov 27, 2010 (gmt 0)

Hi there cmnetworx,

Pleased that this is helping others, either method will work, just that doing ternary's within a normal echo looks more elegant & is easier to follow - after all ternarys are actually *kinda* condensed if/else clauses... at least they work logic the same way & with a bit of thought, you can make them into if/elseif/else chains - nested ternarys are ace to work with, especially in sql stuff.

Anyway, have fun with your code.

Cheers,
MRb

Readie




msg:4235790
 12:35 am on Nov 28, 2010 (gmt 0)

Hey Sofia,

Just a thought to tidy up your code a bit:

<?php
echo '<li class="artist"><a href="artist.php" '.(($thisPage == "Artist") ? 'class=selected' : '').'></a></li>';
echo '<li class="writer"><a href="writer.php" '.(($thisPage == "Writer") ? 'class=selected' : '').'></a></li>';
echo '<li class="musician"><a href="musician.php" '.(($thisPage == "Musician") ? 'class=selected' : '').'></a></li>';
?>

Can be changed to be more manageable by doing this:

<?php

$page_array = array(
'artist' => '/artist.php',
'writer' => '/writer.php',
'musician' => '/musician.php'
);

foreach($page_array as $key => $value) {
$class = (strtolower($thisPage) == $key)? ' class="selected"' : '';
echo '<li class="' . $key . '"><a' . $class . ' href="' . $value . '.php"></a></li>';
}

?>

It'll have the same output, but if you want to add anymore links in it's a lot simpler.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved