I have this variable that INSERTs
to a db the attached filename from a html form input.
$insertAttachments->execute( array(':attachment2' => $attachment2) );
Now the problem is it INSERTs
only one filename although the input is set to allow multiple. So I tried to do this:
$uploads = count($attachment2); //counts the number of attachments
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( array(':attachment2' => $attachment2) );
}
which left me wondering where to place [$i]
so that it iterates over array(':attachment2' => $attachment2)
?
If I assign it a variable and have it like -
$alluploads = array(':attachment2' => $attachment2);
for($i=0; $i<$uploads; $i++){
$insertAttachments->execute( $alluploads[$i] );
}
It runs into an error. Any ideas how I should pull this off? I'm extending a php 5.3 / slim framework application.
Foreach let's you iterate over an associative array.
$alluploads = array(':attachment2' => "attachment2");
foreach($alluploads as $key => $value){
echo "key: " . $key . " Has value: ". $value ."\n";
}
Here is an example with a for loop using array_keys to get the associative array keys and using that in the loop.
$alluploads = array(':attachment2' => "attachment2");
$keys = array_keys($alluploads);
for($i=0;$i<count($keys);$i++){
echo "key: " . $keys[$i] . " Has value: ". $alluploads[$keys[$i]] ."\n";
}
And as a third example you can use array_values to remove the associative keys.
This method should only be used if you know all the values in the arrays are uploads and should be treated the same.
$alluploads = array(':attachment2' => "attachment2");
$alluploads = array_values($alluploads);
for($i=0;$i<count($alluploads);$i++){
echo "key: " . $i . " Has value: ". $alluploads[$i] ."\n";
}
You can use a foreach
loop, and that way you don't need the count, like this:
foreach ($attachment2 as $a){
$insertAttachments->execute( array(':attachment2' => $a['some index with the file path']) );
}
Don't forget to change the index o the array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.