PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 4, 2022

[FIXED] How to separate the full name that has comma in excel using php

 October 04, 2022     php, phpexcel, sql     No comments   

Issue

I have a project that gets the name from excel and stores the value in the first name and last name. The problem is that in the excel file the name is stored (for example John, Constantine) how do I get the John and Constantine and store it in two different variables?


if(isset($_POST['excel_btn']))
{
    require('import/PHPExcel/PHPExcel.php');
    require('import/PHPExcel/PHPExcel/IOFactory.php');

    $file=$_FILES['myFile']['tmp_name'];
    

    $obj=PHPExcel_IOFactory::load($file);
    foreach($obj->getWorksheetIterator() as $sheet)
    {
        $getHighestRow=$sheet->getHighestRow();
        for($i=1; $i<=$getHighestRow; $i++){
            $name=$sheet->getCellByColumnAndRow(0,$i)->getValue();
           
             if($name !=''){
                 $query = "INSERT INTO users(name) VALUES('$name')";
            
            $query_run=mysqli_query($conn, $query);

            }
    }
}

this is what I wrote so far but with that, the full name is stored in the variable $name


Solution

(Updated)

Use explode :

$pieces = explode(",", $name);
echo trim($pieces[0]); // John
echo trim($pieces[1]); // Constantine

Or, as @Markus Zeller:

list($first, $last) = explode(',', 'John, Constantine')
echo trim($first);
echo trim($last);


Answered By - Monnomcjo
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing