Its been two days I'm working with these guys. Need help here.
here is my view
$username = json_decode($this->session->userdata('users'), TRUE);
foreach ($username AS $key => $value) {
<?php echo $value['username']; ?>
<?php echo $value['firstname']; ?>
<?php echo $value['lastname']; ?>
<?php $u = $value['username']; ?>
<?php echo "<a href=".base_url()."index.php/bets/un_sess/$u /> X </a> " ?>
<?php } ?>
here is my controller
$user1 = json_decode($this->session->userdata('users'), TRUE);
foreach ($user AS $key => $value) {
$index = array_search($username, $user);
unset($user[$index]);
$this->session->set_userdata('users', $user);
array from user_data
{"username":"user1","firstname":"name1","lastname":"name2"},
{"username":"user2","firstname":"test","lastname":"test"}
the output of this is when the first time i click the
username its working but when i click another username it gives error like this
A PHP Error was encountered
Severity: Warning
Message: Illegal offset type in isset or empty
Filename: libraries/Session.php
Line Number: 433 A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/bets.php
Line Number: 290
ok since there is no answering in my questions. Here is my solutions
$user = json_decode($this->session->userdata('users'), TRUE);
foreach ($user AS $key => $value) {
if ($username == $value['username']){
unset($user[$key]);
}
}
$this->session->set_userdata('users', json_encode($user));
Related
UPDATE:
$ci =&get_instance();
$ci->load->model(your model);
$ci->(your model)->(your function);
Note: You have to call your model in your controller.Its working fine
Using above answer from (access model from view in codeigniter?)
My above code should work. But why it does not.
I am loading my menu in navigation using database. Im connecting directly from my model. In my navigation I have
$CI =& get_instance();
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
In my masterdata/MasterDataRole_ I have
<?php
class MasterDataRole_ extends CI_Model{
//menu
//submenu
//screen
//check access rights
public function hasAccess($page_id,$level){
$query = $this->db->select('RoleAccess')
->from('masterdatarolemapping')
->where('ItemLevel',$level)
->where('ItemSysID',$page_id)
->where('MasterDataRoleID',$this->session->userdata('UserID'))
->get();
if($query){
if($query->num_rows() > 0){
$data = $query->row();
return $data->access;
}
}
}
}
But I get error saying
A PHP Error was encountered
Severity: Notice
Message: Undefined property: MasterDataRole::$menu
Filename: templates/navigation.php
Line Number: 117
In my Navigation I have:
<?php foreach ($menu as $m): echo $m;?>
<?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
<?php endif; ?>
<?php endforeach; ?>
Any Idea is appreciated.
UPDATE
error is on this <?php if($CI->menu->hasAccess($m->SysID,'menu') == 'yes'): ?>
this line <?php foreach ($menu as $m): echo $m;?> give the list of menu
$CI->load->model('masterdata/MasterDataRole_','masterdatarole');
$menu = $CI->masterdatarole->loadMenu();
Should be masterdatarole not menu
The code for controller while using facebook login. Here, on requesting location, i do get the value from facebook. But the problem is when i feed location info on view page where i only need name not id. It says error on array to string conversion.
{
"id": "1069347886434951",
"name": "Saugat Thapa",
"location": {
"id": "106085869430478",
"name": "Kathmandu, Nepal"
}
}
public function web_login()
{
$data['user'] = array();
// Check if user is logged in
if ($this->facebook->is_authenticated())
{
// User logged in, get user details
$location= $this->facebook->request('get', '/me?fields=location{id,name}');
if (!isset($user['error']))
{
$data['location'] = $location;
}
}
// display view
$this->load->view('examples/web', $data);
}
on view
<?php foreach ($location as $key => $value) : ?>
<li><?php echo $key; ?> : <?php echo $value; ?></li>
<?php endforeach; ?>
change this <?php echo $value; ?> to this <?php echo $value['name']; ?>
code goes in here
<?php break; ?> // break
break out of the loop because the next element in the array is a string and it will produce an error illegal string offset and as you want only the name it's fine
Simple question but I don't know why it is not printing correctly or working as expected. I have this in model (works because when I print_r($result) it shows data:
function get_latest_entries()
{
$query = $this->db->get('property');
return $query->result_array();
}
And for controller:
public function index()
{
$resultSet['properties'] = $this->propertymodel->get_latest_entries();
$this->load->view('home', $resultSet);
}
In the view I want to iterate over the array (the table has description, city, address columns):
<?php
foreach($resultSet as $item)
{
echo $item->city;
echo $item->description;
}
?>
I am getting two records on home page where am displaying the results as above:
Severity: Notice
Message: Undefined variable: resultSet
Filename: views/home.php
Line Number: 16
And
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 16
use $properties instead of $resultSet
use this... you are passing $properties to your view and not $resultSet..
<?php
foreach($properties as $item) // use properties
{
echo $item->city;
echo $item->description;
}
?>
Here is the problem in your code.
-you have returned data as array of arrays and you are trying to access as an object,
-and the second one is,you have passed properties variable to view and you are accessing resultSet.
-so here is your code in view which contains errors
<?php
foreach($resultSet as $item)
{
echo $item->city;
echo $item->description;
}
?>
-And the Correct version of your code is here...
<?php
foreach($properties as $item)
{
echo $item['city'];
echo $item['description'];
}
?>
Hi I am new to Codeigniter but have hit a brick wall.
I am trying to see if a user already exists.
First I upload the data via a form to a controller which does its validation etc but breaks on only that issue. I managed to find where it breaks but cant fix it from there.
Prior to all this it querys the database finds there is in fact a match username and then reaches the snippet below
$errors = $this->tank_auth->get_error_message();
//find the correct error msg
foreach ($errors as $k => $v) $data['errors'][$k] =$this->lang->line($v);
//loop and find etc
$temp_mess = $data['errors'][$k];
//stores relevant stuff in the string
}
}
//echo $temp_mess; it outputs to the html so i can see it "says user exists"
$data['temp_mess'] = $tempmess; /// put this into a array
$this->load->view('layout', $data); ///send
}
}
Now for the view, it then calls the layout view etc but alas there is no output
$username1 = array(
'name' => 'username1',
'id' => 'username1',
'value' => set_value('username1'),
'maxlength' => $this->config->item('username_max_length', 'tank_auth'),
'size' => 30,
);
<?php echo form_open('register', $form_reg_id ); ?>
<fieldset>
<legend align="center">Sign up</legend>
<?php echo form_label('Username', $username1['id']); ?>
<?php echo form_input($username1); ?>
<?php $tempmess; ?>
<div class="error"><?php echo form_error($username1['name']); ?>
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>
<?php echo form_close(); ?>
</fieldset>
Thanks for any help on this
also could some one explain this line please. (for a really dumb person)
<?php echo isset($errors[$username1['name']])?$errors[$username1['name']]:''; ?>
Tank_auth automatically returns an error when the username exists. Juste have to check the errors array.
I have this following code in my controller. I want to print the data i have my array.. should it be double forloop or foreach?
CONTROLLER:
public function index()
{
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
$cart['list'] = $in_cart;
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$this->load->view('layout/default', $data);
}
VIEW:
<?php if(is_array($list)): ?>
<?php foreach($list as $row):?>
<tr>
<td><?=$row->name?></td>
</tr>
<?php endforeach ?>
<?php endif;?>
but i have this following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: shop/cart.php
Line Number: 18
anyhelp guys? :(
I see you are using Code igniter (nice!)
I would do the following to ensure the $key is taking 'name'
public function index() {
foreach ($_SESSION['cartProducts'] as $key => $value) {
echo $key;
} }
Then
public function index() {
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$data['list'] = $in_cart;
$this->load->view('layout/default', $data); }
PHP is telling you exactly what is wrong.
You are trying to access the name property of the $row array when the -> syntax is specifically reserved for objects.
To access array values, you use square brackets and keys: <?=$row['name']?>
Helpful hint to understand - What you're doing is quasi-equivalent to: 7->name, insofar as the left value (7) has no idea how to use the arrow syntax. It's reserved for when the left value is an object. Not an integer, or in your case, an array.
Update after your comment:
You would get at the data like this:
<? foreach($row['list'] as $r):?>
<tr>
<td><?=$r[0]->name;?></td>
</tr>
<? endforeach;?>