$ols_produk = $this->db->query("SELECT files FROM galeri_files WHERE id_galeri = '$id'");
$file = $ols_produk->result();
echo "<pre>";
print_r($file);
echo "</pre>";
if($file != ''){
foreach ($file as $key=>$value) {
unlink('gambar/galeri/'.$key);
}
}
when i look in print_r($file);the result is
Array
(
[0] => stdClass Object
(
[files] => g+.png
)
[1] => stdClass Object
(
[files] => andbook.pdf
)
)
and i cant unlink the file because it select the number of array,
how to get the file?
you $files is an array of objects, foreach will get the value as object, and use -> to access the member as this:
foreach ($file as $v) {
unlink('gambar/galeri/'.$v->files);
}
Try this:
foreach ($file as $key=>$value) {
unlink('gambar/galeri/'.$value['files']);
}
Like this.Use codeigniter's result_array() result set to get the values in array format from database.
$ols_produk = $this->db->query("SELECT files FROM galeri_files WHERE id_galeri = '$id'");
$file = $ols_produk->result_array();
echo "<pre>";
print_r($file);
echo "</pre>";
if(count($file)>0){
foreach ($file as $key=>$value) {
unlink('gambar/galeri/'.$value['files']);
}
}
For more see Codeigniter Result Sets
Related
I have a image named Dark-Green.jpg but the output of function is DARK-GREEN.jpg so the image is not displaying due to case-sensitive.
So how can I fetch the image?
UPDATE
Below is my output of the array.
$output = Array
(
[WE05-5040*L] => Array
(
[qty] => 1
[stitching_category] => 2
[sku_image] => skuimages/WE05/DARK-GREEN.jpg
)
)
Then I am using this array in foreach loop like below.
foreach ($output as $ok => $op) {
$itemQty = $op['qty'];
$itemImagePath = $op['sku_image'];
echo "{$ok} has qty: {$itemQty} and the image as below.";
echo "<img src='{$itemImagePath}' width='50%' />"
}
Try this:
function getFile ($filename){
$files = glob($dir . '/*');
$filename = strtolower($filename);
foreach($files as $file) {
if (strtolower($file) == $filename){
return $file;
}
}
return false;
}
I have a result array which looks like this:
Array
(
[formdata] => [{"id":"1"},{"name":"name"}]
)
Now I want to get the value of id but I can't get it right.
The code I've done so far is:
foreach ($text as $key => $file) {
print_r($file['id']); //<--- it shoud print 1
}
$var2 = json_decode($var['formdata'], true);
echo $var2[0]['id'];
Simple Example to what Babak commented:
$var['formdata'] = '[{"id":"1"},{"name":"name"}]';
$var2 = json_decode($var['formdata']);
foreach($var2 as $item) {
echo $item->id;
}
Hi All I have 2 arrays for example in PHP as seen below:
[users] => Array ( [0] => Array ( [username] => Timothy ) [1] => Array ( [username] => Frederic ) )
[users2] => Array ( [0] => Array ( [username] => Johnathon ) [1] => Array ( [username] => Frederic ) [] => Array ( [username] => Peter))
I am trying to compare the contents of each array against each other in order to put a html element, I tried using a nested foreach as seen below:
foreach($users as $user){
foreach ($users2 as $user2){
if($user['username'] == $user2['username']){
echo "<option value=' ".$user['username']."' selected = 'selected'>".$user['username']."</option>";
break;
} else{
echo "<option value=' ".$user['username']."'>".$user['username']."</option>";
}
}
}
my issue is that the items are being echoed more than once which is ruining my select element. Any ideas on how to compare the contents of each?
I want to achieve a list of each name eg:
-Timothy
-Frederic (this should be highlighted as it is in both arrays)
-Johnathon
- Peter
I would take it in a different way.
//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');
//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');
$temp_array = array();
foreach($users as $key => $value) {
$temp_array[$value['username']] = '';
}
foreach($users2 as $key => $value) {
$temp_array[$value['username']] = array_key_exists($value['username'], $temp_array) ? 'DUPLICATE' : null;
}
echo '<select>';
foreach($temp_array as $key_value => $status) {
echo "<option value='{$key_value}' ".(($status == 'DUPLICATE') ? 'selected style="background-color: yellow;"' : '').">{$key_value}</option>";
}
echo '</select>';
I'll let the array take care of it self, if it shares the same key, it will merge, then just flag it with "duplicate".
If there is never any duplicates as you say in each array, the following works for me. This may look a bit complicated, but read through my comments.
You can copy the code and run it in it's own page to see if it works the way you want.
<?php
//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');
//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');
//create a new array to combine all of the data, yes, there will be duplicates
$allData = array();
//add to allData array
foreach ($users as $user) {
$allData[] = $user['username'];
}
//add to allData array
foreach ($users2 as $user2) {
$allData[] = $user2['username'];
}
//create an array that will hold all of the duplicates
$dups = array();
//add any duplicates to the array
foreach(array_count_values($allData) as $val => $c) {
if($c > 1) $dups[] = $val;
}
//remove the duplicates from the allData array
$allData = array_unique($allData);
//echo out form
echo '<select>';
foreach ($allData as $user) {
if (in_array($user, $dups)) {
echo "<option value=' ".$user."' selected = 'selected'>".$user."</option>";
}
else {
echo "<option value=' ".$user."'>".$user."</option>";
}
}
echo '</select>';
?>
However, I'm not sure what your intentions are since IF you had "Peter" and "Frederic" in BOTH arrays, you are not going to get the form you want. But, this works for what you wanted.
I want to take an array, loop it with a foreach loop, and have each array value be sent through a class to get data from a database. This is the code I am currently using:
foreach ($unique_category as $key => $value)
{
$category = $value;
$value = new database;
$value->SetMysqli($mysqli);
$value->SetCategory($category);
$value->query_category();
${"$value_category"} = $value->multi_dim_array();
print_r(${"$value_category"});
echo "<br /><br />";
}
print_r($unique_category[0]."_category");
I want the variable $unique_category[0]."_category" to be ${"$value_category"}.
Currently, the ${"$value_category"} in the foreach loop prints out the correct value/array, while $unique_category[0]."_category" just prints person_category (person being the first value in that array).
How would I go about making $unique_category[0]."_category" print the same thing as ${"$value_category"}?
Thank you
EDIT:
The foreach loop is making a multidimensional array that looks something like this Array ( [0] => Array ( [0] => Home [1] => 9.8 ) [1] => Array ( [0] => Penny [1] => 8.2 )) I want to be able to print out this array outside the foreach loop, with each md array having its own variable name so I can print them out wherever and whenever I want.
Testing without the object
<?php
$unique_category_list = array('foo', 'bar', 'baz');
foreach ($unique_category_list as $key => $value)
{
$category = $value;
$value_category = $value."_".$category;
$unique_category = $unique_category_list[$key]."_category";
$unique_category = ${"$value_category"} = $key;
print_r($unique_category_list[$key]."_category");
echo "\n\n";
}
?>
Outputs:
foo_category
bar_category
baz_category
With the object
<?php
// note that $unique_category is now $unique_category_list && $value is now $category
foreach ($unique_category_list as $key => $category)
{
$database = new Database();
$database->setMysqli($mysqli);
$database->setCategory($category);
$database->query_category();
// http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
// this will invoke the `__toString()` of your $database object...
// ... unless you meant like this
// $value_category = $category."_".$category;
$value_category = $database."_".$category;
$unique_category = $unique_category_list[$key]."_category";
// http://stackoverflow.com/questions/2201335/dynamically-create-php-object-based-on-string
// http://stackoverflow.com/questions/11422661/php-parser-braces-around-variables
// http://php.net/manual/en/language.expressions.php
// // http://php.net/manual/en/language.variables.variable.php
// 'I want the variable $unique_category[0]."_category" to be ${"$value_category"}.'
$unique_category = ${"$value_category"} = $database->multi_dim_array();
}
print_r($unique_category_list[0]."_category");
echo "<br><br>\n\n";
?>
I'm trying to write a function that returns and array of the files names ordered by time modified.
Although I want to get only a specific number of files, and not the whole files in the directory.
In conclusion, I'd like to get an array that contains the newest X files from a directory.
This is my code:
public static function GetPicsDir()
{
$results = array();
$handler = opendir("pics");
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
$results[] = $file;
}
}
closedir($handler);
return $results;
}
I don't know how to limit it and order by time modified.
I'd be glad to get any help.
Thank you
Use filemtime():
public static function GetPicsDir()
{
$results = array();
$handler = opendir("pics");
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
$results[$time] = filemtime($file);
}
}
arsort($results);
closedir($handler);
return $results;
}
You can use glob
$date = strtotime('2013-01-10 10:00:00');//The date from you want to get the files
$matches = glob('dir/*.*');
$result=array();
if (is_array($matches)) {
$a=0;
foreach ($matches as $filename) {
if (filemtime($filename) >= $date) {//only output file >= your $date
$result[$a]['FileName'] = $filename;
$result[$a]['DateCreated'] = gmdate("Y-m-d H:i:s", filemtime($filename));
}
$a++;
}
}
if(count($result)>=2){//order array if it has at least 1 match
foreach ($result as $key => $row) {
$new_array[$key] = $row['DateCreated'];
}
array_multisort($new_array,SORT_DESC,$result);//Show most recent first
}
using array_multisort to sort by dates SORT_DESC
echo '<pre>';
print_r($result);
echo '<pre>';
Output:
Array
(
[0] => Array
(
[FileName] => test.php
[DateCreated] => 2013-10-20 05:43:06
)
[1] => Array
(
[FileName] => test.sql
[DateCreated] => 2013-09-20 23:38:05
)
[2] => Array
(
[FileName] => general.php
[DateCreated] => 2013-09-02 00:58:33
)
)
If you only want to sort the files by last modified date, you can use
ftp_nlist($conn, '-t .');
This will not tell you what the date for each file is, though.
If you want to get the modified date as well, you can use ftp_rawlist and parse the output. Here's a quick example I scraped together:
$list = ftp_rawlist($ftp, '.');
$results = array();
foreach ($list as $line) {
list($perms, $links, $user, $group, $size, $d1, $d2, $d3, $name) =
preg_split('/\s+/', $line, 9);
$stamp = strtotime(implode(' ', array($d1, $d2, $d3)));
$results[] = array('name' => $name, 'timestamp' => $stamp);
}
usort($results, function($a, $b) { return $a['timestamp'] - $b['timestamp']; });
At this point $results contains a list sorted in ascending last modified time; reverse the sort function to get the list in most recently modified first format.