top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

I want to parse the csv contents into an array using php?

0 votes
318 views

The output is in this form

Array (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana ) 

But I want the output in this form

  Array[1] (
     [0] => name
     [1] => email
     [2] => contact
     [3] => address 
    ) 
Array[2] (
     [0] => sant
     [1] => sant@gmail.com
     [2] => **********
     [3] => haryana
    ) 
posted May 9, 2017 by Kushal S

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
Kushal  could you please share your code?? it would be better for me to understand the exact prob...

Similar Questions
+1 vote

I just wanted to see the best way to securely accomplish this task. When we want to update a DB we upload to a writable directory instead of writing it directly to MySQL, I don't like having writable directories if possible.
Is there a right or better way to accomplish this?

0 votes

I am trying to export certain data from my PHP form to CSV. I can echo out to screen during testing and I can also export to CSV the static test data (stored in the $contents array) you see below. But I am stuck trying to export the certain fields that I only need to export.
This is my code

// How do I get this info into the CSV?
/*foreach ( $entries as $entry ) :  
    echo $entry['2'];
    echo $entry['3'];
    echo $entry['6'];
endforeach;*/

$csv_headers = [
    'Organisation Name',
    'Registered Charity Number',
    'Address',
    'Phone',
];

$contents = [
  [2014, 6, '1st half', 'roland@fsjinvestor.com', 0, 0],
  [2014, 6, '1st half', 'steve@neocodesoftware.com', 0, 0],
  [2014, 6, '1st half', 'susanne@casamanager.com', 0, 0],
  [2014, 6, '1st half', 'tim', 0, 0]
];

fputcsv($output_handle, $csv_headers);

foreach ( $contents as $content) :
    fputcsv($output_handle, $content);
endforeach;
0 votes
for($i=0;$i<=feof($getdata);$i++)
{
if (filter_var($data[$i][1], FILTER_VALIDATE_EMAIL)){
echo $data[$i][1];
$email=$data[$i][1];
$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql ="INSERT INTO promo_user (uid,name,email) VALUES (,'', '$email')";
mysqli_query($sql,$conn);
mysqli_close($conn);

I am using the above code but there is something wrong with it,whenever i run the code the echo is working fine but the content does go into sql table

Please help

+2 votes

Is there a better way to do that:

def Read_CSV_File(filename):
 file = open(filename, "r")
 reader = csv.DictReader(file)
 line = 1
 for row in reader:
 if line < 6:
 reader.next()
 line++
# process the CSV
...