Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I put PHP code inside PHP code??? 2

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
Why does this not work. I'm trying to set $showpage to = a dynamic table. then later if conditions are right, echo it on the screen.
Assuming that all my variable are set, why won't this work. I thought as long as I escape my " (\") I should be able to just copy and paste this from another page where my form is showing up and working fine.

here is the code.


<?php

if ($_GET['page_name'] != ""){
$showpage = $row_htmlcontent['page_content'];
} else {
if ($_GET['photo_category'] != "")
This should be building a dynamic 3 column table with a radiobutton, thumb name, and photo description in each coloumn as long as the array exists. It works until I try to load this into a variable.
$showpage ="<form name=\"form1\" method=\"post\" action=\"\">
<table width=\"547\" border=\"0\">
<?php do { ?> <tr>
<td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"<?php echo $row_thumbscontent['photo_category']; ?>\" onClick=\"document.forms['form1'].submit();\"/>
</td>
<td width=\"209\"><?php echo $row_thumbscontent['photo_thumb_name']; ?></td>
<td width=\"226\"><?php echo $row_thumbscontent['photo_desc']; ?></td>
</tr>
<?php } while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent)); ?>
</table>
</form>";
}


?>

TIA.
 
yes, it's telling me to check the line that has my first array in it.
I have now taken out all the array results and put them in variables like so: and now I don't get any error message but the variables aren't getting set and the page doesn't do anything except show $row_indexcontent.

if ($_GET['page_name'] == ""){
$showpage = $row_indexcontent['page_content'];
}
?>
<?php

if ($_GET['page_name'] != ""){
$showpage = $row_htmlcontent['page_content'];}
if
($_GET['photo_category'] != ""){
$category = $row_thumbscontent['photo_category'];
$thumbname = $row_thumbscontent['photo_thumb_name'];
$content = $row_thumbscontent['photo_desc'];

$showpage .=
"<form name=\"form1\" method=\"post\" action=\"\">
<table width=\"547\" border=\"0\">
<tr>do {
<td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"$category\" onClick=\"document.forms['form1'].submit();\"/></td>
<td width=\"209\">$thumbname</td>
<td width=\"226\">$conent</td>
</tr>} while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent))
</table>
</form>"
;}
?>
 
start by changing this
Code:
    <?php do { ?> <tr>
      <td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"<?php echo $row_thumbscontent['photo_category']; ?>\" onClick=\"document.forms['form1'].submit();\"/>
</td>
      <td width=\"209\"><?php echo $row_thumbscontent['photo_thumb_name']; ?></td>
      <td width=\"226\"><?php echo $row_thumbscontent['photo_desc']; ?></td>
    </tr>
    <?php } while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent)); ?>
to
Code:
    <?php while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent)){  ?> <tr>
      <td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"<?php echo $row_thumbscontent['photo_category']; ?>\" onClick=\"document.forms['form1'].submit();\"/>
</td>
      <td width=\"209\"><?php echo $row_thumbscontent['photo_thumb_name']; ?></td>
      <td width=\"226\"><?php echo $row_thumbscontent['photo_desc']; ?></td>
    </tr>
    <?php } ?>

i.e. use a while loop rather than a do...while loop otherwise your variables have no values until the end of the first iteration as do loops are evaluated only at the end.
 
So you are trying to define a php variable by starting the definition, exiting out of PHP into HTML, going back into PHP, etc....I'm not sure you can do that with PHP. I actually can't think of an example of a language like PHP that does allow it, but that could be the lack of coffee talking.

Based off that my guess is that it is allowing you to build that whole thing as a string, PHP start and end tags included. Then it is trying to substitute values in for the few variables you have in the string and blowing up because it doesn't like your associative array references dropped straight into strings without braces around it.

Example:
Code:
$a = "<? abs(-1); ?>";

or

$b['test']=1;
$c = "<? echo $b['test'] ?>";
The same thing is happening in both of those sequences. Since your already inside the PHP block, it won't allow you to re-enter the block and assumes that is just part of the string your building. Since your still building a string the abs and echo functions I included are also part of the string. It does attempt to substitute the variable values for the variable sin the string, same as it would normally, and in the second situation it fails (it doesn't like trying to access that associative array w/out some curly braces).

