How to iterate though the array?
Hello..
i have this problem.. im doing some kind of late static binding. im really a new to this and don\'t know how to solve this problem. please help me to solve it... What i want to do is instead of using static:: $arr[0].\",\".static:: $arr[1] i want to iterate though the array.. i tried using a foreach loop but it doesn\'t work.. i don\'t understand why.. i will really appreciate if someone demonstrate a code to do this in a better way. this is my code. class MainModel { public static function create($custArray){ global $connection; $query = \"INSERT INTO \".static::$tblname.\"(\"; $query.=static:: $arr[0].\",\".static:: $arr[1]; $query.=\") VALUES (\'\".$custArray[\'name\'].\"\',\'\".$custArray[\'details\'].\"\')\"; if(mysql_query($query) > 0){ return true; }else{ return false; } } } class ChildModel extends MainModel{ protected static $tblname = \'test\'; protected static $arr = array(\'name\',\'details\'); } //here im calling the method.. just for testing.. if(ChildModel::create(array(\'name\'=>\'d\',\'details\'=>\'s\'))) { echo(\"its working! \"); } else { echo(\"its not working! \".mysql_error()); }
2 Answers
Thanx a lot for your answer.. now i'm thinking about using, mysql_real_escape_string() for this query. is there a way i can use it within this???
Posted: virangya 0 of 0 people found this answer helpful. Did you? Yes No
first you cant get the static by static::$var you should use self::$var
and second the ChildModel has no useful now see this code you can use unlimited values class MainModel { protected static $tblname = 'test'; public static function create($custArray){ global $connection; $insert = "INSERT INTO ".self::$tblname." SET "; foreach ($custArray as $key => $value) { $sets[] = "`$key` = '$value'"; } $query = $insert.implode(",",$sets); if(mysql_query($query)){ return true; }else{ return false; } } } //here im calling the method.. just for testing.. if(MainModel::create(array('name'=>'d','details'=>'s','test'=>'test'))){ echo("its working! "); }else{ echo("its not working! ".mysql_error()); } Posted: MacOS 1 of 1 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved