What's new

Need Quick PHP Help!!!

Eagle

aka Alshain
Moderator
OK, I'm writing a script to generate a search with quite a few filtering options. Here is my function.


PHP:
	function addoperator($where) {
		if($where == true) { 
			return " AND"; 
		} else { 
			$where = true; 
			return " WHERE"; }
}

But $where never gets returned as true so I end up getting all WHEREs and no ANDs in the SQL command. Is there a special way to pass by reference in PHP?
 
Last edited:
OP
Eagle

Eagle

aka Alshain
Moderator
OH, and a SQL question. Can you do basic arithmatic in SQL query? Like

Code:
SELECT * FROM `table1` WHERE `field1` - `field2` > `field3` AND `field1` + `field2` < `field4` ORDER BY `field1`
 

Imoto

Game Boy Goddess ;P
Simple aritmetic should be possible, but I think you need a 'virtual field' for that. But I'm not sure about that.
 

Malcolm

Not a Moderator
Eagle said:
OH, and a SQL question. Can you do basic arithmatic in SQL query? Like

Code:
SELECT * FROM `table1` WHERE `field1` - `field2` > `field3` AND `field1` + `field2` < `field4` ORDER BY `field1`

iirc it would be
Code:
SELECT * FROM `table1` WHERE (`field1` - `field2`) > `field3` AND (`field1` + `field2`) < `field4` ORDER BY `field1`

as for your PHP code is your $where variable a global variable? As in not declared inside a function?

try doing this at the beginning of your php document:
PHP:
<?php

$where = false;
....
?>

if that doesn't work then get back to me
 
OP
Eagle

Eagle

aka Alshain
Moderator
From what I read on the internet its not possible unless you are returning that value, I guess I'm just going to have to copy and paste that if else statement a dozen times.
 

Doomulation

?????????????????????????
Can't you just pass a variable to that function which it modifies? Or does it send them by value...?
*Shrug* I don't know, just an idea...
 
OP
Eagle

Eagle

aka Alshain
Moderator
actually that should be set equal to true now that I think about it. It sets it true so next time it will chose AND instead of where, but it still doesnt work :(
 

Doomulation

?????????????????????????
Couldn't something like this work:
PHP:
function func($what)
{
    $what=true;
}

$what = false;
func($what);
 
OP
Eagle

Eagle

aka Alshain
Moderator
Doomulation said:
Couldn't something like this work:
PHP:
function func($what)
{
    $what=true;
}

$what = false;
func($what);

No because when you call function func a copy of the variable $what is made. when you assign true to $what, the copy is modified, but not the original. When the function ends the copy is destroyed but the original remains what it was the begin with. This is called pass by value.
 
OP
Eagle

Eagle

aka Alshain
Moderator
Anyway, the word QUICK in the title of this post had meaning, I turned in the assignment fully working this morning.
 

ShadowPrince

Moderator
Eagle said:
OK, I'm writing a script to generate a search with quite a few filtering options. Here is my function.


PHP:
	function addoperator($where) {
		if($where == true) { 
			return " AND"; 
		} else { 
			$where = true; 
			return " WHERE"; }
}

But $where never gets returned as true so I end up getting all WHEREs and no ANDs in the SQL command. Is there a special way to pass by reference in PHP?

You can use something like this :

PHP:
$where = false 
 function addoperator() {
                         global $where  ;		

                         if($where == true) { 
			return " AND"; 
		} else { 
			$where = true; 
			return " WHERE"; }
}

Declare variable $where somewhere outside of the function ,and put global $where inside the function to access it .
This way global variables are accessible in PHP inside functions .
 

ingonab

New member
Use global variables ...

Christ! What're you guys teaching the poor fellow! :doh: The easiest way to make the variable change is to stick an ampersand infront of the variable name in the function declaration. Like so:

PHP:
function addoperator(&$where)
{
    ...
}
 

Top