ollie007 Posted June 9, 2009 Share Posted June 9, 2009 How do you add to an array from a textarea input? I guess if you add a comma, then you can add a straight array?? $array = ("$_POST[$string]"); How would you do it for linebreaks?? and secondly, is it a security risk if you allow input like this to your DB? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/ Share on other sites More sharing options...
Garethp Posted June 9, 2009 Share Posted June 9, 2009 Ok A) You add it like this $array[] = "Input"; B) You'd do it like $array[] = mysql_real_escape_string($_POST[$string']); The mysql_real_escape_string escapes the string, so it's no long a security risk On a side note $W00T = mysql_real_escape_string($_POST['200TH!']); Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852125 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 cool ty. how do i insert with a linebreak?? eg data1 data2 data3 instead of data1,data2,data3 Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852128 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Care to be a little more descriptive? Do you meen 'how do I retain linebreaks when inserting data into a database?' Unless you go out of your way to remove them, linebreaks are saved with your data. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852130 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Share Posted June 9, 2009 Exactly. But, you want to retain your line breaks as <br>'s, you use nl2br() Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852134 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Exactly. But, you want to retain your line breaks as <br>'s, you use nl2br() Not on the way into your database though. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852136 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 i just need each line to be a set of data, linebreaks means nothing internally they are a way for the user to see what they are entering or maybe a linebreak is take by array[] as a single data? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852137 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 Profile Menu:\r\n \r\n http://yakkityyak.x24hr.com \r\n i tried the "mysql_real_escape_string" and i got the \r\n on the output?? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852150 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Again, you need to explain what it is you are doing. Your statements don't mean allot to people reading your posts. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852154 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 welll when people post data1,data2,data3 FROM one textarea i would like it to be inserted into a database (already done), and THEN to be retrieved into an array, ie data1,data2,data3 into one cell !!! So data from one cell delineated by comma BUT manipulated as single data entities?? Are we on the same page? PS. I am NOT adding to the array the normal way like array[]; I am just gonna grab the data from the db and write out the array !! $string='apples, oranges, bannana' $array = ($string); Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852163 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 You mean like $post = $_POST['textarea']; $post = explode("\n", $post); ? oooooh wait, you mean like.... $sql= /*your sql data*/; $data = array(); while($fetch = $mysql_fetch_array($sql)) { $array = explode(',', $fetch['row']); array_push($data, $array); } Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852167 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 I am NOT adding to the array the normal way like array[]; I am just gonna grab the data from the db and write out the array !! $string='apples, oranges, bannana' $array = ($string); the reverse of this to be honest $sql= /*your sql data*/; $data = array(); while($fetch = $mysql_fetch_array($sql)) { $array = explode(',', $fetch['row']); array_push($data, $array); } but i want to remove line breaks and add the commas where line breaks are Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852171 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 str_replace("\n", ',', $data); ?? I'm having a hard time understanding tbh... Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852172 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 If you have a string that looks like this "foo,bar,bob" and you want to turn it into an array you will need to use implode. eg; $stringfromdb = "foo,bar,bob"; $array = implode(',', $stringfromdb); Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852174 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 If you have a string that looks like this "foo,bar,bob" and you want to turn it into an array you will need to use implode. eg; $stringfromdb = "foo,bar,bob"; $array = implode(',', $stringfromdb); What? Doesn't implode create a string from an array..? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852175 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Sorry, my bad. You need explode. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852177 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 You mean like $post = $_POST['textarea']; $post = explode("\n", $post); ? oooooh wait, you mean like.... $sql= /*your sql data*/; $data = array(); while($fetch = $mysql_fetch_array($sql)) { $array = explode(',', $fetch['row']); array_push($data, $array); } Yeah, already suggested that. Apparently it's unsatisfactory though... Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852180 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 $stringfromdb = "foo,bar,bob"; $array = implode(',', $stringfromdb); hahha almost there LOL what about $stringfromdb = "foo bar bob"; with the spaces?? or linebreaks? what does mysql do with a linebreak on insert? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852184 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 ...creates a line break. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852186 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 what does mysql do with a linebreak on insert? Nothing. I said that several replies ago. if you want to split on a space use.... $array = explode(' ' , $stringfromdb); Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852188 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 how how does explode deal with the linebreak? is it removed automatically? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852190 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 What do you actually want to do? Split on a space, line break, create from an array, fetch data from the database or what? You've suggested all of these and we've provided a solution. I still don't understand what you actually want to do. You're not being very clear... And explode removes the delimiting character and creates an array of each part around it. You can read the manual for more info. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852192 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 is it removed automatically? No, why would it be? Do you want to split on a line break? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852193 Share on other sites More sharing options...
ollie007 Posted June 9, 2009 Author Share Posted June 9, 2009 $post = $_POST['textarea']; $post = explode("\n", $post); what does this do?? Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852195 Share on other sites More sharing options...
jxrd Posted June 9, 2009 Share Posted June 9, 2009 explode. Link to comment https://forums.phpfreaks.com/topic/161478-how-to-add-to-an-array/#findComment-852198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.