I cannot get my in_array to work on my library both functions are on same library. I will not let me do
$this->user_auth->hasPermission('modify', 'folder/controller-name');
By using the function above I can check if has permission to modify.
On my library function hasPermission the $key & $value does not work.
On my login function I unserialize the permissions
I cannot figure out why that the hasPermission always returns false.
Login
public function login($username, $password) {
$user_query = $this->CI->db->query("SELECT * FROM " . $this->CI->db->dbprefix . "user
WHERE username = " . $this->CI->db->escape($username) . "
AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(" . $this->CI->db->escape($password) . ")))))
OR password = " . $this->CI->db->escape(md5($password)) . ")
AND status = '1'
");
if ($user_query->num_rows() > 0) {
$this->user_id = $user_query->row('user_id');
$this->username = $user_query->row('username');
$this->user_group_id = $user_query->row('user_group_id');
$data_session = array(
'logged' => true,
'user_id' => $this->user_id
);
$this->CI->session->set_userdata($data_session);
$user_group_query = $this->CI->db->query("SELECT permission FROM " . $this->CI->db->dbprefix . "user_group
WHERE user_group_id = '" . (int)$user_query->row('user_group_id') . "'");
$permissions = unserialize($user_group_query->row('permission')); // Vardumps fine.
if (is_array($permissions)) {
foreach ($permissions as $key => $value) {
$this->permission[$key] = $value;
}
}
return true;
} else {
return false;
}
}
Has Permission
public function hasPermission($key, $value) {
if (isset($this->permission[$key])) {
return in_array($value, $this->permission[$key]);
} else {
return false;
}
}
Var Dump
array(2) {
["access"]=> array(18) {
[0]=> string(18) "catalog/Categories"
[1]=> string(27) "code_examples/Code_examples"
[2]=> string(23) "dashboard_modules/Chart"
[3]=> string(34) "dashboard_modules/Latest_customers"
[4]=> string(30) "dashboard_modules/Latest_users"
[5]=> string(14) "design/Banners"
[6]=> string(14) "design/Layouts"
[7]=> string(16) "extension/Module"
[8]=> string(15) "module/Category"
[9]=> string(16) "module/Slideshow"
[10]=> string(43) "module_code_examples/Codeigniter_controller"
[11]=> string(38) "module_code_examples/Codeigniter_email"
[12]=> string(39) "module_code_examples/Codeigniter_routes"
[13]=> string(16) "settings/Setting"
[14]=> string(14) "settings/Store"
[15]=> string(8) "tool/Log"
[16]=> string(10) "user/Users"
[17]=> string(17) "user/Users_groups"
}
["modify"]=> array(18) {
[0]=> string(18) "catalog/Categories"
[1]=> string(27) "code_examples/Code_examples"
[2]=> string(23) "dashboard_modules/Chart"
[3]=> string(34) "dashboard_modules/Latest_customers"
[4]=> string(30) "dashboard_modules/Latest_users"
[5]=> string(14) "design/Banners"
[6]=> string(14) "design/Layouts"
[7]=> string(16) "extension/Module"
[8]=> string(15) "module/Category"
[9]=> string(16) "module/Slideshow"
[10]=> string(43) "module_code_examples/Codeigniter_controller"
[11]=> string(38) "module_code_examples/Codeigniter_email"
[12]=> string(39) "module_code_examples/Codeigniter_routes"
[13]=> string(16) "settings/Setting"
[14]=> string(14) "settings/Store"
[15]=> string(8) "tool/Log"
[16]=> string(10) "user/Users"
[17]=> string(17) "user/Users_groups"
}
}
Update this is how I use the hasPermission
public function index() {
if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
redirect();
}
// load view area & content
}
public function validateForm() {
if (!$this->user_auth->hasPermission('modify', 'folder/controller-name')) {
// Displays error
}
// Other checks.
}
Thanks in advance
I've done some test on a simple php file :
<?php
$mydata = array(
"test" => array("one", "two", "three")
);
var_dump(hasPermission("test", "two"));
function hasPermission($key, $value)
{
if (isset($mydata[$key]))
{
echo "here";
return in_array($value, $mydata[$key]);
}
else
{
return false;
}
}
?>
Echo : boolean false
Conclusion : $mydata[$key] does not exists.
<?php
$mydata = array(
"test" => array("one", "two", "three")
);
var_dump(hasPermission("test", "two", $mydata));
function hasPermission($key, $value, $thearray) //Pass the array through parameter
{
if (isset($thearray[$key]))
{
echo "here";
return in_array($value, $thearray[$key]);
}
else
{
return false;
}
}
?>
Echo : "here" | boolean true
Conclusion : in_array() works correctly. Your script doesn't work because hasPermission can't access to your permission array.
You can write below code to validate the permission.
function hasPermission($key, $value)
{
foreach($this->permission[$key] as $k=>$v)
{
if($v == $value) return true;
}
return false
}
Related
I fetched all codes user got from the database and they are returned as arrays of arrays.
How could I merge them so that the codes were sequentially in a one-dimensional array?
Unfortunately, I can't change query of the procedure:
Code:
function loadGlobalPermissions(){
global $pdo;
$user_id = $_SESSION['id'];
$sql="CALL skaj_listp(:user_id)";
$statement = $pdo->prepare($sql);
$statement->bindParam(':user_id',$user_id,PDO::PARAM_STR);
if($statement->execute()){
$response[] = $statement->fetchALL(PDO::FETCH_ASSOC);
}
return $response;
}
Array:
array(1) { [0]=> array(1) { [0]=> array(28) { [0]=> array(1) { ["kod"]=> string(2) "DR" } [1]=> array(1) { ["kod"]=> string(3) "DRW" } [2]=> array(1) { ["kod"]=> string(2) "ER" } [3]=> array(1) { ["kod"]=> string(4) "ERDD" } [4]=> array(1) { ["kod"]=> string(5) "ERDSP" } [5]=> array(1) { ["kod"]=> string(4) "ERED" } [6]=> array(1) { ["kod"]=> string(5) "EREFP" } [7]=> array(1) { ["kod"]=> string(4) "EREM" } [8]=> array(1) { ["kod"]=> string(5) "EREMA" } [9]=> array(1) { ["kod"]=> string(4) "EREO" } [10]=> array(1) { ["kod"]=> string(4) "EREP" } [11]=> array(1) { ["kod"]=> string(4) "ERES" } [12]=> array(1) { ["kod"]=> string(4) "ERET" } [13]=> array(1) { ["kod"]=> string(4) "EREU" } [14]=> array(1) { ["kod"]=> string(5) "EREWM" } [15]=> array(1) { ["kod"]=> string(4) "ERUD" } [16]=> array(1) { ["kod"]=> string(5) "ERUSP" } [17]=> array(1) { ["kod"]=> string(2) "EU" } [18]=> array(1) { ["kod"]=> string(3) "EUS" } [19]=> array(1) { ["kod"]=> string(6) "EUSEUP" } [20]=> array(1) { ["kod"]=> string(3) "EWR" } [21]=> array(1) { ["kod"]=> string(3) "LRW" } [22]=> array(1) { ["kod"]=> string(5) "LUDDG" } [23]=> array(1) { ["kod"]=> string(5) "LUUZG" } [24]=> array(1) { ["kod"]=> string(3) "LUW" } [25]=> array(1) { ["kod"]=> string(2) "UR" } [26]=> array(1) { ["kod"]=> string(2) "UW" } [27]=> array(1) { ["kod"]=> string(3) "WPA" } } } } string(4)
Here's a function i wrote for you to merge a multidimensional array of any depth and return a one-dimensional array. Use it to merge your multidimensional array:
function mergeMultidimensionalArray($array, $merged = [], $depth = 0)
{
foreach($array as $key => $value) {
if(is_array($value)) {
$merged = mergeMultidimensionalArray($value, $merged, ++$depth);
} else {
if(isset($merged[$key])) {
$merged[$key . $depth] = $value;
} else {
$merged[$key] = $value;
}
}
}
return $merged;
}
$merged = mergeMultidimensionalArray($response);
Im trying many days now to select data from this db
[1]: https://i.stack.imgur.com/QA34L.jpg
I want to print for example all comments
echo $comment['username']." | "; \ Alex | Alex
My php code so far:
[![$m= new MongoDB\Client ("mongodb://127.0.0.1/");
$db = $m->stores;
$collection = $db->storeinfo;][1]][1]
$storez = $collection->find(array("Products.pTHUMBNAIL" => $pThumb));
$o=1;
$afm=array();
foreach ($storez as $stor) {
$afm[$o] = $stor['AFM'];
$record = $collection->findOne(array(
"AFM" => $afm[$o],"Products.pTHUMBNAIL" => $pThumb));
foreach ($record['Products'] as $pro){
if($pThumb == $pro['pTHUMBNAIL']){
echo $temp = $pro['pID']." ";
foreach($pro as $pro1['pCOMMENTS']) {
foreach($pro1 as $com['Comment']) {
var_dump($com['Comment']);
/*
foreach($com as $comment) {
echo $comment['username'];
}
*/
}
}
}
}
$o += 1;
}
It seems that i just cannot find the correct foreach to loop through my Comment array
var_dump output:
099360111/1 object(MongoDB\BSON\ObjectId)#55 (1) { ["oid"]=> string(24) "6003403a695900000c002649" } string(11) "099360111/1" string(9) "Old Skool" string(2) "75" string(4) "Vans" string(25) "Leather and textile upper" string(2) "44" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "7" object(MongoDB\Model\BSONArray)#65 (1) { ["storage":"ArrayObject":private]=> array(1) { [0]=> object(MongoDB\Model\BSONDocument)#10 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#73 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#45 (1) { ["milliseconds"]=> string(13) "1611028053000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } } } 099360666/1 object(MongoDB\BSON\ObjectId)#44 (1) { ["oid"]=> string(24) "6006563a3f1c0000c80034a8" } string(11) "099360666/1" string(12) "old school 2" string(2) "50" string(4) "Vans" string(11) "black/white" string(8) "42,43,43" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "6" object(MongoDB\Model\BSONArray)#79 (1) { ["storage":"ArrayObject":private]=> array(2) { [0]=> object(MongoDB\Model\BSONDocument)#7 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#39 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#68 (1) { ["milliseconds"]=> string(13) "1611028089000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } [1]=> object(MongoDB\Model\BSONDocument)#78 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#77 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#76 (1) { ["milliseconds"]=> string(13) "1611030745000" } ["text"]=> string(8) "good!!!!" ["rating"]=> string(1) "5" } } } } } }
What about this?
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach ($comments['Comment'] as $comment) {
echo $comment['text'];
}
}
You used foreach() bad: the index should be added to the first parameter, not in the 'as' part.
Also it helps if you use more clear variable names. That doesn't take too much, but makes it more readable and easy to debug.
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach($comments as $comment) {
echo $comment['text'];
}
}
controller
$data['userInfo'] = $this->user_model->get15();
User_model
function get15()
{
$this->db->select('userId');
$this->db->from('tbl_security');
$this->db->order_by('userId','ASC');
$this->db->where('status', 0);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
the array i get
array(1) { [0]=> array(1) { [0]=> array(1) { ["userInfo"]=> array(7) { [0]=> array(1) { ["userId"]=> string(2) "15" } [1]=> array(1) { ["userId"]=> string(2) "17" } [2]=> array(1) { ["userId"]=> string(2) "18" } [3]=> array(1) { ["userId"]=> string(2) "19" } [4]=> array(1) { ["userId"]=> string(3) "178" } [5]=> array(1) { ["userId"]=> string(4) "1444" } [6]=> array(1) { ["userId"]=> string(5) "12778" } } } } }
you can use array_column()
$user_ids = array_column($data[0][0]['userInfo'], 'userId');
print_r($user_ids);
output:
array(15,17,18,19,178,1444,12778);
Simply modify your model like below. This code will give you first array data . it same as code $statement->fetch(PDO::FETCH_OBJ). Write below code and modify it.
function get15()
{
$this->db->select('userId');
$this->db->from('tbl_security');
$this->db->order_by('userId','ASC');
$this->db->where('status', 0);
$query = $this->db->get();
$result = $query->row();
return $result;
}
array(2) { [0]=> array(2) { ["name"]=> string(16) "Daerah Pertanian" ["sub"]=> array(6) { [0]=> array(2) { ["name"]=> string(5) "Sawah" ["value"]=> string(3) "145" } [1]=> array(2) { ["name"]=> string(18) "Sawah Pasang Surut" ["value"]=> string(3) "455" } [2]=> array(2) { ["name"]=> string(6) "Ladang" ["value"]=> string(3) "678" } [3]=> array(2) { ["name"]=> string(10) "Perkebunan" ["value"]=> string(3) "688" } [4]=> array(2) { ["name"]=> string(19) "Perkebunan Campuran" ["value"]=> string(3) "966" } [5]=> array(2) { ["name"]=> string(16) "Tanaman Campuran" ["value"]=> string(3) "565" } } } [1]=> array(2) { ["name"]=> string(22) "Daerah Bukan Pertanian" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(18) "Hutan Lahan Kering" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(25) "Hutan Lahan Kering Primer" ["value"]=> string(3) "566" } [1]=> array(2) { ["name"]=> string(27) "Hutan Lahan Kering Sekunder" ["value"]=> string(3) "255" } } } [1]=> array(2) { ["name"]=> string(17) "Hutan Lahan Basah" ["sub"]=> array(2) { [0]=> array(1) { ["name"]=> string(24) "Hutan Lahan Basah Primer" } [1]=> array(1) { ["name"]=> string(26) "Hutan Lahan Basah Sekunder" } } } } } }
I have an array like I mention above, so I want to print out every "name" key including the index (number) of it's array parent,
for example when I print out "Tanaman Campuran" so all index parent is (0)(5) and when I print "Hutan Lahan Basah Sekunder" the index parent is (1)(1)(1)
how can I achieve it?
here is some recursive function that I've tried
$GLOBALS['all'] = '';
function printout($arr){
foreach ($arr as $ia=>$a){
if(is_array($a)){
foreach ($a as $ib=>$b){
if(is_array($b)){
printout($b);
}
else{
if ($ib == 'name') {
$GLOBALS['all'] .= $ia;
echo '<tr>';
echo '<td>' . $b . ' (' . $ia . ')</td>';
echo '</tr>';
$GLOBALS['all'] = '';
}
}
}
}
}
}
*sorry for my bad explanation, I hope you guys can understand it
You could use the following function:
function search(array $array, $name)
{
foreach ($array as $key => $entry) {
if ($entry['name'] === $name) {
return [$key];
}
if (isset($entry['sub']) && $found_keys = search($entry['sub'], $name)) {
return array_merge([$key], $found_keys);
}
}
return null;
}
It returns:
if the value was directly found, an array of one containing the associated index,
if it wasn't but was found in any descendant item, an array merging its index with the indices of said descendant,
null if it wasn't found in that part of the tree.
Note: if a given name is present several times, it will only find the first occurrence.
Demo: https://3v4l.org/1hGr1
I'm getting an error on a for each loop, saying that it get's invalid argument.
$bidder = new MuxBidder( $mailer->bidder_id );
$today = $bidder->getTodayBoughtLeads();
$legum = $bidder->getLegum();
$zipInterval = $bidder->getMeta('zip-intervals');
var_dump($bidder->getMeta('zip-intervals'));
$region = $lead->getRegion();
$zip = $lead->getZip();
// If "revisor"
if ( !$legum ) {
if( $lead->getPrice( $bidder->getId() ) >= $bidder->getMeta( 'min_clips' ) ) {
foreach ($zipInterval as $interval) {
if ($interval['from'] <= $zip && $zip <= $interval['to']) {
// "revisor" just get the lead straight away
$this->push_lead( $mailer->lead_id, $mailer->bidder_id );
return true;
}
}
}
// If Legum user ("advokat")
}
The error appears with $zipInterval. When I var_dump that variable I have:
array(1) {
[0]=> array(2) {
["from"]=> string(4) "0000" ["to"]=> string(4) "9999"
}
}
array(1) {
[0]=> array(2) {
["from"]=> string(4) "1000" ["to"]=> string(4) "9999"
}
}
array(1) {
[0]=> array(2) {
["from"]=> string(4) "1000" ["to"]=> string(4) "9999"
}
}
array(2) {
[0]=> array(2) {
["from"]=> string(4) "0000" ["to"]=> string(4) "9999"
}
[1]=> array(2) {
["from"]=> string(0) "" ["to"]=> string(0) ""
}
}
string(0) ""
Can anybody see what's wrong with the argument ?
Looks like you call this function more than once? On the last loop you get an empty string returned instead of an array.
try checking if the value is an array before iterating.
<?php
if(is_array($zipInterval)){
foreach($zipInterval as $interval){
//...
}
}