Codeigniter Selecting from an array - php

This will be simple for someone, and I have looked it up but don't understand why my syntax returns blank.
$this ->adminship_model->get_admininfo();
Returns an Array;
'username' => $this->adminship_model->get_admininfo(),
put in the session stores an array and when I print_r it, I can see the array of data.
[UserName] => SomeUserName
Is in the array
$variable = $this ->adminship_model->get_admininfo();
Sets the array to that variable
'username' => $this->$varaible->UserName,
returns blank
'username' => $this->$variable['UserName'],
returns blank
'username' => $variable['UserName'],
returns blank
What am I doing wrong please. Sorry if it is simple, I'll only need told once. :)

I'm a little confused by your syntax, but it looks like you should have
$variable = $this->adminship_model->get_admininfo(); // space removed
$user_name = $variable['UserName'];
echo $user_name;
That should echo SomeUserName

Related

Codeigniter increase value in database from array with only one component escaped [duplicate]

This question already has an answer here:
CI update_batch; Concatenate and increment variable and string as array value
(1 answer)
Closed 19 days ago.
I need to increase the value of a field in my CodeIgniter database.
I understand that CI allows not to escape values, setting as FALSE the third parameter in $this->db->set().
I am inserting multiple data from an array, and as I understand, it would be not much secure to escape them all (data is coming from form), since I only need increasing in one field.
How can I avoid inserting this value separately?
My current code:
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
'reservas' => "reservas+1"
);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
EDIT: I HAVE TESTED WITH SIMPLE AND DOUBLE QUOTES, IN BOTH CASES THE VALUE OF THE FIELD IS SAVED AS 0 (ZERO)
The previous code does not produce the expected result. But this works:
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
//'reservas' => "reservas+1"
);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
$this->db->set('reservas', 'reservas+1', FALSE);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
My intention is to use only one statement to insert data.
Is it possible not to escape a single element of the array, using the first code?
Thank you very much!
The simple answer is No. It's a very simple array.
You can see that the SET statement has 3 parameters. How are you planning on adding that into your data array.
The other reasons probably are...
It's already covered by using set with false.
It's a royal pain in the butt to add more fields in the array and have to parse them to determine which one requires the false flag.
Using Set as you have is perfectly acceptable and when you think about it in your case...
Each time you perform an update to an entry you will always increment the "reservas" value. This isn't and definitely should not be a part of your "Data" you wish to update from your Form. It is a function of the update itself.
So that kind of leads into... lets make a function. This isn't really part of the answer to your question but I have added it as simply something to ponder over.
You can put this where ever you like but to keep it simple I'll assume you have a client controller and I'll put it all in there for this example...
So set up your $telefono and $dataCliente as only you know where that comes from... Then create a simple single function.
private function update_client_details($data, $telefono) {
$this->db->set('reservas', 'reservas+1', FALSE);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $data);
}
And call the above method to perform the update. It's become One Statement.
It's a good idea to make this private if its in the controller. In a Model it would need to be public.
use a single quote not double quote
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
'reservas' => 'reservas+1'
);
see this link for further info

Getting array from generated php document

I am currently generating config.php file that has an array w user details in it ,but I have a problem with getting the array back out to be used on the page, the config.php looks like this
Array
(
[DBLocation] => localhost
[DBName] => name
[DBUsername] => name
[DBPassword] => 123456
)
How can I use this array later?
Convert your config.php file like this:-
<?php
$arr = Array
(
'DBLocation' => 'localhost',
'DBName' => 'TAK14_Ostermann',
'DBUsername' => 'TAK14_Ostermann',
'DBPassword' => '123456'
);
?>
Now include this file into others with code:-include 'config.php' and use $arr variable.
Note:
Better to do a complete configuration code in the file (config.php) itself and create a database connection object. Now use that object by including the file. It will remove db-connection code redundancy in each page.
If the array you posted is the exact content of the config.php file you are not generating it in the right way.
The posted "array" is what print_r() outputs. As the documentation says:
print_r — Prints human-readable information about a variable.
The key here is "human-readable". The purpose of print_r() is to produce an output that is easy to read and understand by the programmer. It is a debug function, not meant to be used in the production code.
The function you need to generate the content of config.php is var_export(). It produces correct PHP code and it is specifically crafted for this purpose.
Assuming your configuration data is stored in the $config array, the code that generates config.php should be like this:
file_put_contents('config.php', '<?php return '.var_export($config, TRUE).";\n");
The generated config.php file will look like this:
<?php return array (
'DBLocation' => 'localhost',
'DBName' => 'name',
'DBUsername' => 'name',
'DBPassword' => '123456',
);
In order to load the configuration use include:
$config = include 'config.php';

