Jump to content

Comma adding extra value to array


NathanLedet

Recommended Posts

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.