Codeigniter 2.x, Database, Printing contents of results - php

I have a controller as follows:
$data['query'] = $this->model_admin->search_countries($config['per_page'] , $offset, $sort_by, $sort_order);
$data['templateVersion'] = 'template1';
$data['headerVersion'] = 'header1';
$data['navBarVersion'] = 'navbar1';
$data['main_content'] = 'detail-wrap/admin/country_index'; $this->load->view('detail-wrap/includes/template1', $data);
The search_countries method is in the controller and as follows:
return $this->db->select('country_id, country')->from('mhcountry')->get()->result();
My view is as follows:
<tbody>
<?php
var_dump($query);
foreach ($query['rows'] as $row ) {
?>
<tr>
<td><?php echo $row->country_id; ?></td>
<td><?php echo $row['country']; ?></td>
<td>something</td>
<td>someone</td>
</tr>
<?php } ?>
</tbody>
I cannot get country_id and country to display. I have tried many techniques but no go. The content of var_dump($query); is correct and shows:
Found Countries array(20) { [0]=> object(stdClass)#21 (2) { ["country_id"]=> string(1) "1" ["country"]=> string(6) "Canada" } [1]=> object(stdClass)#22 (2) { ["country_id"]=> string(1) "2" ["country"]=> string(5) "Japan" } [2]=> object(stdClass)#23 (2) { ["country_id"]=> string(1) "3" ["country"]=> string(5) "Korea" } [3]=> object(stdClass)#24 (2) { ["country_id"]=> string(1) "4" ["country"]=> string(1) "0" } [4]=> object(stdClass)#25 (2) { ["country_id"]=> string(1) "5" ["country"]=> string(6) "pakist" } [5]=> object(stdClass)#26 (2) { ["country_id"]=> string(1) "6" ["country"]=> string(6) "ddsdsd" } [6]=> object(stdClass)#27 (2) { ["country_id"]=> string(1) "7" ["country"]=> string(5) "texas" } [7]=> object(stdClass)#28 (2) { ["country_id"]=> string(1) "8" ["country"]=> string(8) "scotland" } [8]=> object(stdClass)#29 (2) { ["country_id"]=> string(1) "9" ["country"]=> string(8) "scotland" } [9]=> object(stdClass)#30 (2) { ["country_id"]=> string(2) "10" ["country"]=> string(8) "scotland" } [10]=> object(stdClass)#31 (2) { ["country_id"]=> string(2) "11" ["country"]=> string(8) "texassss" } [11]=> object(stdClass)#32 (2) { ["country_id"]=> string(2) "12" ["country"]=> string(14) "texassdsdsdsds" } [12]=> object(stdClass)#33 (2) { ["country_id"]=> string(2) "13" ["country"]=> string(11) "cuncuncunuc" } [13]=> object(stdClass)#34 (2) { ["country_id"]=> string(2) "14" ["country"]=> string(1) "d" } [14]=> object(stdClass)#35 (2) { ["country_id"]=> string(2) "15" ["country"]=> string(1) "a" } [15]=> object(stdClass)#36 (2) { ["country_id"]=> string(2) "16" ["country"]=> string(4) "sdsd" } [16]=> object(stdClass)#37 (2) { ["country_id"]=> string(2) "17" ["country"]=> string(9) "scotlands" } [17]=> object(stdClass)#38 (2) { ["country_id"]=> string(2) "18" ["country"]=> string(11) "scotlandsss" } [18]=> object(stdClass)#39 (2) { ["country_id"]=> string(2) "19" ["country"]=> string(6) "asdasd" } [19]=> object(stdClass)#40 (2) { ["country_id"]=> string(2) "20" ["country"]=> string(21) "scotlandsssssssssssss" } }
Any help much appreciated.

Related

How can I get subcategories under Main Category from Arrays by using PHP foreach loop?

I would like to get subcategory and main category from this arrays which I give below. First I want to get category and then would like to show subcategory under main category.
I have tried this PHP code but unfortunately my code doesn't work which I want. Can you suggest me another code please?
<?php
$resp = $c->execute($req);
var_dump($c->execute($req));
output:
object(SimpleXMLElement)#6 (2) {
["resp_result"]=>
object(SimpleXMLElement)#3 (3) {
["resp_code"]=>
string(3) "200"
["resp_msg"]=>
string(13) "Call succeeds"
["result"]=>
object(SimpleXMLElement)#4 (2) {
["categories"]=>
object(SimpleXMLElement)#7 (1) {
["category"]=>
array(499) {
[0]=>
object(SimpleXMLElement)#8 (2) {
["category_id"]=>
string(1) "2"
["category_name"]=>
string(4) "Food"
}
[1]=>
object(SimpleXMLElement)#9 (3) {
["category_id"]=>
string(9) "201379402"
["category_name"]=>
string(4) "Meat"
["parent_category_id"]=>
string(1) "2"
}
[2]=>
object(SimpleXMLElement)#10 (3) {
["category_id"]=>
string(9) "201379801"
["category_name"]=>
string(18) "Fruits and Berries"
["parent_category_id"]=>
string(1) "2"
}
[3]=>
object(SimpleXMLElement)#11 (3) {
["category_id"]=>
string(9) "201378002"
["category_name"]=>
string(18) "Bread and Pastries"
["parent_category_id"]=>
string(1) "2"
}
[4]=>
object(SimpleXMLElement)#12 (3) {
["category_id"]=>
string(9) "201248407"
["category_name"]=>
string(11) "Canned Food"
["parent_category_id"]=>
string(1) "2"
}
[5]=>
object(SimpleXMLElement)#13 (3) {
["category_id"]=>
string(9) "201378103"
["category_name"]=>
string(8) "Sausages"
["parent_category_id"]=>
string(1) "2"
}
[6]=>
object(SimpleXMLElement)#14 (3) {
["category_id"]=>
string(9) "200183144"
["category_name"]=>
string(12) "Canned Food1"
["parent_category_id"]=>
string(1) "2"
}
[7]=>
object(SimpleXMLElement)#15 (3) {
["category_id"]=>
string(9) "201345805"
["category_name"]=>
string(19) "Alcoholic Beverages"
["parent_category_id"]=>
string(1) "2"
}
[8]=>
object(SimpleXMLElement)#16 (3) {
["category_id"]=>
string(9) "201376903"
["category_name"]=>
string(17) "Fish and Sea Food"
["parent_category_id"]=>
string(1) "2"
}
[9]=>
object(SimpleXMLElement)#17 (3) {
["category_id"]=>
string(9) "200180142"
["category_name"]=>
string(7) "Grocery"
["parent_category_id"]=>
string(1) "2"
}
[10]=>
object(SimpleXMLElement)#18 (3) {
["category_id"]=>
string(9) "200001321"
["category_name"]=>
string(13) "Nut & Kernel1"
["parent_category_id"]=>
string(1) "2"
}
[11]=>
object(SimpleXMLElement)#19 (3) {
["category_id"]=>
string(9) "200001317"
["category_name"]=>
string(12) "Dried Fruit1"
["parent_category_id"]=>
string(1) "2"
}
[12]=>
object(SimpleXMLElement)#20 (3) {
["category_id"]=>
string(9) "200001287"
["category_name"]=>
string(15) "Grain Products1"
["parent_category_id"]=>
string(1) "2"
}
[13]=>
object(SimpleXMLElement)#21 (3) {
["category_id"]=>
string(9) "200001238"
["category_name"]=>
string(7) "Coffee1"
["parent_category_id"]=>
string(1) "2"
}
[14]=>
object(SimpleXMLElement)#22 (3) {
["category_id"]=>
string(9) "201376204"
["category_name"]=>
string(13) "Milk and Eggs"
["parent_category_id"]=>
string(1) "2"
}
[15]=>
object(SimpleXMLElement)#23 (3) {
["category_id"]=>
string(9) "201376702"
["category_name"]=>
string(10) "Ready Meal"
["parent_category_id"]=>
string(1) "2"
}
[16]=>
object(SimpleXMLElement)#24 (3) {
["category_id"]=>
string(9) "201249506"
["category_name"]=>
string(21) "Water/ Juices/ Drinks"
["parent_category_id"]=>
string(1) "2"
}
[17]=>
object(SimpleXMLElement)#25 (3) {
["category_id"]=>
string(9) "201248805"
["category_name"]=>
string(31) "Dried Goods / Local Specialties"
["parent_category_id"]=>
string(1) "2"
}
[18]=>
object(SimpleXMLElement)#26 (3) {
["category_id"]=>
string(9) "201248804"
["category_name"]=>
string(6) "Coffee"
["parent_category_id"]=>
string(1) "2"
}
[19]=>
object(SimpleXMLElement)#27 (3) {
["category_id"]=>
string(9) "200003573"
["category_name"]=>
string(32) "Dried Goods / Local Specialties1"
["parent_category_id"]=>
string(1) "2"
}
[20]=>
object(SimpleXMLElement)#28 (3) {
["category_id"]=>
string(9) "201379901"
["category_name"]=>
string(6) "Cheese"
["parent_category_id"]=>
string(1) "2"
}
[21]=>
object(SimpleXMLElement)#29 (3) {
["category_id"]=>
string(9) "201254404"
["category_name"]=>
string(11) "Dried Fruit"
["parent_category_id"]=>
string(1) "2"
}
[22]=>
object(SimpleXMLElement)#30 (3) {
["category_id"]=>
string(9) "201254306"
["category_name"]=>
string(14) "Grain Products"
["parent_category_id"]=>
string(1) "2"
}
[23]=>
object(SimpleXMLElement)#31 (3) {
["category_id"]=>
string(9) "201253905"
["category_name"]=>
string(12) "Nut & Kernel"
["parent_category_id"]=>
string(1) "2"
}
[24]=>
object(SimpleXMLElement)#32 (3) {
["category_id"]=>
string(9) "201374104"
["category_name"]=>
string(15) "Frozen Products"
["parent_category_id"]=>
string(1) "2"
}
[25]=>
object(SimpleXMLElement)#33 (3) {
["category_id"]=>
string(9) "100007269"
["category_name"]=>
string(3) "Tea"
["parent_category_id"]=>
string(1) "2"
}
[26]=>
object(SimpleXMLElement)#34 (3) {
["category_id"]=>
string(9) "201373004"
["category_name"]=>
string(21) "Vegetables and Greens"
["parent_category_id"]=>
string(1) "2"
}
[27]=>
object(SimpleXMLElement)#35 (2) {
["category_id"]=>
string(1) "3"
["category_name"]=>
string(21) "Apparel & Accessories"
}
[28]=>
object(SimpleXMLElement)#36 (3) {
["category_id"]=>
string(9) "200003274"
["category_name"]=>
string(11) "Sportswears"
["parent_category_id"]=>
string(1) "3"
}
[29]=>
object(SimpleXMLElement)#37 (2) {
["category_id"]=>
string(1) "6"
["category_name"]=>
string(15) "Home Appliances"
}
[30]=>
object(SimpleXMLElement)#38 (3) {
["category_id"]=>
string(3) "628"
["category_name"]=>
string(21) "Other Home Appliances"
["parent_category_id"]=>
string(1) "6"
}
}
}
["total_result_count"]=>
string(3) "499"
}
}
["request_id"]=>
string(13) "15r9xetbc5h1i"
}
I mean:
Main category -> Food
Subcategory-> Meat
-> Fruits and Berries
Main category -> Apparel & Accessories
Subcategory -> Sportswears
Here is my foreach code:
foreach($CategoryResp->resp_result->result->categories->category as $cat){
if(isset($cat->parent_category_id->string) == 'NUL'){
//mother category
echo "<b>".$cat->category_id. ' '.$cat->category_name."</b><br />";
if(isset($cat->parent_category_id)){
//subcategories
echo $cat->category_id. ' '.$cat->category_name."<br />";
}
}
}

