homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

php code inside an echo wrapped by a div
how to php inside a div and php inside echo

Msg#: 4431666 posted 9:59 am on Mar 21, 2012 (gmt 0)

Hello i am trying to put this code inside and echo cause i want to be triggered by and if

<div id="remove_all"><a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">Remove</a></div>

so can you please give me a hand
this is where i am stuck

Thanks! George.
I guess it is an easy one but i am struggling on this on



WebmasterWorld Senior Member 10+ Year Member

Msg#: 4431666 posted 10:36 am on Mar 21, 2012 (gmt 0)

You just have to be aware of mixing the single and double quotes - they have to be balanced and in some cases escaped.

Best way to learn this is to split your desired html output into different lines and then see which line triggers the error.

echo '<div id="remove_all">';
echo '<a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">';
echo 'Remove</a></div>';

run that and you will see the error line being the one with $sess. If you still can't spot the error split it up further

echo '<a href="$sess->url($_SERVER['PHP_SELF']';
echo '?page=shop.browse&amp;category_id=';
echo $category_id;
echo ')">';

You should now see which line(s) are not matching single and double quotes. Editing one line like this is much easier than trying to edit a whole line like this

echo '<div id="remove_all"><a href="$sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id )">Remove</a></div>';


If you still can not spot it the problem is with the single quotes around PHP_SELF. They need to be escaped



WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4431666 posted 3:55 pm on Mar 21, 2012 (gmt 0)

Another less graceful and not so pretty approach, you can surround other blocks and php calls with php . . . like

<?php if ($filter_selected) { ?>

<p>This will only display if filter selected.</p>

<?php } // end the IF ?>

This allows you to only echo where you need it, and can contain other nested php calls. so you can do

<?php if ($filter_selected) { ?>

<div id="remove_all"><a href="<?php echo $sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id);?>">Remove</a></div>

<?php } // end the IF ?>

IMO this makes for some sloppy programming that's hard to debug but PHP lends itself to it very easily and works in a pinch. Note the big difference is you're not echoing static HTML, just the stuff from PHP - be sure to note the ending ; after the parentheses:

<?php echo $sess->url($_SERVER['PHP_SELF'].'?page=shop.browse&amp;category_id='.$category_id);?> <--- right there

An aside, it seems like calling a class and using PHP_SELF just for the script's name is really a long way to go about what you're doing. PHP_SELF has potential security issues anyway. I'd just define the script name somewhere and do

$thisscript = 'myscript.php';

<?php echo "/$thisscript?page=shop.browse&amp;category_id=$category_id";?>

By using double quotes it allows the scalar variables to interpolate and is much simpler to debug.


Msg#: 4431666 posted 4:52 pm on Mar 21, 2012 (gmt 0)

I must thank you both.

Frank Rizzo thank you for the guide on how find errors.

And rocknbil indeed a <p> can do the job.
and i ll change the PHP_SELF with the domain like this
<?php echo 'http://www.example.com?page=shop.browse&amp;category_id='.$category_id);?>

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