Counting in php - php

When I use my glob It lets me list files. I would like to be able to count the files but not the ones in my not count array();
When I echo the count it counts 3 but should only echo count 2 there is no other file.
How do I make sure counts correct?
<?php
class Count extends MX_Controller {
public function index() {
$name = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');
$not_count = array(
FCPATH . 'application/modules/admin/controllers/common/dashboard.php',
FCPATH . 'application/modules/admin/controllers/common/footer.php',
FCPATH . 'application/modules/admin/controllers/common/header.php',
FCPATH . 'application/modules/admin/controllers/common/login.php',
FCPATH . 'application/modules/admin/controllers/common/logout.php',
FCPATH . 'application/modules/admin/controllers/common/menu.php',
FCPATH . 'application/modules/admin/controllers/common/register.php',
FCPATH . 'application/modules/admin/controllers/dashboard/customer_total.php',
FCPATH . 'application/modules/admin/controllers/dashboard/online.php',
FCPATH . 'application/modules/admin/controllers/dashboard/user_total.php',
FCPATH . 'application/modules/admin/controllers/error/permission.php'
);
var_dump($not_count);
echo "<br/>";
echo "<br/>";
var_dump($name);
// Not Counting Correct When Echo Out Puts 3 Should be 2
$filecount1 = count(array_diff($not_count, $name));
echo "<br/>";
echo "<br/>";
echo $filecount1;
}
}
Var Dump Not Count Array
array(11) { [0]=> string(14) "customer_total" [1]=> string(9) "dashboard" [2]=> string(6) "footer" [3]=> string(6) "header" [4]=> string(5) "login" [5]=> string(6) "logout" [6]=> string(4) "menu" [7]=> string(6) "online" [8]=> string(10) "permission" [9]=> string(8) "register" [10]=> string(10) "user_total" }
Var Dump All Files Out Put
array(13) { [0]=> string(95) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/dashboard.php" [1]=> string(92) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/footer.php" [2]=> string(92) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/header.php" [3]=> string(91) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/login.php" [4]=> string(92) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/logout.php" [5]=> string(90) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/menu.php" [6]=> string(94) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/common/register.php" [7]=> string(103) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/dashboard/Customer_total.php" [8]=> string(95) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/dashboard/Online.php" [9]=> string(99) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/dashboard/User_total.php" [10]=> string(95) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/error/permission.php" [11]=> string(110) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/extension/extension_permissions.php" [12]=> string(88) "C:\Xampp\htdocs\project1\application/modules/admin/controllers/user/user.php" }

Just use array_diff() to only count the difference like this:
echo count(array_diff($files, $ignore));
EDIT:
Your question is unclear, but I think you may want something like this:
echo count(array_filter(array_map(function($v)use($ignore){
return in_array(pathinfo(basename($v))["filename"], $ignore);
}, $files)));

Related

output an Array with same values

I have a SQL Query result (array): "title", "content" and "name"
Here is my var_dump:
array(28) {
[0]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(43) "https://www.wrike.com/open.htm?id=440908999"
["name"]=>
string(14) "Christian Wahl"
}
[1]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(5) "MySQL"
["name"]=>
string(14) "Christian Wahl"
}
[2]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[3]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[4]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(7) "content"
["name"]=>
string(9) "Elena Ott"
}
(I cut off the end of the array to make it a little clearer to see)
These are Tasks who are assigned to a User.
Now i want to output the "Name" (panel-heading)
and the "title" and "content" (panel-body).
It should look smth like this but for each given name:
how it should look like
I tried to find a solution on my own, but without success :(
I hope u can help me?
thx a lot
-Taddl
just loop on your data and render it
$data = [];
foreach ($databaseResult as $row) {
$data[$row['name']][] = $row;
}
foreach($data as $name => $stuff) {
echo $name . '<br>';
foreach($stuff as $row) {
echo $row["title"] . ':' . $row["content"] . '<BR>';
}
}
You can use foreach loop. As example:
foreach($yourarrayvariable as $data)
{
echo "<tr>";
echo "<td class='heading'>".$data['name']."</td>";
echo "<div class='content'>";
echo "<td>".$data['title']."</td>";
echo "<td>".$data['content']."</td>";
echo "</div>";
echo "</tr>";
}
And create your desired template look classes as per your need inside foreach.

Data read and output with a loop

I make an event list with a maximum of 4 events, saved in its own jason files.
I wrote this code for the output.
JSON:
{"field1":"new york","field2":"1553404200","field3":1554415200}
CODE:
$jsonA = file_get_contents('code/data1.json');
$fieldsA = json_decode($jsonA, true);
$cityA = $fieldsA["field1"];
$dateStartA = $fieldsA["field2"];
$dateEndA = $fieldsA["field3"];
$jsonB = file_get_contents('code/data2.json');
$fieldsB = json_decode($jsonB, true);
$cityB = $fieldsB["field1"];
$dateStartB = $fieldsB["field2"];
$dateEndB = $fieldsB["field3"];
<div><?php echo $cityA . $dateStartA . " - " . $dateEndA?></div>
<div><?php echo $cityB . $dateStartB . " - " . $dateEndB?></div>
The problem is:
I have 4 events with each start and end date. So I have to do the same 8 times, but I do not want and should copy this code 8 times with a higher variable name...
In my project I have 13 fields and also 2 arrays to output the day and month name in my language. That's why it's so important not to copy everything 8 times.
I need a loop, but I do not know exactly how. I'm an absolute beginner, so please as simple as possible.
Great job with your attempt!
You might just want to write a foreach and read your files and store it in an array.
In your local server, you might just want to find the absolute path to your code folder. If you open your terminal, and CTRL+C code directory and CTRL+V in your terminal, you might see what your absolute path may look like. Then, paste upto before code directory in $server_path. Then, run the code, hopefully it would work.
function searchFilenames($array, $re)
{
return preg_grep('/' . preg_quote($re, '/') . '/i', $array);
}
$server_path = '/path/to/your/localserver/';
$dir = $server_path . 'code';
$filename_array = searchFilenames(glob($dir . "/*"), 'data');
$fields = array();
foreach ($filename_array as $key => $filename) {
$jsonA = file_get_contents($filename);
$values = json_decode($jsonA, true);
$fields[$key]["city"] = $values["field1"];
$fields[$key]["dateStart"] = $values["field2"];
$fields[$key]["dateEnd"] = $values["field3"];
}
var_dump($fields);
Output
array(8) {
[0]=>
array(3) {
["city"]=>
string(8) "new york"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[1]=>
array(3) {
["city"]=>
string(6) "boston"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[2]=>
array(3) {
["city"]=>
string(7) "chicago"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[3]=>
array(3) {
["city"]=>
string(13) "washington dc"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[4]=>
array(3) {
["city"]=>
string(5) "miami"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[5]=>
array(3) {
["city"]=>
string(10) "los angles"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[6]=>
array(3) {
["city"]=>
string(7) "seattle"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
[7]=>
array(3) {
["city"]=>
string(6) "austin"
["dateStart"]=>
string(10) "1553404200"
["dateEnd"]=>
int(1554415200)
}
}

Filled up $_FILES doesn't return file extension

I wrote this short script to upload multiple files, based on an old topic from Stackoverflow : How do you loop through $_FILES array?
(I didn't reply into it as it's 7 years old... Let me know if I did wrong)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
So basically, I'm sending multiple files in $_FILES['images'] and I create unique names before upload (using time() + $i). I never get the file extension as $_FILES['images']['name'] seems to be empty. It's not though as Var_dump returns a complete array with everything I need :
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
I'd also need ['type'] to test the files but same thing : I Can't get to return the array's content.
Do you see something wrong in this script ?
$_FILES['images']['tmp_name'] does not contain an extension, that is the temp file PHP made of the uploaded file.
If you want the filename with extension of the file that was uploaded from the users PC, you need to look in $_FILES['images']['name']
So
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. strtolower(substr(strrchr($_FILES['images']['name'][$index], '.'),1));
// changed here -----------------------------^^^^^^^^^^^^^^^^
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
Also you can simplify that bunch of functions that get the extension to
$img_url = url_rewrite($intitule)
.'-'
. time()
. $i
. '.'
. pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);
$gallery = $gallery . '|' . $img_url;

Access multidimensional array in php?

array(10) {
["table_name"]=>
string(0) ""
["chart_type"]=>
string(5) "Table"
["chart_name"]=>
string(9) "Veterans "
["dashboard_name"]=>
string(7) "Default"
["data_option"]=>
string(0) ""
["limit_value"]=>
string(1) "0"
["view_status"]=>
bool(true)
["view_priority"]=>
string(0) ""
["chart_data"]=>
array(72) {
[0]=>
array(11) {
[0]=>
string(4) "Rank"
[1]=>
string(6) "Branch"
[2]=>
string(12) "Level 3 Name"
[3]=>
string(12) "Level 4 Name"
[4]=>
string(23) "Logged Premia Score MTD"
[5]=>
string(26) "Converted Premia Score MTD"
[6]=>
string(21) "Persistancy Score MTD"
[7]=>
string(23) "Logged Premia Score YTD"
[8]=>
string(26) "Converted Premia Score YTD"
[9]=>
string(21) "Persistancy Score YTD"
[10]=>
string(5) "Score"
}
[1]=>
array(11) {
[0]=>
float(1)
[1]=>
string(22) " GWALIOR DIRECTSegment"
[2]=>
string(29) "G00563 - GOVIND SINGH KUSHWAH"
[3]=>
string(21) "S03643 - Shishir Jain"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.9994)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.89901)
}
[2]=>
array(11) {
[0]=>
float(2)
[1]=>
string(26) " KURUKSHETRA DIRECTSegment"
[2]=>
string(23) "A02311 - Amarjeet Singh"
[3]=>
string(21) "S00927 - Sachin Kumar"
[4]=>
float(1)
[5]=>
float(1)
[6]=>
float(1)
[7]=>
float(0.953)
[8]=>
float(1)
[9]=>
float(1)
[10]=>
float(9.82245)
}
}
["color_scheme_name"]=>
string(7) "Default"
}
How do i display the chart_data in php
$arr=json_decode($row[0],TRUE);
var_dump($arr);
foreach($arr as $row)
{
foreach($row['chart_data'] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
EDIT:
for ($x=0; $x<=count
($arr['chart_data']); $x++) {
foreach($arr['chart_data'][$x] as $key=>$val)
{
echo $val;
}
}
You are having 2 foreach calls.
The first one passes all keys of the outer array, amongst which chart_data.
The second one looks at the children of the outer array, and looks for chart_data in each child. But is obviously not finding any.
Change your code to this:
$arr=json_decode($row[0],TRUE);
foreach($arr['chart_data'] as $k){
echo $k['Rank'];
echo $k['Branch'];
}
edit:
I see. (Thanks for indenting your array) Your problem is off course that $arr['rank']and $arr['branch'] don't exist. They are mere strings inside $k[0] and $k[1] and themselves don't contain any other data.
What would you have like to see outputted?
Maybe this is solution.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k['Rank'];
echo $k['Branch'];
}
}
Update :
Rank, Branch is values. Your array is bad format.
$arr = json_decode($row[0],TRUE);
foreach($row['chart_data'] as $chart_data)
{
foreach($chart_data[0] as $k)
{
echo $k[0];
echo $k[1];
}
}

Problem reading the returned JSON into PHP usable arrays in a foreach loop

I don't really understand at all what I am doing wrong. But for the life of me I cannot access the information in the JSON string within a foreach loop.
Here is my code. (the json string contains a list of events for any given user. The user ID is passed in the URL as user=xxxxxx).
The Live URL is here http://livemuzik.co.uk/fb3.php
Code:
include('fb/src/facebook.php');
if ($_REQUEST["user"]){
$user_id = $_REQUEST["user"];
} else $user_id = "me";
$app_id = "xxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://livemuzik.co.uk/fb2.php";
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=create_event,user_events,friends_events";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
// GET JSON DATA FOR EVENTS
$events_url = "https://graph.facebook.com/";
$events_url .= $user_id;
$events_url .= '/events?fields=id,owner&limit=100&';
$events_url .= $access_token;
$json = file_get_contents($events_url,0,null,null);
var_dump(json_decode($json, true));
// foreach(json_decode($json) as $obj) {
// Need to work out what code to put in here or what else I can do to output the json information as single variable values from an array
// }
The returned JSON appears like this:
array(2) {
["data"] => array(8) {
[0] => array(4) {
["id"] => string(15) "159231904144232"
["owner"] => array(2) {
["name"]=> string(11) "Dean Hurley"
["id"]=> string(10) "1038439076"
}
["start_time"]=> string(24) "2011-07-15T04:00:00+0000"
["rsvp_status"]=> string(9) "attending"
}
[1]=> array(4) { ["id"]=> string(15) "118572044893404" ["owner"]=> array(3) { ["name"]=> string(9) "RAVEOLOGY" ["category"]=> string(13) "Musician/band" ["id"]=> string(10) "8443998018" } ["start_time"]=> string(24) "2011-07-10T04:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [2]=> array(4) { ["id"]=> string(15) "123371994410260" ["owner"]=> array(3) { ["version"]=> int(1) ["name"]=> string(23) "The Bozeat City Rollers" ["id"]=> string(15) "182725898429985" } ["start_time"]=> string(24) "2011-07-03T04:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [3]=> array(4) { ["id"]=> string(12) "316743171061" ["owner"]=> array(2) { ["name"]=> string(17) "Richard Johnstone" ["id"]=> string(9) "668431151" } ["start_time"]=> string(24) "2011-07-01T23:00:00+0000" ["rsvp_status"]=> string(6) "unsure" } [4]=> array(4) { ["id"]=> string(15) "160599624007096" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-15T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [5]=> array(4) { ["id"]=> string(15) "231851770163680" ["owner"]=> array(2) { ["name"]=> string(11) "Dean Hurley" ["id"]=> string(10) "1038439076" } ["start_time"]=> string(24) "2011-06-13T08:30:00+0000" ["rsvp_status"]=> string(9) "attending" } [6]=> array(4) { ["id"]=> string(15) "203743706335174" ["owner"]=> array(2) { ["name"]=> string(15) "Bozeat Red Lion" ["id"]=> string(12) "155723533443" } ["start_time"]=> string(24) "2011-06-09T16:00:00+0000" ["rsvp_status"]=> string(9) "attending" } [7]=> array(4) { ["id"]=> string(15) "208151712549353" ["owner"]=> array(3) { ["name"]=> string(20) "Blackbush Promotions" ["category"]=> string(13) "Musician/band" ["id"]=> string(12) "336182297448" } ["start_time"]=> string(24) "2011-06-05T02:30:00+0000" ["rsvp_status"]=> string(6) "unsure" } } ["paging"]=> array(2) { ["previous"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&since=1310702400" ["next"]=> string(181) "https://graph.facebook.com/1038439076/events?fields=id%2Cowner&limit=100&access_token=331843765383|b9ef3b78db6a708b1a735347.1-1038439076|DkL44VVbAPlHl8mb03P1WA9VF_o&until=1307241000" } }
Any help would be greatly appreciated... If anyone is looking at a similar project relating to Facebook events and places feel free to contact me. I am happy to share my code!
My project involves synchronizing events and places to and from Facebook from Joomla Events management components.
Thanks in advance!!!
This is wrong:
foreach(json_decode($json) as $obj) {
}
it should be:
$json = json_decode($json);
foreach ($json->data as $data) {
echo $data->id . ' ' . htmlspecialchars($data->owner->name);
}
You can try something like
$json = json_decode($json);
foreach ($json as $item) {
var_dump($item);
}
instead of putting the function in the foreach directly.
I don't see what your problem is:
$events = json_decode($json, true)['data'];
foreach($events as $event) {
echo "Owner: {$event['owner']['name']}, Id: {$event['owner']['id']}";
echo "Start time: {$event['start_time']}";
}

Categories