Jump to content

Recommended Posts

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...  :wtf:

Link to comment
https://forums.phpfreaks.com/topic/168906-solved-function-with-implode-wont-work/
Share on other sites

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!

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  :shrug:

 

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

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);
}

}
?>

Hi,

 

I've tried it like Kickstart told and this works just fine and is less type work  ;D

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>

 

  :-\

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!  :D

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.