turkman Posted October 21, 2010 Share Posted October 21, 2010 Hey i hope the title explains it enough. I'm just trying to get into classes. I am making a function to make making mysql querys easier. I pass the class the first string $db->inputstring("INSERT INTO ? (?,?) VALUES ('?','?')"); $db->format('mytable','id','name','1','ben'); Can someone tell me how the format fuction would look? Im not sure how to swap arrays. Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/ Share on other sites More sharing options...
phprocker Posted October 21, 2010 Share Posted October 21, 2010 I actually haven't watched this yet but this guy has really great tutorials. I've been meaning to get around to watching this one myself. Hopefully I get the chance today. Let me know if that tutorial helps you. Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/#findComment-1124973 Share on other sites More sharing options...
turkman Posted October 21, 2010 Author Share Posted October 21, 2010 i dont think thats what i need, i know how to do the mysql stuff, i just need to know how to replace arrays... switch each ? with the corresponding word sent to format Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/#findComment-1124981 Share on other sites More sharing options...
AbraCadaver Posted October 21, 2010 Share Posted October 21, 2010 I can show you some ways, but why? PDO has this: http://us2.php.net/manual/en/pdostatement.bindparam.php and if you're using MySQL you can use this: http://us2.php.net/manual/en/mysqli-stmt.bind-param.php. These have the benefit of escaping the data for you, and maybe other benefits. If not, then there is always: http://us2.php.net/manual/en/function.sprintf.php. Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/#findComment-1124985 Share on other sites More sharing options...
turkman Posted October 21, 2010 Author Share Posted October 21, 2010 its not really for functionality its just more out of intrest to see how it would work... can you replace may ?'s with different elements of an array? Also if you want to pass an array to a function, how do you do it. say function acceptarray($arr) would you call that like the followin ---- acceptarray('arr1','arr2','arr3') can someone answer these for me please. Thanks. Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/#findComment-1124990 Share on other sites More sharing options...
AbraCadaver Posted October 21, 2010 Share Posted October 21, 2010 This is very convoluted, but I'm tired and I couldn't get the simple solution to work. I'll revisit it later. This is still not the right approach for this type of thing, but just to answer the question: $db = new DB; $db->inputstring("INSERT INTO ? (?,?) VALUES ('?','?')"); $db->format('mytable', array('id'=>1, 'name'=>'ben')); echo $db->query; class DB { public function inputstring($string) { $this->query = $string; } public function format($table, $data) { $replace = array_merge(array($table), array_keys($data), array_values($data)); $search = array_fill(0, count($replace), '/\?/'); $count = true; while($count) { $this->query = preg_replace($search, $replace, $this->query, 1, $count); } } } Link to comment https://forums.phpfreaks.com/topic/216504-replace-every-with-each-word-in-an-array/#findComment-1124995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.