PHP code generation - how to append text that has $_GET in it

Hi.  I created logic that generates hard-coded php pages. I have to add one more piece of logic to the top of the generatied page so I can pass a category value to it.

Here's the line I am trying to add.
   $texttoaddGet = "<? $categoryselected = $_GET['category'];?>" ;

The generated page will display the value in $categoryselected at the top of the page.

When I run the generation logic, I get an error on this line (line 447).  Without this line, the code runs without a probelm and the page is generated correctly but then I can't pass the category value from pages that link to the generated page.

Here's the error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/40/d228880029/htdocs/njbridetobe/new/webadmin/advertiser_rec_submit.php on line 447

I have tried writing this line many diferent ways but can't seem to hit on what I am doing wrong.

Is there a special way I have to handle this line because it has a "$_GET" in it?

Thanks for your help.
Alexis
alexisbrAsked:
Who is Participating?
 
Greg AlexanderConnect With a Mentor Lead DeveloperCommented:
Ahh, you need to escape the $ and ? so it does not execute it, so something like this:

$texttoaddGet = "<\? \$categoryselected = \$_GET['category'];\?>" ;
0
 
Greg AlexanderLead DeveloperCommented:
<?php
$texttoaddGet = $_GET['category'];
echo $texttoaddGet;
?>

or

<?= $_GET['category']; ?>
0
 
Greg AlexanderLead DeveloperCommented:
This would add them together

$texttoaddGet = $categoryselected . $_GET['category'] ;

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
shobinsunCommented:
Hi,

You have to simple use this:

$categoryselected = $_GET['category'];
0
 
shobinsunCommented:
If you want to concatenate the values, $categoryselected and the GET value,

use:  $texttoaddGet = $categoryselected.$_GET['category'];
0
 
alexisbrAuthor Commented:
Thanks, everyone.  I can see by the answers that I didn't explain myself well. Let me try again.  Sorry for the confusion.

I am generating hard-coded php pages from another php page.  The page I am asking about is the page that generates the hard-coded page.

On this page, there is no value in $_GET['category'] or $categoryselected but there will be values on the hard-code page because the category value will be passed from another page to the generated page.  I want to pass the exact string as is so, on the generated page, it can have the $_GET to read the value passed to it.  Both $_GET['category'] and $categoryselected are only set on the generated page, not on the page I am working on now.

Please see the code examples.

I hope this makes more sense.

Thanks,
Alexis

 
For example, the start of the generated page should look like this:
<? $categoryselected = $_GET['category'];?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

The page that generates the code above looks like this:
$texttoaddGet = "<? $categoryselected = $_GET['category'];?>" ;
$texttoadd1 = $texttoaddGet . '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' .
'<html xmlns="http://www.w3.org/1999/xhtml">' .
'<head>' .
ETC
I echo the string values to the generated page.

Open in new window

0
 
shobinsunCommented:
Try this:

$texttoaddGet = "<? ".$categoryselected = $_GET['category']."?>" ;

or:

$categoryselected = $_GET['category'];
$texttoaddGet = "<?".$categoryselected."?>";
0
 
Ray PaseurConnect With a Mentor Commented:
If i understand your question correctly, you might try using NOWDOC syntax.
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

See http://www.laprbass.com/RAY_temp_alexisbr.php and use View Source to see what came out.

Best regards, ~Ray
<?php // RAY_temp_alexisbr.php
error_reporting(E_ALL);

// CREATE THE PHP SCRIPT
$php = <<<'EOD'
<?php // HERE IS THE CONTENTS OF $_GET
var_dump($_GET);
if (isset($_GET["q"])) echo $_GET["q"];
EOD;

// WRITE THE PHP SCRIPT TO THE BROWSER
echo $php;

Open in new window

0
 
alexisbrAuthor Commented:
Thanks to everyone who helped me.  I didn't even think about escaping.  My page is working perfectly now.

galexander07, Your code was what I used, except the <? and ?> did not have to be escaped.  Here's the line that worked for me.
  $texttoaddGet = "<? \$categoryselected = \$_GET['category'];?>" ;

Ray,  I looked at your links and I know this code would have also worked for me.  However, in addition to the line I asked about, I had already written about 150 lines of additional code to generate and I didn't want to have to make any other changes to my code because the rest of the generation was already working and I didn't want to take the chance of breaking it.

Regards,
Alexis
0
 
Ray PaseurCommented:
Hi, Alexis, and thanks for the points.  It may seem beside the point, but consider using the full <?php tag instead of the short <? tag.  If you use the full tag it will avoid collisions with XML.
0
 
alexisbrAuthor Commented:
Thanks, Ray.  Great suggestion.  I will do so.  I've really learned a lot from all your posts.  I also use many of the posts when you have helped other people, in addition to the ones you have helped me directly with. I appreciate all the time you spend helping everyone.
Alexis
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.