Making a dynamic valuation with foreach php

I want to make a dynamic valuation. So the idea is like this :
In my database I've user table contains 'score'.
And I've category table contains fields 'minScore' and 'categoryName(bad, good, awesome)'
And In the end I want to get result like this categoryName=>howMuchUserBasedOnScore.
I'm thinking like this :
function valuation(){
$allScore = $this->db->select('score')->get('user')->result();
$category = $this->db->get('categori')->result();
$arrCategori = array();
foreach ($allScore as $value) {
foreach($categori as $row){
if($value->score <= $row->minScore){
array_push($arrCategori, )
//I don't know what should I do
}
}
}
}
can anyone help me?
edit :
Now I add maxScore into my category table field.
and I changed the if statement. it become like this
if($value->score <= $row->minScore && $value->score >= $row->maxScore){
array_push($arrCategori, $arrCategori[$row->Categori] = $value->bsc );
}
I var_dump it. and the result is what I expected. the problem now is that, how can I count how much people that are awesome, bad or good
edit :
here's the var_dump from $allscore (I change bsc into score for the sake simplicity):
array(11) { [0]=> object(stdClass)#42 (1) { ["bsc"]=> string(6) "100.00" } [1]=> object(stdClass)#43 (1) { ["bsc"]=> string(5) "40.00" } [2]=> object(stdClass)#44 (1) { ["bsc"]=> string(6) "100.00" } [3]=> object(stdClass)#45 (1) { ["bsc"]=> string(6) "100.00" } [4]=> object(stdClass)#46 (1) { ["bsc"]=> string(5) "40.00" } [5]=> object(stdClass)#47 (1) { ["bsc"]=> string(5) "40.00" } [6]=> object(stdClass)#48 (1) { ["bsc"]=> string(5) "40.00" } [7]=> object(stdClass)#49 (1) { ["bsc"]=> string(5) "40.00" } [8]=> object(stdClass)#50 (1) { ["bsc"]=> string(5) "40.00" } [9]=> object(stdClass)#51 (1) { ["bsc"]=> string(5) "60.00" } [10]=> object(stdClass)#52 (1) { ["bsc"]=> string(4) "0.00" } }
var_dump from $category (bxnak = maxScore, bxnaw = minScore)
array(9) { [0]=> object(stdClass)#53 (4) { ["id"]=> string(1) "1" ["bxnaw"]=> string(1) "0" ["bxnak"]=> string(2) "25" ["kategori"]=> string(5) "GAGAL" } [1]=> object(stdClass)#54 (4) { ["id"]=> string(1) "2" ["bxnaw"]=> string(2) "26" ["bxnak"]=> string(2) "40" ["kategori"]=> string(16) "DALAM PENGAWASAN" } [2]=> object(stdClass)#55 (4) { ["id"]=> string(1) "3" ["bxnaw"]=> string(2) "41" ["bxnak"]=> string(2) "50" ["kategori"]=> string(18) "PERLU PENGEMBANGAN" } [3]=> object(stdClass)#56 (4) { ["id"]=> string(1) "4" ["bxnaw"]=> string(2) "51" ["bxnak"]=> string(2) "60" ["kategori"]=> string(5) "BURUK" } [4]=> object(stdClass)#57 (4) { ["id"]=> string(1) "5" ["bxnaw"]=> string(2) "61" ["bxnak"]=> string(2) "75" ["kategori"]=> string(11) "KURANG BAIK" } [5]=> object(stdClass)#58 (4) { ["id"]=> string(1) "6" ["bxnaw"]=> string(2) "76" ["bxnak"]=> string(2) "80" ["kategori"]=> string(10) "CUKUP BAIK" } [6]=> object(stdClass)#59 (4) { ["id"]=> string(1) "7" ["bxnaw"]=> string(2) "81" ["bxnak"]=> string(2) "90" ["kategori"]=> string(4) "BAIK" } [7]=> object(stdClass)#60 (4) { ["id"]=> string(1) "8" ["bxnaw"]=> string(2) "91" ["bxnak"]=> string(2) "99" ["kategori"]=> string(11) "SANGAT BAIK" } [8]=> object(stdClass)#61 (4) { ["id"]=> string(1) "9" ["bxnaw"]=> string(3) "100" ["bxnak"]=> string(4) "1000" ["kategori"]=> string(8) "ISTIMEWA" } }
I finally did it!!
so here's the final code of my function
function valuation(){
$allBsc = $this->db->select('bsc')->get('kpi')->result();
$kategori = $this->db->get('kategori')->result();
$arrKategori = [];
foreach ($allBsc as $value) {
foreach($kategori as $row){
if($value->bsc <= $row->bxnak && $value->bsc >= $row->bxnaw){
if(array_key_exists($row->kategori, $arrKategori)){
array_push($arrKategori[$row->kategori], $value->bsc);
}else{
array_push($arrKategori, $arrKategori[$row->kategori] = array($value->bsc));
}
}
}
}
var_dump(count($arrKategori));
die();
}
thank you, for helping me!
$allBsc = $score
$kategori = $category
$arrKategori = $allCategory

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Codeigniter 2.x, ActiveRecord, var_dump(), what is the expected result?

I am trying to debug some problems I am having with viewing data from a database. I am using codeigniter 2.x and active records. Here is my code:
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
var_dump($data);
die;
Var_dump returns the following:
array(20) { [0]=> object(stdClass)#21 (1) { ["country_id"]=> string(1) "1" } [1]=> object(stdClass)#22 (1) { ["country_id"]=> string(1) "2" } [2]=> object(stdClass)#23 (1) { ["country_id"]=> string(1) "3" } [3]=> object(stdClass)#24 (1) { ["country_id"]=> string(1) "4" } [4]=> object(stdClass)#25 (1) { ["country_id"]=> string(1) "5" } [5]=> object(stdClass)#26 (1) { ["country_id"]=> string(1) "6" } [6]=> object(stdClass)#27 (1) { ["country_id"]=> string(1) "7" } [7]=> object(stdClass)#28 (1) { ["country_id"]=> string(1) "8" } [8]=> object(stdClass)#29 (1) { ["country_id"]=> string(1) "9" } [9]=> object(stdClass)#30 (1) { ["country_id"]=> string(2) "10" } [10]=> object(stdClass)#31 (1) { ["country_id"]=> string(2) "11" } [11]=> object(stdClass)#32 (1) { ["country_id"]=> string(2) "12" } [12]=> object(stdClass)#33 (1) { ["country_id"]=> string(2) "13" } [13]=> object(stdClass)#34 (1) { ["country_id"]=> string(2) "14" } [14]=> object(stdClass)#35 (1) { ["country_id"]=> string(2) "15" } [15]=> object(stdClass)#36 (1) { ["country_id"]=> string(2) "16" } [16]=> object(stdClass)#37 (1) { ["country_id"]=> string(2) "17" } [17]=> object(stdClass)#38 (1) { ["country_id"]=> string(2) "18" } [18]=> object(stdClass)#39 (1) { ["country_id"]=> string(2) "19" } [19]=> object(stdClass)#40 (1) { ["country_id"]=> string(2) "20" } }
I have 20 countries entered from 1 to 20 BUT I do not see my country names in the var_dump. It is just dumping the first field. Is this normal or expected?
I tried the same thing on another table and it also just returns the first field.
You should pay attention to the documentation here, meanwhile you can try this :
$this->db->select(array('country_id', 'country'))
// OR
$this->db->select('country_id, country')
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
This is wrong. it will get only country_id.
Use this
$data = $this->db->select('country_id , country')->from('mhcountry')->get()->result();

Use PHP if() to match item in array

I am working on an admin panel in which the code will display different if the PDO query returns all the admins from the admin table and the userid matches 1 of the admin id results. Only there can be an unlimited amount of admins and I don't want to be editing the code for each of them.
Would it be possible to do this (pseudo code):
if($userid isfoundin $result['admin_user_id']{
-- admin code here
}
I haven't yet written the admin table as I want more info on it first but if I cycle through the user table and look for id $query->fetchAll() then I get this:
array(30) { [0]=> array(2) { ["id"]=> string(1) "7" [0]=> string(1) "7" } [1]=> array(2) { ["id"]=> string(1) "6" [0]=> string(1) "6" } [2]=> array(2) { ["id"]=> string(1) "8" [0]=> string(1) "8" } [3]=> array(2) { ["id"]=> string(2) "31" [0]=> string(2) "31" } [4]=> array(2) { ["id"]=> string(2) "26" [0]=> string(2) "26" } [5]=> array(2) { ["id"]=> string(1) "4" [0]=> string(1) "4" } [6]=> array(2) { ["id"]=> string(2) "35" [0]=> string(2) "35" } [7]=> array(2) { ["id"]=> string(2) "21" [0]=> string(2) "21" } [8]=> array(2) { ["id"]=> string(2) "38" [0]=> string(2) "38" } [9]=> array(2) { ["id"]=> string(2) "24" [0]=> string(2) "24" } [10]=> array(2) { ["id"]=> string(2) "34" [0]=> string(2) "34" } [11]=> array(2) { ["id"]=> string(2) "20" [0]=> string(2) "20" } [12]=> array(2) { ["id"]=> string(2) "19" [0]=> string(2) "19" } [13]=> array(2) { ["id"]=> string(2) "23" [0]=> string(2) "23" } [14]=> array(2) { ["id"]=> string(2) "33" [0]=> string(2) "33" } [15]=> array(2) { ["id"]=> string(2) "28" [0]=> string(2) "28" } [16]=> array(2) { ["id"]=> string(1) "3" [0]=> string(1) "3" } [17]=> array(2) { ["id"]=> string(2) "15" [0]=> string(2) "15" } [18]=> array(2) { ["id"]=> string(1) "9" [0]=> string(1) "9" } [19]=> array(2) { ["id"]=> string(2) "25" [0]=> string(2) "25" } [20]=> array(2) { ["id"]=> string(1) "1" [0]=> string(1) "1" } [21]=> array(2) { ["id"]=> string(2) "32" [0]=> string(2) "32" } [22]=> array(2) { ["id"]=> string(1) "5" [0]=> string(1) "5" } [23]=> array(2) { ["id"]=> string(2) "18" [0]=> string(2) "18" } [24]=> array(2) { ["id"]=> string(2) "29" [0]=> string(2) "29" } [25]=> array(2) { ["id"]=> string(2) "27" [0]=> string(2) "27" } [26]=> array(2) { ["id"]=> string(2) "30" [0]=> string(2) "30" } [27]=> array(2) { ["id"]=> string(2) "22" [0]=> string(2) "22" } [28]=> array(2) { ["id"]=> string(2) "10" [0]=> string(2) "10" } [29]=> array(2) { ["id"]=> string(2) "36" [0]=> string(2) "36" } }
this si what I am trying to use right now
require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
$query = $pdo->prepare("SELECT id FROM users");
$query->execute();
var_dump($query->fetchAll());
if (in_array($user['id'], $query->fetchAll())){
echo $user['id'];
}
Yes, you can use in_array() for this.
I assume that you have a set of users for admin. Say, it is:
$adminUsers = array("admin", "administrator");
# Or by your code
$adminUsers = $result['admin_user_id'];
Now the code part is like:
if (in_array($userid, $adminUsers))
// Admin code here
I think you're looking for in_array() to see if a given item is in an array.
You should use in_array() if all you need to know is that the user ID exists in your array.
But if you actually need a reference to the item, take a look at array_search() http://au1.php.net/manual/en/function.array-search.php
Instead of going through all the users in your admins table and then having another loop in order to find if a specific user exists , why not doing this check in the query itself?
For instance:
$stmt = $mysqli->prepare("SELECT name,email FROM admins WHERE id=?");
$stmt->bind_param("d", $user_id);
$stmt->execute();
$stmt->bind_result($name, $email);
if ($stmt->fetch()) {
//The user with user_id exists in the admins table , show him the admin panel.
//You can use $name and $email.
}
$stmt->close();

Categories