Jump to content


Photo

Writing functions


  • Please log in to reply
2 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 September 2006 - 12:38 PM

HI all

im just testing writing some functions but why will this not return the second or third $sql if the varible isset

function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit){
          if (isset($contents) && isset($table) && isset($postname1)){
          return $sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1."";
          }
          if (isset($name2) && isset($postname2) ){
               return $sql.=" AND ".$name2." = ".$postname2."";
          }
          if (isset($limit)){
               return $sql.="LIMIT ".$limit."";
          }
}

$test=Select('*',test_table,username,testname,'username2','testname2','2');

echo($test);

?>
Im loving it ........

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 02 September 2006 - 12:50 PM

try this..


function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit) {
          if (($contents !== '') && ($table !== '') && ($postname1 !== '')) {
          return $sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1."";
          }
          if (($name2 !== '') && ($postname2 !== '') ) {
               return $sql.=" AND ".$name2." = ".$postname2."";
          }
          if ($limit !== '')) {
               return $sql.="LIMIT ".$limit."";
          }
}


i have changed to !== '' as this checks if there is a value but i think isset() will always return true because it is set at the top wether there is a value or not, i've also corected the if statement enclosures..

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 02 September 2006 - 12:57 PM

Hi thanks

i used  !empty()

i've realized what i should have done .

function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit){

		if (!empty($contents) && !empty($table) && !empty($postname1)){
		$sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1."";
		} 
		if (!empty($name2) && !empty($postname2) ){
			$sql.=" AND ".$name2." = ".$postname2."";
		}
		if (!empty($limit)){
			$sql.=" LIMIT ".$limit."";
		}
		return $sql;
}

$test=Select('*','',test_table,testname,'','','1');

echo($test);

Im loving it ........




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users