Forum Moderators: coopster

Message Too Old, No Replies

Passing variable into function

         

rariti

11:41 am on Dec 26, 2007 (gmt 0)

10+ Year Member



Pls forgive the newbie question... but can anyone tell me how to pass an existing variable.
I have looked at the other posts, and need a kickstart.

This is what I would like to do. Pass the existing $whereVar into a function.

$whereVar='WHERE parentID=99'; //already exists

function getPage()
{
$query = 'select id, name from page $whereVar';

$result = mysql_query($query);
$page = array();
while ($row = mysql_fetch_object($result)) {
$page[$row->id] = $row->name;
}
return $page;
}
$page = getPage();

dreamcatcher

1:37 pm on Dec 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function getPage()
{
global $whereVar;
$query = 'select id, name from page $whereVar';

dc

coopster

2:38 pm on Dec 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, rariti.

Another option ...

$whereVar='WHERE parentID=99'; //already exists 
function getPage($whereVar)
{
$query = 'select id, name from page $whereVar';
$result = mysql_query($query);
$page = array();
while ($row = mysql_fetch_object($result)) {
$page[$row->id] = $row->name;
}
return $page;
}
$page = getPage($whereVar);

rariti

8:08 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



Thanks for replying so quickly,
however, I am still not getting the variable passed into the function.
Quite sad, considering I have tried the examples in my dummies book.

I have tried both dreamcatcher's and coopster's, and would like to try dc's, as it seems simpler.
I have a feeling that the global $whereVar needs to be acknowledged as a global first. Does that sound right?

----------------
global $show_whereVar;
$whereVar='WHERE parentID=99';//exists in main program
$show_whereVar=$whereVar;

function getPage()
{
global $show_whereVar;
$query = 'select id, name from page $show_whereVar';
print $query;
-----------------------
The print outputs:
"select id, name from page $show_whereVar"

with no variable.

rariti

8:19 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



If you don't mind my asking...
I was also wondering what the proper way of selecting other fields in that same function. (abstract and sequence)
<?
$whereVar='WHERE parentID=99'; //already exists

function getPage()
{
$query = 'select id, name, abstract, sequence from page $whereVar';

$result = mysql_query($query);
$page = array();
while ($row = mysql_fetch_object($result)) {
$page[$row->id] = $row->name;
}
return $page;
}
$page = getPage();
?>

<table>
<? foreach ($page as $id => $name) {?>
<tr>
<td><?=$id?></td>
<td><?=$sequence_order?></td>
<td><?=$name?></td>
<td><?=$abstract?></td>
</tr>
<? }?>
</table>

I know that is has something to do with:
$page[$row->id] = $row->name;

Just don't know how I would add more fields.

Thanks.

rariti

11:21 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



I placed a print $whereVar outside and inside the function, and they both output correctly with the right values,
however the $whereVar inside the $query is not recognizing the global.
The query is printing as
"select id, name from page $get_where"

--------------------
$whereVar='WHERE parentID=99'; //already exists

print "<br>(OUTSIDE whereVar=".$whereVar.")<br>";

function getPage()
{
global $whereVar;
print "<br>(INSIDE whereVar=".$whereVar.")<br>";

$query = 'select id, name from page $whereVar';

print "<br>(INSIDE AFTER QUERY whereVar=".$whereVar.")<br>";

$result = mysql_query($query);
$page = array();
while ($row = mysql_fetch_object($result)) {
$page[$row->id] = $row->name;
}
return $page;
}
$page = getPage();
--------------------------

rariti

11:30 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



well this is embarrassing.... apparently it works fine with double quotes.
How's that for a kick in the pants?

Thanks guys.

Can anyone pls tell me how to pull more fields regarding the page array? (from previous post)

Thanks again.