jimbob-2k7 Posted October 10, 2007 Share Posted October 10, 2007 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 Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/ Share on other sites More sharing options...
lessthanthree Posted October 10, 2007 Share Posted October 10, 2007 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); } Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366139 Share on other sites More sharing options...
jimbob-2k7 Posted October 10, 2007 Author Share Posted October 10, 2007 Thanks for the reply the first number wont always be 1000, is there anyway we can get it so the first vaule will be a Y and the rest N? Thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366141 Share on other sites More sharing options...
lessthanthree Posted October 10, 2007 Share Posted October 10, 2007 $n = 0; foreach($cats as $cat_to_add) { $main = ($n == 0) ? 'Y' : 'N'; $sql = "INSERT INTO table1 (`testdata`, `main`) VALUES ('$cat_to_add', '$main') "; $query = mysql_query($sql); $n++; } Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366146 Share on other sites More sharing options...
sasa Posted October 10, 2007 Share Posted October 10, 2007 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366150 Share on other sites More sharing options...
jimbob-2k7 Posted October 10, 2007 Author Share Posted October 10, 2007 Wicked thank you both ;D Quote Link to comment https://forums.phpfreaks.com/topic/72616-csv-help/#findComment-366167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.