Forum Moderators: coopster

Message Too Old, No Replies

Function / Array Problem

Function / Array Problem

         

crankshaft

6:00 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Can someone tell me why the function fails? - it works fine if I execute the foreach directly, but if I execute it as a function it fails!
======================
<?php
$tm_names = array('aaaa,'yyyy' ,'zzzz');

function DoMenus()
{
foreach($tm_names as $tm_name)
{
echo "<tr><td>". $tm_name . "</td></tr>";
}
}
?>

<body>
<table>
<?php
// This succeeds!
foreach($tm_names as $tm_name)
{
echo "<tr><td>". $tm_name . "</td></tr>";
}

// This fails!
Echo DoMenus();

?>
</table>
</body>

==========================
Thanx so much!

PeterC

wrightee

7:02 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Change to:

function DoMenus()
{
global $tm_names;
foreach($tm_names as $tm_name)
{
echo "<tr><td>". $tm_name . "</td></tr>";
}
}

Or, pass $tm_names as an argument to it:

---
function DoMenus($tm_names)
{
foreach($tm_names as $tm_name)
{
echo "<tr><td>". $tm_name . "</td></tr>";
}
}

DoMenus($tm_names)
---

crankshaft

8:05 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Excellent, thanx :)

One more related question, right now the echo will be placed where the DoMenus()is positioned.

How can I call the function from say a menu and have the result appear in a different cell / table other than from where the function was called?

Thanx

PeterC

wrightee

8:24 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Don't echo in a function, return the output instead thus:

---
function DoMenus()
{
global $tm_names;
foreach($tm_names as $tm_name)
{
$var.= "<tr><td>". $tm_name . "</td></tr>";
}
return $var;
}

$names=DoMenus();
---

..then you can echo $names wherever you want, or process it further.

crankshaft

8:30 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Great!

If I do that, will the echo execute when the function is executed or only when the page refreshes as I need the menu array to display the new 'context' menu without needing to reload the whole page?

Thanx again!

PeterC

killroy

2:50 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP is strictly server side. PHP is ONLY executed when a page is loaded.

SN

crankshaft

1:51 am on Mar 30, 2005 (gmt 0)

10+ Year Member



Hmmm;

Following on from this, I have been using innerHTML to place my array in the required celly 'dynamically' meaning that if I click a button, the text will update without refreshing the page.

What is now happening, is that I am having trouble with the tag styles / parameters in the innerHTML, if I just use a simple <tag> then all is ok, but if I use parameters, the innerhtml fails i.e.:

This creates a on-cell clickable table:
<table><tr><td onclick=location.href= 'http://www.yahoo.com';>Click</td></tr></table>

This is a button with innerHTML copied from the table above, it fails:
<INPUT onclick="contents.innerHTML='<table><tr><td onclick=location.href= 'http://www.yahoo.com';>Click</td></tr></table>';" type='button' value='Button' name='B2'>
<span id="contents"></span>

This is the same button but with the parameters stripped out, it generates the table but obviously is not clickable!:
<INPUT onclick="contents.innerHTML='<table><tr>Click</td></tr></table>';" type=button value=Button name=B1>

I've spent many hours changing syntax and searching google, but can't seem to find an answer?

Thanx

PeterC

killroy

8:49 am on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's the quoting you cannot nest so many levels of quotes.
You will have to escape your quotes as \' or \" depending on what you use as outer quotes.

SN

crankshaft

11:21 am on Mar 30, 2005 (gmt 0)

10+ Year Member



Hi Kilroy;

Yo were right, thanx!

PeterC