Jump to content

PHP To CSV Help - Coding included.


jayfcf

Recommended Posts

Hi Everyone,

 

I am having some trouble with getting my form to write the contents to csv correctly.

 

Everything is going in to the CSV but not in the correct place.

 

The Name, Email and Mobile fields are being displayed in a row (a1, b1, c1) but the $checkmsg options are creating new rows for every option ticked. (a2, a3, a4)

 

I would like everything from the form to be on the one row (a1, a2, a3, a4, a5, a6) etc.

 

Any help with this would be very appreciated.

 

 

 

if(isset($_POST['Submit'])){

  $name = $_POST['name'];

  $email = $_POST['email'];

  $mobile = $_POST['mobile'];

  $err = '';

 

  foreach($_POST['check'] as $value) {

$check_msg .= "Yes: $value\n";

}

 

  if(trim($name)==''){

      $err .= '-Please enter a name<br>';

  }

  if(empty($email)){

      $err .= '-Please enter an email address';

  }

    if(empty($mobile)){

      $err .= '-Please enter amobile number';

  }

 

  if($err!=''){

      echo $err;

  }

  else{

      $filename = 'file.csv';

      $somecontent = $name . ',' . $email . ',' . $mobile . ',' . $check_msg . "\n";

     

      // Let's make sure the file exists and is writable first.

      if (is_writable($filename)) {

     

        // In our example we're opening $filename in append mode.

        // The file pointer is at the bottom of the file hence

        // that's where $somecontent will go when we fwrite() it.

        if (!$handle = fopen($filename, 'a')) {

            echo "Cannot open file ($filename)";

            exit;

        }

     

        // Write $somecontent to our opened file.

        if (fwrite($handle, $somecontent) === FALSE) {

            echo "Cannot write to file ($filename)";

            exit;

        }

       

        echo "Success, wrote ($somecontent) to file ($filename)";

       

        fclose($handle);

     

      } else {

        echo "The file $filename is not writable";

      }

  }

}

 

Many thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/205178-php-to-csv-help-coding-included/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.