I've written code that reads data from a cURL source which then processes it to extract records of data.
Each record becomes an instance of an object with several variables set from the data. A function extracts all the data and returns an array of objects.
The problem is that all of the objects have the same value which is the last record to be read from the source data.
After some testing I realize this is a reference problem. Data is read from the source and then assigned to an object which is then added to the array. The same object is reused in a loop that cycles through all the records in the source. Whenever this object is updated all previous values in the objects in the array are also reset to the newest value as they continue to reference the object when it is updated.
How can I make all the values independent?
function get_object_array () {
//reads raw data from cRUL source, returns array of objects
//array to hold objects
obj_arr = [];
//raw data has been split into array called $record, one element for each object
//loops through $record array
foreach ($record as $rec) {
//splits $rec into array of data called $data
//creates new object, but problem here as this object
//is being referenced by all values so last value
//changes all previous objects in array
$obj = new SaleItem();
//populates object with record data array
$obj->set_data($data);
//add object to array
$obj_arr [] = $obj;
}
return $obj_arr;
}
Update: Here is the function to set the data:
function set_data (array $arr) {
global $order_num, $name, $price, $cprice, $cheapest, $category;
try {
$order_num = (int)$arr[0];
$name = $arr[1];
$price = (float)$arr[2];
$cprice = (float)$arr[3];
$cheapest = $this->$price <= $this->$cprice ? true : false;
$category = $arr[5];
return true;
}
catch (Exception $ex) {
echo $ex;
return false;
}
}
Update: Full class code:
class SaleItem {
public $order_num = 12;
public $name = "";
public $price = 3.4;
public $cprice = 5.6;
public $cheapest = true;
public $category = "No Category";
function set_data (array $arr) {
try {
$this->order_num = (int)$arr[0];
$this->name = $arr[1];
$this->price = (float)$arr[2];
$this->cprice = (float)$arr[3];
$this->cheapest = $price <= $cprice ? true : false;
$this->category = $arr[5];
return true;
}
catch (Exception $ex) {
echo $ex;
return false;
}
}
function get_data () {
echo $this->order_num . ' num<br/>';
echo $this->name . ' name<br/>';
echo $this->price . ' price<br/>';
echo $this->cprice . ' cprice<br/>';
echo $this->cheapest . ' cheapest<br/>';
echo $this->category . ' category<br/>';
echo '<br/>';
}
}//end SaleItem class
You are using global variables instead of members. Remove
global $order_num, $name, $price, $cprice, $cheapest, $category;
From the function and preface each assignment with $this->
$this->order_num = (int)$arr[0];
$this->name = $arr[1];
$this->price = (float)$arr[2];
$this->cprice = (float)$arr[3];
$this->cheapest = $this->price <= $this->cprice;
$this->category = $arr[5];
Related
I am trying to test a few scripts that I would normally get from an SQL database but for testing offline I am just creating an array.
Here is what I have right now;
$result = array
(
array("name"=>"Toby", "q1"=>"1"),
array("name"=>"Phelps", "q1"=>"1"),
array("name"=>"Davies", "q1"=>"1"),
array("name"=>"Keith", "q1"=>"1"),
);
$resultnum = count($result);
echo "<b>Question 1</b> <br/><br/>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$name = $row['name'];
$answer = $row['q1'];
$q1answer = 1;
if($answer == $q1answer) {
echo $name . " got this right! <br/>";
} else {
echo $name . " got this wrong! <br/>";
}
}
}
How can I get this to work the same as though it was getting the array from an SQL query instead of just my array, for some reason I can't find a way to get this to run.
You can wrap the array in an anonymous class. Single uses like this are a main reason they exist.
$result = new class {
private $data = array
(
array("name"=>"Toby", "q1"=>"1"),
array("name"=>"Phelps", "q1"=>"1"),
array("name"=>"Davies", "q1"=>"1"),
array("name"=>"Keith", "q1"=>"1"),
);
private $data_index = 0;
public $num_rows;
public function __construct() {
$this->num_rows = count($this->data);
}
public function fetch_assoc() {
if (isset($this->data[$this->data_index])) {
$index = $this->data_index++;
return $this->data[$index];
}
}
};
Similar to the earlier answer, I propose a class. Here I would actually name the class, and pass the data to the constructor. The iteration over the array can be done with the current and next methods:
class ResultSet {
private $array = [];
public $num_rows = 0;
public function __construct($data) {
$this->array = $data;
$this->num_rows = count($this->array);
}
public function fetch_assoc() {
$val = current($this->array);
next($this->array);
return $val;
}
}
Until there it would be fixed. You would play with the data in the following:
$result = new ResultSet([
["name"=>"Toby", "q1"=>"1"],
["name"=>"Phelps", "q1"=>"1"],
["name"=>"Davies", "q1"=>"1"],
["name"=>"Keith", "q1"=>"1"],
]);
I did not implement support for count($result) as I don't think that is supported on real mysqli result sets either. You get the count via ->num_rows (like you also do).
So basically I have an array, and I have a function that will return an array value, but it is not returning anything.
$skills = array("Attack");
$i = 0;
$hs = file_get_contents("localhost/player=".$_GET["player"]);
foreach($skills as $value) {
$hs[$i] = explode(",",$hs[$i]);
$stats[$value]["rank"] = $hs[$i][0];
$stats[$value]["level"] = $hs[$i][1];
$stats[$value]["xp"] = $hs[$i][2];
$i++;
}
function getSkill($skill) {
//echo $skill;
return $stats[$skill]["level"];
}
I have tried getSkill("Attack"); and it doesn't work;
but if I do echo $stats["Attack"]["level"]; it works fine.
You don't pass the $stats array to your function so it can't know what is in it.
function getSkill($stats, $skill) {
//echo $skill;
return $stats[$skill]["level"];
}
and call it with getSkill($stats,"Attack");
I need to be able to echo a value from a private property in one of my classes if a method is called within the class. It's a little tricky to explain so let me demostrate and hopefully someone can fill in the blank for me :)
<?php
class test {
private $array['teachers']['classes'][23] = "John";
public function __construct($required_array) {
$this->array['teachers']['classes'][23] = "John";
$this->array['students'][444] = "Mary";
$this->echo_array($required_array);
}
public function echo_array($array) {
// Echo the value from the private $this->array;
// remembering that the array I pass can have either
// 1 - 1000 possible array values which needs to be
// appended to the search.
}
}
// Getting the teacher:
$test = new test(array('teachers','classes',23));
// Getting the student:
$test = new test(array('students',444));
?>
Is this possible?
$tmp = $this->array;
foreach ($array as $key) {
$tmp = $tmp[$key];
}
// $tmp === 'John'
return $tmp; // never echo values but only return them
An other approach to get value;
class Foo {
private $error = false,
$stack = array(
'teachers' => array(
'classes' => array(
23 => 'John',
24 => 'Jack',
)
)
);
public function getValue() {
$query = func_get_args();
$stack = $this->stack;
$result = null;
foreach ($query as $i) {
if (!isset($stack[$i])) {
$result = null;
break;
}
$stack = $stack[$i];
$result = $stack;
}
if (null !== $result) {
return $result;
}
// Optional
// trigger_error("$teacher -> $class -> $number not found `test` class", E_USER_NOTICE);
// or
$this->error = true;
}
public function isError() {
return $this->error;
}
}
$foo = new Foo();
$val = $foo->getValue('teachers', 'classes', 24); // Jack
// $val = $foo->getValue('teachers', 'classes'); // array: John, Jack
// $val = $foo->getValue('teachers', 'classes', 25); // error
if (!$foo->isError()) {
print_r($val);
} else {
print 'Value not found!';
}
Having some trouble with the following code. I've created a class to manage the DB connection, using what you see below as queryPreparedQuery and works fine when getting data for a single user, or any data that returns a single result using something like this...
include 'stuff/class_stuff.php';
function SweetStuff() {
$foo = new db_connection();
$foo->queryPreparedQuery("SELECT Bacon, Eggs, Coffee FROM Necessary_Items WHERE Available = ?",$bool);
$bar = $foo->Load();
$stuff = 'Brand of Pork is '.$bar['Bacon'].' combined with '.$bar['Eggs'].' eggs and '.$bar['Coffee'].' nectar for energy and heart failure.';
return $stuff;
}
echo SweetStuff();
Problem is, I want to build the functionality in here to allow for a MySQL query which returns multiple results. What am I missing? I know it's staring me right in the face...
class db_connection
{
private $conn;
private $stmt;
private $result;
#Build a mysql connection
public function __construct($host="HOST", $user="USER", $pass="PASS", $db="DB_NAME")
{
$this->conn = new mysqli($host, $user, $pass, $db);
if(mysqli_connect_errno())
{
echo("Database connect Error : "
. mysqli_connect_error());
}
}
#return the connected connection
public function getConnect()
{
return $this->conn;
}
#execute a prepared query without selecting
public function execPreparedQuery($query, $params_r)
{
$stmt = $this->conn->stmt_init();
if (!$stmt->prepare($query))
{
echo("Error in $statement when preparing: "
. mysqli_error($this->conn));
return 0;
}
$types = '';
$values = '';
$index = 0;
if(!is_array($params_r))
$params_r = array($params_r);
$bindParam = '$stmt->bind_param("';
foreach($params_r as $param)
{
if (is_numeric($param)) {
$types.="i";
}
elseif (is_float($param)) {
$types.="d";
}else{
$types.="s";
}
$values .= '$params_r[' . $index . '],';
$index++;
}
$values = rtrim($values, ',');
$bindParam .= $types . '", ' . $values . ');';
if (strlen($types) > 0)
{
//for debug
//if(strpos($query, "INSERT") > 0)
//var_dump($params_r);
eval($bindParam);
}
$stmt->execute();
return $stmt;
}
#execute a prepared query
public function queryPreparedQuery($query, $params_r)
{
$this->stmt = $this->execPreparedQuery($query, $params_r);
$this->stmt->store_result();
$meta = $this->stmt->result_metadata();
$bindResult = '$this->stmt->bind_result(';
while ($columnName = $meta->fetch_field()) {
$bindResult .= '$this->result["'.$columnName->name.'"],';
}
$bindResult = rtrim($bindResult, ',') . ');';
eval($bindResult);
}
#Load result
public function Load(&$result = null)
{
if (func_num_args() == 0)
{
$this->stmt->fetch();
return $this->result;
}
else
{
$res = $this->stmt->fetch();
$result = $this->result;
return $res;
}
}
#Load result
public function Execute(&$result = null)
{
if (func_num_args() == 0)
{
$this->stmt->fetch_array();
return $this->result;
}
else
{
$res = $this->stmt->fetch_array();
$result = $this->result;
return $res;
}
}
private function bindParameters(&$obj, &$bind_params_r)
{
call_user_func_array(array($obj, "bind_param"), $bind_params_r);
}
}
UPDATE
Got this to work with Patrick's help. Was able to find the following code with the help of this question, and with a few tweaks, it works beautifully. Added the following after the execute() statement in ExecPreparedQuery, returning an array at the very end instead of the single result:
# these lines of code below return multi-dimentional/ nested array, similar to mysqli::fetch_all()
$stmt->store_result();
$variables = array();
$data = array();
$meta = $stmt->result_metadata();
while($field = $meta->fetch_field())
$variables[] = &$data[$field->name]; // pass by reference
call_user_func_array(array($stmt, 'bind_result'), $variables);
$i=0;
while($stmt->fetch())
{
$array[$i] = array();
foreach($data as $k=>$v)
$array[$i][$k] = $v;
$i++;
}
# close statement
$stmt->close();
return $array;
As a result of the altered code, I changed the call to interpret multidimensional array data rather than a single result, of course. Thanks again!
In your Execute function you are calling $this->stmt>fetch_array().
That function only returns an array of a single row of the result set.
You probably want:
$this->stmt->fetch_all()
Update
To retrieve the entire result set from a prepared statement:
$this->stmt->store_result()
I need some guide or reference on how should I do this.
What I should do is, have a class structure named Cat and have a static method that outputs new object.
class Cat{
public $name;
public $age;
public $string;
static public function ToData($Cat) {
$input = "";
foreach ($Cat as $key => $value) {
$input .= "object: " . $key . "</br>";
}
return $input;
}
}
$name = "meow";
$age = "12";
$string = "'test', 'sample', 'help'";
$Cat = array($name, $age);
$output = Cat::ToData($Cat);
echo $output;
This is the best thing that I can come up with
here is the problem, they said I just used an array and not an object.
I used array because I have to put the values on the $Cat so it can be passed on the parameter.
Looks like it's an assignment on object-oriented programming concept in PHP. I believe this is what you're trying to accomplish, with comments explaining the steps.
class Cat{
public $name;
public $age;
// Output the attributes of Cat in a string
public function ToData() {
$input = "";
$input .= "object: name :".": ".$this->name." </br>";
$input .= "object: age :".": ".$this->age." </br>";
return $input;
}
}
$name = "meow";
$age = "12";
// Instantiate Cat
$Cat = new Cat();
$Cat->name = $name;
$Cat->age = $age;
// Output Cat's attributes
$output = $Cat->ToData();
echo $output;
if you want to set those values to the object here is what you do
...
foreach ($Cat as $key => $value) {
$this->$key = $value;
}
...
$name = "meow";
$age = "12";
$Cat = array("name"=>$name,"age"=> $age);
$cat = new Cat();
$cat->toData($Cat);
echo $cat->name;
// meow
Update:
Now i get a better idea what you are trying to do, this is how your class will look like:
class Cat{
public $name;
public $age;
public $string;
static public function ToData($Cat) {
$obj = new self();
$obj->name = $Cat["name"];
$obj->age = $Cate["age"];
$obj->string = $Cate["string"];
return $obj;
}
// echo
public function __toString(){
return "$this->name - $this->age - $this->string";
}
}
now you can set your values
$name = "meow";
$age = "12";
$string = "'test', 'sample', 'help'";
$Cat = array($name, $age,$string);
$output = Cat::ToData($Cat);
echo $output;
Note that $output is an object