So back to your logic. Instead of trying to embed php loops inside a php string, why not create your string then concatenate additional content as you loop through the recordset:
Code:
$showpage ="<form name=\"form1\" method=\"post\" action=\"\">
  <table width=\"547\" border=\"0\">";

do {

   // add another row to the string
   $showpage .= "<tr>
      <td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"[highlight]{[/highlight]$row_thumbscontent['photo_category'] [highlight]}[/highlight]\" onClick=\"document.forms['form1'].submit();\"/>
</td>
      <td width=\"209\">[highlight]{[/highlight]$row_thumbscontent['photo_thumb_name'][highlight]}[/highlight] </td>
      <td width=\"226\">[highlight]{[/highlight]$row_thumbscontent['photo_desc'][highlight]}[/highlight] </td>
    </tr>";

} while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent));

//finish the string
$showpage .= "</table></form>";

I didn't really spend any time checking this, so there are probably some errors, but it's more of an example then a working solution.

-T

 
@Tarwn

i think php does what you refer to. this, for example, is fine in a single script

Code:
<?php
$text = "hello world";
?>
<html>
<body>
<p> lots of text goes in here</p>
<div style="color:red;"><?=$text?></div>
</body>
</html>

if i misunderstood you, then apologies.
 
That works but this doesnt:

<?PHP
$othervar="Hello";
$somevar="sometext [red]<?PHP echo $othervar;?>[/red]";
?>

Opening PHP tags withing a string thats inside PHP tags is not right. You are already inside PHP scope, so why would you need to open it again:

Code:
[green]<?php[/green]
[red]
if ($_GET['page_name'] != ""){
        $showpage = $row_htmlcontent['page_content'];
        } else {
        if ($_GET['photo_category'] != "")
This should be building a dynamic 3 column table with a radiobutton, thumb name, and photo description in each coloumn as long as the array exists. It works until I try to load this into a variable.             
$showpage ="<form name=\"form1\" method=\"post\" action=\"\">
  <table width=\"547\" border=\"0\">
    [blue]<?php do { ?>[/blue] <tr>
      <td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"[blue]<?php echo $row_thumbscontent['photo_category']; ?>[/blue]\" onClick=\"document.forms['form1'].submit();\"/>
</td>
      <td width=\"209\">[blue]<?php echo $row_thumbscontent['photo_thumb_name']; ?>[/blue]</td>
      <td width=\"226\">[blue]<?php echo $row_thumbscontent['photo_desc']; ?>[/blue]</td>
    </tr>
    [blue]<?php } while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent)); ?>[/blue]
  </table>
</form>";
                }
    [/red]

[green]?>[/green]

You opening and closing PHP tags inside a PHP string over and over again. If you are already in PHP tags you don't need and should never open and close tags like that.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Tarwn and JPadie, Thanks alot.
The page does load now and I understand that I shouldn't have been looping my form tag or my close form tag. It's working fine except for now it doesn't like my array and it only displays one array element on the screen.
I've switched the loops like Jpadie suggested and I changed the structure like Tarwn suggested.
Please can you tell me what looks wrong now.

here is the code.

$showpage ="<form name=\"form1\" method=\"post\" action=\"\">
<table width=\"547\" border=\"0\">";

do {

// add another row to the string
$showpage .= "<tr>
<td width=\"36\"><input name=\"thumbselect\" type=\"radio\" value=\"{$row_thumbscontent['photo_category'] }\" onClick=\"document.forms['form1'].submit();\"/>
</td>
<td width=\"209\">{$row_thumbscontent['photo_thumb_name']} </td>
<td width=\"226\">{$row_thumbscontent['photo_desc']} </td>
</tr>";
it errors on the line below and the error says:
Warning: mysql_fetch_assoc(): 6 is not a valid MySQL result resource in C:\AppServ\ on line 31

} while ($row_thumbscontent = mysql_fetch_assoc($thumbscontent));


//finish the string
$showpage .= "</table></form>";
 
that error just means that there is something wrong with your query. the results of mysql_error() should lead you to the answer.
 
jpadie: Yeah, I didn't exactly word what I was thinking well. I originally thought he was exiting out of the PHP block then back in, all inside a string definition. It wasn't until I started modifying the code that I realized that it was just more PHP tags embedded inside a string that was embedded in am existing PHP block.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top