07-08-2006, 09:34
|
|
|
חבר מתאריך: 26.06.05
הודעות: 36
|
|
אתה לא יוכל בתוך פונקציה להכניס FOREACH
קוד PHP:
<? $sql = " SELECT * FROM `downloads` WHERE "; $keys = array("dfdf" , "dsfsd"); foreach($keys as $k=>$keys2) { if (count($keys) == $k ) $sql .="(`name` LIKE '%{$keys2}%')"; else $sql .="(`name` LIKE '%{$keys2}%') OR "; } $sql .= " (`comment` LIKE '%{$_GET['keys']}%') ORDER BY `id` DESC "; echo $sql;
נסה את זה הבעיה היא במשפט ה SQL שלך תבדוק לפני זה שהמשפט יוצא לך נכון
קבל דוגמה אחרת בעשיתי ליצירת פונקיצה של מנגנון SQL
קוד PHP:
public function genrate_Sql($tables = array() ,$distinct , $fileds = array() , $where = " ",$join="", $group = "", $order = array("id" , "desc") ,$between= "", $minLimit = 0 , $maxLimit = 50) { $sql = ""; if (count($tables)>=1) { if (count($fileds)>=1) { $distinct = ($distinct==1)?strtoupper('distinct'):""; $sql .= "Select $distinct \n\t"; $orderCount = count($order); $fildCounter =count($fileds); $tabCounter =count($tables); $innectcounter = 1; foreach ($tables as $k=>$tab) { $p = $tab; foreach ($fileds as $kay=>$filed) { if ($filed[0]==$p){ if ($kay == $fildCounter-1){ $f .= "$p.$filed[1]"; } else { $f .= "$p.$filed[1] , "; $innectcounter++; } } if ($innectcounter==6) { $f .= "\n\t"; $innectcounter = 1; } } if ($k != $tabCounter-1){ $t .= "$p , "; } else{ $t .= $p; } } $sql .= $f."\n\t\tFrom ".$t; if (!is_null($where)) { $sql .= "\n\t\tWhere $where"; } if (is_array($join)) { switch ($join[0]) { case 1: $sql .= "\n\t\tRIGHT JOIN "; break; case 2: $sql .= "\n\t\tLEFT JOIN "; break; case 3: $sql .= "\n\t\tINNER JOIN "; break; } $sql .= "$join[1] "; $sql .= "\n\t\tON $join[2]"; } if (!is_null($group)) { $sql .="\n\t\t\tGroup By $group"; } foreach ($order as $k=>$val) { if ($orderCount-1 == $k) { $r .= "$val[0] ".strtoupper($val[1]); } else { $r .= "$val[0] ".strtoupper($val[1]).", "; } } if (is_array($between)) { $sql.="\n\t\t\t"; if ($beween[0]=="n"){ $sql.="Not "; } $sql.="BETWEEN '$between[1]' AND '$between[2]'"; } $sql .="\n\t\t\tORDER BY $r"; $sql .="\n\t\t\tLimit $minLimit , $maxLimit"; } } $this->Query($sql); return $this->errCheck; }
כפי שאתה שם לב רוב הפונקיצה משתמשת
בבמרות לפניה הפונקציה עצמה ולא אחרי
|