NathanLedet Posted July 15, 2008 Share Posted July 15, 2008 I have code putting data into a MySQL database divided by commas. Here's the break down: Before being put into the database: // file names selected from previous page $files = $_POST['files']; //Array which separates each file name with a comma, and then creates a single string foreach ($files as $f => $value) { $str_files_db .= $f . ","; } //Put it in the database $query = "INSERT INTO downloads (files) VALUES (\"$str_files_db\")"; mysql_query($query) or die(mysql_error()); Now, the string gets put into the database like so: filename1,filename2,filename3, Note the additional comma at the end of filename3 Onto another page where we get this data: //$dlcode is a random code in the browser bar $dlcode = $_GET['code']; //Just checking to make sure it works echo $dlcode; //Grab the data where the $dlcode matches up with randkey inside the database (It's there, I just didn't put it in my query above, to make it a bit easier to read) $query = "SELECT files FROM downloads WHERE randkey = '$dlcode'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); //Everything there? Ok! display it! echo $row['files']."<br />"; //Now just setting it up as a variable $files = $row[ "files" ]; //for each comma, split it up and make an array with each file name $strippedfiles = split(',', $files); //Display the Array print_r($strippedfiles); now, when i see the array, it looks something like this: Array([0] => filename1 [1] => filename2 [2] => filename3 [3] = > ) Because there is an extra comma at the end of filename3, the array has created a 4th position which has no data in it. How can I run my initial foreach loop and delete the comma on the very end? or am I doing this the hard way? Thanks for any assistance! Quote Link to comment https://forums.phpfreaks.com/topic/114922-comma-adding-extra-value-to-array/ Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 or trim it //Array which separates each file name with a comma, and then creates a single string foreach ($files as $f => $value) { $str_files_db .= $f . ","; } $str_files_db = trim($str_files_db, ","); Quote Link to comment https://forums.phpfreaks.com/topic/114922-comma-adding-extra-value-to-array/#findComment-591066 Share on other sites More sharing options...
kenrbnsn Posted July 15, 2008 Share Posted July 15, 2008 Instead of <?php foreach ($files as $f => $value) { $str_files_db .= $f . ","; } //Put it in the database $query = "INSERT INTO downloads (files) VALUES (\"$str_files_db\")"; ?> do <?php //Put it in the database $query = "INSERT INTO downloads (files) VALUES ('" . implode("','",$files) . "')"; ?> ken Quote Link to comment https://forums.phpfreaks.com/topic/114922-comma-adding-extra-value-to-array/#findComment-591067 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 ahh very true kenrbnsn , (i really need to read all the code, lol) kenrbnsn code is faster.. but looping works better if you need to filter the data.. Quote Link to comment https://forums.phpfreaks.com/topic/114922-comma-adding-extra-value-to-array/#findComment-591068 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.