How to return a specific output to a variable using raw query in cakephp

I have a variable which needed a value from a specific query
$player_id_owner = $this->Player->fetchAll('Select id from player
where name = ?', array($name));
$player_id_owner = ($this->Player->find('all', array(
'fields' => array('id'),
'conditions' => array('Player.name' => '$name')
)));
i tried both raw query and cakephp find but both of them returns only "array"
have i forgotten something? how can i access the expected result from query? thanks
Well
'Player.name' => '$name'
is not valid PHP code, at least not for what you try to do.
Don't escape variables as strings:
'Player.name' => $name
You could have easily seen that by checking the resulting query in the debug kit or the bottom of the view.
And most likely you would want to use find('first', ...) as documented if you only expect a single entry here.
Last but not least:
You most likely just lack basic debugging skills. Don't echo it, as it is indeed an array. Never do that with unknown variables as they often are everything but a basic string.
Use debug() to see whats inside and then properly echo what you see, e.g. echo $player['Player']['name'];.
Bear in mind that stringish output should be secured by h() on output:
echo h($player['Player']['name']);
try this
$player_id_owner = $this->Player->find('first', array(
'fields' => array('id'),
'conditions' => array('Player.name' => $name)
));
or try (you can also use your variable instead of yourname)
'conditions' => array('Player.name LIKE' => "%yourname%")
after that you can get the id with
$player_id_owner['Player']['id']

PHP Mongodb - POST array insert

I am trying to convert post data into a format that would allow me to pass it right into my collection. For example: When I print_r on the $_POST I get this form data:
Array
(
[Name] => Steve
[Email] => Steve#mail.com
[submit] => Submit
)
I am wondering how i can convert this to an acceptable object to insert into mongodb collection using php similar to:
$Record = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
$Collection->insert($Record);
I am thinking a loop of the above array with some additional formatting but I can't seem to figure it out. I have also tried json_encode but keep getting the same error "Call to a member function insert() on a non-object in..." saying that its not a proper object. Thank you for any help.
No need to encode anything, it's just PHP native and expects an array. Let the driver do the work for you:
$Collection->insert( $_POST );
As it is the two should be equivalant:
$rec = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
print_r ($rec);

How to get array information

I got configuration file database.php
<?php defined('_ENGINE') or die('Access Denied'); return array (
'adapter' => 'mysqli',
'params' =>
array (
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'db',
'charset' => 'UTF8',
'adapterNamespace' => 'Zend_Db_Adapter',
),
'isDefaultTableAdapter' => true,
'tablePrefix' => 'engine4_',
'tableAdapterClass' => 'Engine_Db_Table',
); ?>
How to get only password from this array?
something like echo $array['password'];
How do I get the array from database.php?
You'll need to include the file and bind the returned value to a variable, such as in the below example.
$db_conf = require ('/path/to/database.php');
$db_conf will contain the data returned by database.php.
Documentation
PHP: include - Manual
How do I read the specific value from my array?
Since you are working with a nested array the solution is not as far away as you might think. First use $a[key] to get to the array stored under params, and then get the value of password from there.
As in the below example.
$password = $array['params']['password'];
Note: The above is, in a logical sense, equivalent to;
$params = $array['params'];
$password = $params['password'];
Documentation
PHP: Arrays - Manual
I tried the above but it's just shouting "Access Denied" in my face, why?
To protect database.php from unintended access it has been protected with a check to see so that it's being used inside of the engine.
The script will die if _ENGINE is not defined.
If you want to use database.php in a script outside of the thought of engine you'll need to define the _ENGINE constant before includeing the file.
define ('_ENGINE', 1);
...
$db_conf = include ('database.php');
echo $array['params']['password'];
Give this a try. :-)

Categories