Miko Posted August 5, 2009 Share Posted August 5, 2009 Hi, I have a function needs to implode several ID from a DB: my code: <?php require("config.php"); class insert_Title { function implodeId($test){ foreach($test as $name){ $sql = "SELECT * FROM categories WHERE name = '$name' "; $query = mysql_query($sql); $row = mysql_fetch_array($query); $id = $row['id']; $implode = implode(",", $id); echo $id; } } } $test = $_POST['test']; $submit = $_POST['submit']; if(isset($submit)){ $get_id = new insert_Title(); $exec = $get_id->implodeId($test); } ?> <form name="myForm" action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <input type="checkbox" name="test[]" value="Movies" />Movies<br /> <input type="checkbox" name="test[]" value="Manga" />Manga<br /> <input type="submit" name="submit" value="Go!" /> </form> I'm getting this error: Warning: implode() [function.implode]: Invalid arguments passed in D:\xampp\htdocs\scripts\implode.php on line 15 but when I make the function without a query inside it will work... Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/ Share on other sites More sharing options...
kickstart Posted August 5, 2009 Share Posted August 5, 2009 Hi You appear to be trying to implode a normal variable rather than an array. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891140 Share on other sites More sharing options...
Miko Posted August 5, 2009 Author Share Posted August 5, 2009 off course! d'oh! function implodeId($test){ foreach($test as $name){ $sql = "SELECT * FROM categories WHERE name = '$name' "; $query = mysql_query($sql); $row = mysql_fetch_array($query); $id = $row['id']; $array = array($id); $implode = implode(",", $array); } } now it works, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891141 Share on other sites More sharing options...
Miko Posted August 5, 2009 Author Share Posted August 5, 2009 hmm, I was talking to fast. when I echo the var $implode I don't get the exact result. function implodeId($test){ foreach($test as $name){ $sql = "SELECT * FROM categories WHERE name = '$name' "; $query = mysql_query($sql); $row = mysql_fetch_array($query); $id = $row['id']; $array = array($id); $implode = implode(",", $array); echo $implode; } } instead of having for ex: 1,2 I have 12 Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891142 Share on other sites More sharing options...
kickstart Posted August 5, 2009 Share Posted August 5, 2009 Hi Your array only has a single member. You only retrieve one row at a time from the database and assign it to $id, then you turn $id into the array $array. You then do another SELECT and wipe out the array $array and create a new one. You would need something like this:- function implodeId($test) { $array = array(); foreach($test as $name) { $sql = "SELECT * FROM categories WHERE name = '$name' "; $query = mysql_query($sql); $row = mysql_fetch_array($query); $array[] = $row['id']; } $implode = implode(",", $array); echo $implode; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891150 Share on other sites More sharing options...
RichardRotterdam Posted August 5, 2009 Share Posted August 5, 2009 Looking at the bigger picture it seems like you are trying to fetch the id's of the categories where it matches an array of names. I think your implode isn't the issue here I think your query is. You could write it in one query instead of a couple of queries in a loop. <?php /** * get a string with id's seperated by commas * @params array names * @return string */ function implodeId($names){ if(is_array($names)){ $sql = "SELECT * FROM categories "; $ids = array(); for($i=0; $i < sizeof($names); $i++){ if($i==0){ $sql .= "WHERE name = '{$names[$i]}' "; }else{ $sql .= "OR name = '{$names[$i]}' "; } } $result = mysql_query($sql); while($row = mysql_fetch_array( $result )) { $ids[]= $row['id']; } return implode(",",$ids); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891168 Share on other sites More sharing options...
Miko Posted August 5, 2009 Author Share Posted August 5, 2009 Hi, I've tried it like Kickstart told and this works just fine and is less type work So this time it works, I got the ID's like this 1,2 Tried the to insert into another table because the other table will contain more values like year, genre etc ... but it seems that he only inserts the first value (1) <?php require("config.php"); class insert_Title { function implodeId($test){ foreach($test as $name){ $sql = "SELECT * FROM categories WHERE name = '$name' "; $query = mysql_query($sql); $row = mysql_fetch_array($query); $array[] = $row['id']; } $insert_sql = "INSERT INTO titles(id,title,genre,year,actors) VALUES(NULL,'test title',' ".implode(" ',' ", $array)." ','2009','test actors')"; $insert_query = mysql_query($insert_sql); if(!$insert_query){ echo "Failed!"; }else{ echo "Success!"; } } } $test = $_POST['test']; $submit = $_POST['submit']; if(isset($submit)){ $get_id = new insert_Title(); $exec = $get_id->implodeId($test); //$test_implode = implode(",",$test); //echo $test_implode; } ?> <form name="myForm" action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <input type="checkbox" name="test[]" value="Movies" />Movies<br /> <input type="checkbox" name="test[]" value="Manga" />Manga<br /> <input type="submit" name="submit" value="Go!" /> </form> :-\ Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891182 Share on other sites More sharing options...
Miko Posted August 5, 2009 Author Share Posted August 5, 2009 found it! had to change my collumn properties in DB, it was an INT instead of VARCHAR. also changed a bit the query from : $insert_sql = "INSERT INTO titles(id,title,genre,year,actors) VALUES(NULL,'test title',' ".implode(" ',' ", $array)." ','2009','test actors')"; to: $insert_sql = "INSERT INTO titles(id,title,genre,year,actors) VALUES(NULL,'test title',' ".implode(",", $array)." ','2009','test actors')"; thanks anyway! Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891209 Share on other sites More sharing options...
kickstart Posted August 5, 2009 Share Posted August 5, 2009 Hi Database design wise it would be better to have a table of genres for a title, possibly several rows per title. This would make it far easier in future if you want to search for all the titles for a particular genre. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/#findComment-891224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.