Adding an array of objects to another array in codeigntier PHP? - php

im using codeigntier framework im trying to solve a problem to do with retrieving information from a database for example:
model.php:
public function read(){
$query = $this->db->get('table');
return $query->result();
}
controller.php:
public function doSomething () {
$exampleArray['name'] = "Bob's Database";
$getModel = $this->load->model('model','getData');
$modelData = $this->getData->read();
// i want to assign the the $modelData to a array element like so
$exampleArray['modelData'] = $modelData // here's where im stuck :(
}
thanks for your help!!
p.s. this is not an error, its just a question :)
}

If you want to be able to access $exampleArray outside of that method, you'll have to do one of two things:
a) set it as a class variable
class MyClass {
public $exampleArray;
.
.
.
}
then refer to it using
$this->exampleArray['index']
or b) pass it by reference into your doSomething() function:
public function doSomething(&$exampleArray) { ... }
The php manual has a section on variable scope that should help you better understand this.

Related

Access variable across methods Laravel PHP

Hey all actually I too facing the problem but I couldn't understand any of the above methods. Please help me to understand those stuffs and help t fix my problem.
I have two methods method1 and method2, where I receive some value in method 1 which needs to used in method 2. I created a variable on class level but I couldn't access the variable below is the code snippet.
class testController extends controller
{
public $isChecked = false;
public $isSelectedValue = 0;
public function ValidateValue(Request $req)
{
$isChecked = $req->checked;
$isSelectedValue = $req->value;
}
public function UsethoseValues()
{
if ($isChecked) { // I can't use the variable here it throws run time error. I need help on this please help.
}
}
}
because you are in class and you declare a property not a simple variable
so when you try to access it from the method in your class you need to add $this
keyword that refer to your class
$this->isChecked
so your code will be like this after editing
class testController extends controller {
public $isChecked = false;
public $isSelectedValue = 0;
public function ValidateValue(Request $req) {
$this->isChecked = $req->checked;
$this->isSelectedValue = $req->value;
}
public function UsethoseValues() {
if($this->isChecked) { // I can't use the variable here it throws run time error. I need help on this please help.
}
}
}
feel free to check the docs for more info

How would I rewrite this getter as a setter?

I have only started PHP 3 weeks ago so be gentle with me :)
I understand that the method should accept an array index and some value as parameters to use them to update the array but I have no idea of the syntax and have tried so many times now.
I'm not sure if I can still display the array inside the method?
Or should it be outside the method but inside the class?
Any help would be awesome guys.
Best,
Derek.
<?php
class Names{
public $Names = array("Derek", "Paddy", "Des", "Billy" , "Jack");
public function getElement($IndexParameter){
return $this->Names[$IndexParameter];
}
}
$obj = new Names;
echo $obj->getElement(4);
?>
Here you go:
public function setElement($IndexParameter, $NewName) {
$this->Names[$IndexParameter] = $NewName;
}
Usage:
$obj->setElement(4, "Bilbo");
Note: I've kept your style, although I would generally recommend using InitialCaps for class names.
Mark $Names property as protected or private to prevent direct access to it.
class Names{
protected $Names = array("Derek", "Paddy", "Des", "Billy" , "Jack");
public function getNames($IndexParameter){
return $this->Names[$IndexParameter];
}
public function setNames($IndexParameter, $NewName) {
$this->Names[$IndexParameter] = $NewName;
}
}

Why can't i use an attribute containing mysqli object?

i kinda faced a problem recently that i wasn't able to figure out its solution .
I have a core class that contains the follow code in its constructor:
public function __construct()
{
$this->runDatabaseConnection();
$this->fetchSettings();
}
private function runDatabaseConnection()
{
global $config;
$this->db = new mysqli($config['db']['server'], $config['db']['user'], $config['db']['password'], $config['db']['name']);
$this->db->set_charset($config['db']['charset']);
}
As you can see i stored the object in an attribute called "db"
After that , I made a method in the same class that runs a given query:
public function query($queryStr)
{
return $this->db->query($queryStr) or die($this->db->error.($this->debugMode ? '<br><b>Query: </b><i>'.$queryStr.'</i>' : ''));
}
Now outside class, When i use something like:
$studentsQuery = $core->query("SELECT * FROM ".TP."students");
$studentsQuery variable seems to be a boolean value and not the ressource that i was expecting , So what am i missing ? Thanks in advance .
You are returning a result of boolean expression.
return $this->db->query($queryStr) or die());
So what I assume you wanted to do was
public function query($queryString)
{
$result = $this->db->query($queryString);
if (!$result) {
die(...);
}
return $result;
}

Question about form_dropdown with Codeigniter

I have a question about using form_dropdown().
The code below works, but i am unsure about if I have to do the new array in the view or is there a better way of doing it with the array $games - passed from $data['games']?
Should I be doing all the processing in the controller and sending over a ready array to populate the dropdown?
I tried this in the view: echo form_dropdown('games', $games); but got the error "Object of class stdClass could not be converted to string", I think it's because its an array of objects and I have to convert it?
TABLE: GAMES
GM_ID - int
GM_NAME - var
MODEL:
class Test_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_game_names()
{
$queryg = $this->db->query("SELECT * FROM games");
return $queryg->result();
}
}
CONTROLLER
class Test extends CI_Controller {
public function index()
{
$this->load->model('test_model');
$data['games'] = $this->test_model->get_game_names();
$this->load->view('view_test',$data);
}
}
VIEW
$this->load->helper('form');
echo form_open('send');
$list = array(); //is this the best way to do it??
foreach($games as $row)
{
$list[$row->GM_ID] = $row->GM_NAME; //is this the best way to do it??
}
echo form_dropdown('games', $list); //then pass this array?
echo form_close();
You are correct that it needs to be converted from an object to an array, the keys are your input values, and the array values are the text displayed in the <option> when you use form_dropdown(). The way you are doing it is fine, and I personally recommend it.
Reason: The form controls and HTML/text output are view logic. For instance, what if you want to do this instead?:
$list[''] = 'Please select a game';
foreach($games as $row)
{
$list[$row->GM_ID] = ucfirst(htmlspecialchars($row->GM_NAME));
}
That's just my perspective, it really doesn't matter too much, but usually as a rule - HTML and presentation logic should be in the view.
Aside: your function name get_game_names() is confusing because it returns more than just names. Also for simplicity's sake, you're using ActiveRecord so you can just do this:
function get_games()
{
return $this->db->get('games')->result();
}

passing variables between functions in same controller using codeigniter

How to pass variable between two functions in same controller?
Do I need to use $this->session->set_flashdata is this right way to go ?
function load() {
$this->userhash = $this->uri->segment(3);
}
function save() {
$query_customer = $this->Customers->get_customer($this->userhash);
}
sorry if this is a basic stuff, but I am still learning.
I need to edit my question
In Ivan link, that I also googled out
there is a code which goes something like this
function index() {
$this->msg = 'data';
$this->testme();
}
function testme() {
echo $this->msg;
}
and its works, but I can not call $this->testme() directly in index(), so I can not load save() in load()
Passing data between two functions in my controller class

Categories