Jump to content

CSV Help


jimbob-2k7

Recommended Posts

Hi Guys,

 

I'm kinda new at this and just need a little bit of help  :'(

 

I have a csv, with only 1 cell for now, test data is "100,101,102".

 

Then in my database i want to have, each of them on their own row, which works, but as there 3 values, i want the first 1 which is the main value 2 have a Y next to it and then anything else to have an N next to it.

 

this is how i would like it to look ....

 

id           testdata          main

10001       1000              Y

10002       1001              N

10003       1002              N

 

This is the function that i've made ...

 

function add_to_categories ($categories, $prodid) {
if (strstr($categories, ",")) {
$cats = explode (",", $categories);  //explode from comma
} else {
$cats[] = $categories;
}

foreach ($cats AS $cat_to_add) {

$sql = "insert into table1 (id, testdata) values ('', '$cat_to_add') ";
$query = mysql_query($sql);

}

}

 

I've managed to get some code to work which adds them, but i dont know how i would get it to work out the main field ....

 

Any help would be great  ;D

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/72616-csv-help/
Share on other sites

Lots of ways you could do it, if you're sure you only want this field to ever be 'Y' for that value then you can just do it like the below:

 

    foreach($cats as $cat_to_add) 
    {
        $main = ($cat_to_add == '1000') ? 'Y' : 'N';  
        
        $sql = "INSERT INTO table1 (`testdata`, `main`) VALUES ('$cat_to_add', '$main') ";
        $query = mysql_query($sql); 
    }

Link to comment
https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366139
Share on other sites

or try

function add_to_categories ($categories, $prodid) {
if (strstr($categories, ",")) {
$cats = explode (",", $categories);  //explode from comma
} else {
$cats[] = $categories;
}

foreach ($cats AS $cat_to_add) {
$main = $main ? 'N' : 'Y';
$sql = "insert into table1 (id, testdata, main) values ('', '$cat_to_add', '$main') ";
$query = mysql_query($sql);

}

}

Link to comment
https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366150
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.