0

The implode function is not worked!

I want to show the string as follows;

id-101-102-103-104-105

$handle = fopen($_FILES['file']['tmp_name'], "r");
while(!feof($handle)) {
	$data = fgets($handle);
	$listing = explode(",", $data);	
	$id = $listing[0];
	$name=$listing[1];  // this is second column data.
	$telephone = $listing[2];
	$email = $listing[3];

	$id1 = implode("-,$id);
	
	echo "id = " . $id . "<br />";
7
Contributors
17
Replies
18
Views
6 Years
Discussion Span
Last Post by cliffcc
0

You forgot a " on line 10

$id1 = implode("-", $id);

and $id isn't an array but a single value so you can't implode that (implode takes an array).

0

please check this line of code $id1 = implode("-,$id);


try this one

$id1 = implode("-",$id);
0

It cannot work? Could anyone help!


Since the data is from a csv file, i just get the data from one column. "$id" is id101102103104105, i want to chsnge it to id-101-102-103-104-105

0

implode works on array not on string
so you need to replace spaces with hyphen.

I am assuming only one space between all ids

$id1 = str_replace(" ","-", $id);
0

Are all numbers 3 digit? Trivial solution:

$str = "id101102103104105";
$out = str_split(substr($str,2), 3);
echo "id-" . implode("-",$out);

Works for me if 'id' always the prefix and all numbers are 3 digit.

Edited by diafol: n/a

0

Why the result is

id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105id-101-102-103-104-105

0

Actually, i import the csv file and then i want to get the particular cell value? (coluumn one row three). I know how to get all the column value (first column value) as previously mentioned, but it is data set. But i don't know how to get the value of row 3 in column value data set? So i need to seperate the column value to get what i want?

0

I upload the file for you to check! Thanks all


The csv file as below:

id name telephone email
1000001 A 2323232
1000002 B 2323232
1000003 C 2323232
1000004 D 2323232
1000005 E 2323232
1000006 F 2323232
1000007 G 2323232
1000008 H 2323232
1000009 I 2323232
1000010 J 2323232

Edited by cliffcc: n/a

Attachments
<form enctype="multipart/form-data" action="importCSV.php" method="POST">
<p>Choose a file to upload : <input name="file" type="file" /></p>
<p><input type="submit" value="Upload File" /></p> </form>


<?php



   if(is_uploaded_file($_FILES['file']['tmp_name'])){
	//Connect to the database
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$dbname = 'visualupload';
	$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
	mysql_select_db($dbname);



        $handle = fopen($_FILES['file']['tmp_name'], "r");
        while(!feof($handle)) {
	$data = fgets($handle);
	$listing = explode(",", $data);	
	$id = $listing[0];



        $str = $id;
        $out = str_split(substr($str,2), 7);
        $id2 = implode("-",$out);
        $output = explode("-", $id2);


        echo"$output[2]";



	
        
	}
	mysql_close($link);
        }


  

?>
0

OK, this is a slightly different question to the one you originally asked. Your file has mysql details. DO you need to involve a DB? The code does not involve a DB.

If you want to access cols/rows data from csv, you need to create a multidimensional array, like:

array[row][col]

$str = file_get_contents($file);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(" ",$line); 
}

Not tested - so you may need to fiddle with it. You could build with

function getRowCol($array,$row,$col){
  return $array[$row][$col];
}
0

I don't know how to use it! Could you give me more support!

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>

Edited by cliffcc: n/a

0
for(){
 if (condition) {
           $errors[] = 'something.';
    }
}

echo implode('<br/>',$errors);

$errors contains multiple data or indexed value if you want to show that error in a next line then user implode. hope u understand

Edited by Dani: Formatting fixed

0

sorry , i just want to know what is the error ?

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>
0

i just want to get the value of specific cell value from the imported csv. Anyone can help ! Can anyone provide the code?

0

What is the mistake about the code?

<?php


$str = file_get_contents('./info.csv', true);
$lines = explode("\n",$str);
foreach($lines as $line){
 $array[] = explode(",",$line);
}




function getRowCol($array,$row,$col){
  return $array[$row][$col];
}

getRowCol($array,3,1);


?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.