functions defining html code snippets<

Any ideas as to why the following does not work:


<?php
// HTML for individual pages
function html_table_top() {
?>
<div id="main">
<div id="tablename"><?php echo "$dbname.$mytable"; ?></div>
<div id="table">
<table align="center">
<tr>
<?php
}
function html_table_base() {
?>
</tr>
<tr>
<td align="center"><a href=http://www.htmlforums.com/archive/index.php/"javascript:void(0)" onClick="history.go(-1)">Back one page</a></td>
</tr>
</table>
</div>
</div>
<?php
}

function some_php_code () {

html_table_top();
// lots of PHP code here
html_table_base();
}


This SHOULD work, but instead I get nothing when I call the html functions. This is a common problem I have seen before, and felt that now would be a good time to get an answer.

All replies appreciated!!you ARE calling the some_php_code() function....right? there is no other reason why it wouldnt work.You've changed your post a little.. Earlier you had problems using multiple PHP blocks? Nowadays I mostly use the heredoc syntax for longer strings, I recommend it. It's particularly useful in class definitions.
<!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc">http://www.php.net/manual/en/language.t ... ax.heredoc</a><!-- m -->

What you have now should work, I guess. Long time since I did anything like that, so I'm not sure(OOP, baby). Did n8 solve it?Originally posted by n8thegreat
you ARE calling the some_php_code() function....right? there is no other reason why it wouldnt work.

Yes I am calling the function. I will try the heredoc approach as outlined by Rydberg, but am still unhappy as to why this didn't work, more from a conceptual angle rather than practical.

Thanks for the posts.

And yes Rydberg, I had changed my post a little - sorry about that :sneaky: (and there was me thinking no-one was reading!!) :Pif globals are off then if I am not mistaken then these will be empty

$dbname.$mytable

you need to set them global in the functions you want to use them in.yes you are right Scoutt, but that was not the crux of my original post. thanks anyway.actually this seems redundant

function some_php_code () {

html_table_top();
// lots of PHP code here
html_table_base();
}


why call a function just to have it call 2 more?

so when those run you see nothing? it might have something to do with you going in and out of php. sounds funny but try to make it all php. and have the function return instead of just echoing it out.Originally posted by scoutt
actually this seems redundant

function some_php_code () {

html_table_top();
// lots of PHP code here
html_table_base();
}

why call a function just to have it call 2 more?

Because the functions html_table_top() and html_table_base() are reused on more than one page.


so when those run you see nothing? it might have something to do with you going in and out of php. sounds funny but try to make it all php.


That kind of defeats the purpose of reusable code/functions? I think I will go the way Rydberg suggested, which I assume I can put into a function and then just call it....Originally posted by tatlar
That kind of defeats the purpose of reusable code/functions? I think I will go the way Rydberg suggested, which I assume I can put into a function and then just call it....
no it doesn't. it can stil be used when and where ever you want. just don't go in and out of php. if you use html doc you will be in php all the way so it is the samething I suggested.<?php
// HTML for individual pages
function html_table_top() {

echo <<<END
<div id="main">
<div id="tablename">$dbname.$mytable</div>
<div id="table">
<table align="center">
<tr>
END;
}
function html_table_base() {
echo <<<END
</tr>
<tr>
<td align="center"><a href=http://www.htmlforums.com/archive/index.php/"java script:void(0)" onClick="history.go(-1)">Back one page</a></td>
</tr>
</table>
</div>
</div>
END;
}

function some_php_code () {

html_table_top();
// lots of PHP code here
html_table_base();
}

if that doesn't work then you have something else wrong. but that is the same as making each line a varaible or making the function return the code instead of echoing it.much obliged. I will check it out.well, i just tried your exact code and it worked fine on my machine. must be something weird with your php configuration?Hmmm - let me double check. Thanks...
 
Back
Top