0

I have a csv file with data I would like the data to be filled in the empty spaces. then export to excel file or into a database for example. I would like the first column to copy the first Product A to fill the other spaces

For example
    Type       Description Cost
    Product A   rrrrrrrr    33.00
    NEEDS FILL  ttttttttt   45.00
    NEEDS FILL  eeeeeeeee   50.00
    Product B   yyyyyyyyy   80.00
    NEEDS FILL   qqqqqqq    90.00
    NEEDS FILL  iiiiiiii    100.00

 Needs to look like this 

    Type       Description Cost
    Product A   rrrrrrrr    33.00
    Product A ttttttttt     45.00
    Product A  eeeeeeeee    50.00
    Product B   yyyyyyyyy   80.00
    Product B   qqqqqqq     90.00
    Product B  iiiiiiii     100.00

The file result will be written in a file please help been scratching my head

3
Contributors
18
Replies
23
Views
5 Years
Discussion Span
Last Post by LastMitch
0

@tapuwa2002

I have a csv file with data I would like the data to be filled in the empty spaces. then export to excel file or into a database for example. I would like the first column to copy the first Product A to fill the other spaces

Can you provide the query so we have a better understanding what you are trying to do?

Base on your chart, you need something like this query in order to copy the data from the first column to the second column:

INSERT INTO
SELECT 
FROM 

It's much easier if you have the query so you can plug in to the statement.

Edited by LastMitch: grammer

0

Please help am really new to this. Product A will still be in the table all i want is todo catch the blank spaces and add in product A and when it goes to Product B it will fill in the respective after that. hope it makes sense.

0

@tapuwa2002

Please help am really new to this. Product A will still be in the table all i want is todo catch the blank spaces and add in product A and when it goes to Product B it will fill in the respective after that. hope it makes sens

Do you have database?

If yes, then there should be a query like this:

$mysqli->query("SELECT * FROM table")

or

$query = "SELECT * FROM table";

If you don't have a database, then I should suggested you create one.

Here is a link on how to create a database:

This one is MYSQLI

http://www.daniweb.com/web-development/php/code/434480/using-phpmysqli-with-error-checking

or

This one is MYSQL

http://www.daniweb.com/web-development/php/code/434415/using-phpmysql-with-error-checking

Edited by LastMitch: grammer

0

file_get_contents() to get the whole csv file into a string.
explode() the string on new line character giving many single $line strings.
foreach loop through all $lines:
explode() the $line on ',' into $pieces
foreach loop through $pieces.
if first $piece of line, check value:
if holds value like "Product A", store in $variable.
if holds value like "", change value to $variable.
implode() $pieces to $line using "," as glue.
add "\n" to each $line.
store $line in $newLines array.
foreach loop through $newLines array adding to DB, outputting to excel format...

0

Thanks will it loop now if it sees Product B. What it should do is if there is a value anything after that if its empty copy that value. I will try now.

0

@tapuwa2002

Since you test out Alex's code then I assume you do have a DB then you should provide a query so you can used Alex code. Right now, it's clueless because you don't know how to make Alex code to work with your query?

Edited by LastMitch: grammer

0

How to, all i need is to duplicate. I have a DB. Last its a bit complicated than that. trust me am trying.

0

@tapuwa2002

How to, all i need is to duplicate. I have a DB. Last its a bit complicated than that. trust me am trying.

I want you to know that I'm not being critical. Don't get frustrated and take it personal. It's just much easier to have a query.

Unless you have other languages (beside php & mysql) that is involve with this then maybe it's more complicated than.

In order to duplicate the data you need a query to select the data from Table 1 and insert the data from Table 1 to Table 2.

Does that make sense?

Adam's code provide you a guideline but it's not a code. Meaning you have to write the code out base on Adam's guideline.

Edited by LastMitch: grammer

0

Thanks guys for the feed back I @adam will send through and at LastMitch i will send through the sql statements.

0

Regarding the sql part is there a way where I select the first column get the amount of records then do a for loop that will check on the records.

0

@tapuwa2002

Regarding the sql part is there a way where I select the first column get the amount of records then do a for loop that will check on the records.\

Do you have a actual name for the 2 columns? Are the 2 columns are in the same table or separate table?

0

I do know my sql the prob is that I need to add the missing fields. I appreciate your help guys. I do know a bit but I need to normalize a table but for it to work I need to fill in the empty spaces from a csv to a table. Its like 1:47am here I guess am just exhausted will look at it in the morning.

0

Check out my code

<?php
$row = 1;
if (($handle = fopen("bk1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, filesize('bk1.csv'), ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
///first column A1 has the type "Product A"
$store=$data[1][0]
///Now if next row position 0 is empty then add whats stored
if ($data[$c][0]==""){
$data[$c][0]=$store;
          }
/// or else if the row is not empty then take that new postion values put in the array and continue to loop.
elseif($data[$c][0]!==""){
$store==$data[$c[0]];
}
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
0

@tapuwa2002

I do know my sql the prob is that I need to add the missing fields. I appreciate your help guys. I do know a bit but I need to normalize a table but for it to work I need to fill in the empty spaces from a csv to a table. Its like 1:47am here I guess am just exhausted will look at it in the morning.

OK, instead of going into a database.

Try this Import CSV file directly into MySQL:

http://www.tech-recipes.com/rx/2345/import_csv_file_directly_into_mysql/

or try this Quick CSV import with visual mapping

http://i1t2b3.com/2009/01/14/quick-csv-import-with-mapping/

This will solve you some headache.

As for your code I thought you are copying data from 1 column to another column?

This is what I came up base from the link I read from there and variables you provided:

// adjust to actual number expected 
$numColumns = 2; 

if(($fh = fopen("bk1.csv", "r")) !== false) { 
   $values = array(); 
   $count = 0; 
   while(($row = fgetcsv($fh)) !== false) { 
      if(count($row) == $numColums) { 
        $values[] = "(" . implode(', ', array_map($row)) . ")"; 
      } 
   } 

   // run query in $row: 
   foreach($values => $row) {
      $sql = "INSERT INTO `table_name` (`Type1`, `Type2`) " 
           . "VALUES " . implode(",\n", $value); 
      $result = mysql_query($sql) or die(mysql_error()."<br />\n$sql"); 
   } 
 } 

Edited by LastMitch: grammer

This question has already been answered. 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.