I have a simple array thats returned when executing:
$this->forecast($value->id,$value->db_connection)
when I call the $reports::create($data) it duplicates each row, i have added some logic to print out $data within the foreach and it never returns duplicates, when printing the $result object it has 2 entries.
any idea where i am going wrong?
public function store() // function to store report data in db
{
$reports = new Reports;
$companyData = new ClientSettings;
$settings = $companyData::all();
foreach ($settings as $value) {
$data = $this->forecast($value->id,$value->db_connection);
$result = $reports::create($data);
}
echo "Forecast generated";
}
public function store() // function to store report data in db
{
$reports = new Reports;
$companyData = new ClientSettings;
$settings = $companyData::all();
foreach ($settings as $value) {
$data = $this->forecast($value->id,$value->db_connection);
$result = Reports::create($data);
}
echo "Forecast generated";
}
Related
I wrote an API call in symfony to return all array data from database.
public function getData()
{
$data = [];
$users = $this->getUserRepository()->findAll();
foreach ($users as $user){
array_push($data, $user->getId());
array_push($data, $user->getEmail());
array_push($data, $user->getUsername());
}
return $data;
}
I got it like this
1,first#mail.com,userOne,2,second#gamail.com,userTwo
but I want to sort every group of data in new row like
1,first#mail.com,userOne,
2,second#gamail.com,userTwo
Another way of doing it would be:
public function getData()
{
$users = $this->getUserRepository()->findAll();
foreach ($users as $user) {
$data[] = [$user->getId(), $user->getEmail(),$user->getUsername()];
}
return $data;
}
Which removes the overhead of the function call array_push().
Source: http://php.net/manual/en/function.array-push.php
All of previous answers are not so good in my opinion. You don't need additional loop, just create query. (And return with your needed hydration)
For example
public function test()
{
return $this->createQueryBuilder('user')
->select('user.id, user.email, user.username')
->getQuery()
->getResult(); // will return objects
// ->getArrayResult() -> will return array of arrays
}
Place it in your user repository and then call it, not the findAll method.
this should work
public function getData()
{
$data = [];
$users = $this->getUserRepository()->findAll();
foreach ($users as $user){
$data[$user->getId()][] = $user->getId();
$data[$user->getId()][] = $user->getEmail();
$data[$user->getId()][] = $user->getUsername();
}
return $data;
}
You are creating 3 occurances in $data for each row returned from the database, instead load a single occurance for each returned row and put an array in it.
public function getData()
{
$data = [];
$users = $this->getUserRepository()->findAll();
foreach ($users as $user){
array_push($data, [$user->getId(), $user->getEmail(),$user->getUsername()]);
}
return $data;
}
Thank you all guys for help but I did it like this and it works for me.
public function getData()
{
$results = $this->getUserRepository()->findAll();
$rows = [];
$rows[] = array(
"id",
"username",
"email"
);
foreach ($results as $row) {
$rows[] = array(
$row->getId(),
$row->getUsername(),
$row->getEmail()
);
}
return $rows;
}
$users = $this->getUserRepository()->findAll();
$i=0;
foreach ($users as $user){
$data[$i][] = $user->getId();
$data[$i][] = $user->getEmail();
$data[$i][] = $user->getUsername();
$i++;
}
I'm trying to use OOP ways. I have bunch of methods that return same format of array. I want to guarantee that user of this class knows what will be returned. How would I go about doing that? Please forgive me as I'm not sure of correct terminologies to describe my problem.
class myModel {
public function getArray1(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
public function getArray2(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
public function getArray3(){
$data = array();
$id = array();
....
return array('data'=>$data, 'id'=>$id); <== HOW TO GUARANTEE RETURN FORMAT
};
}
You can try to define additional class for returning value:
class returnArray {
$data = array();
$id = array();
function construct($data,$id) {
$this->data = $data;
$this->id = $id;
}
}
//...
public function getArray1(){
$data = array();
$id = array();
....
return new returnArray('data'=>$data, 'id'=>$id);
};
Then You can add accessors to the returnArray class or leave it as public if You want or even implement __toString() if necessary.
My array list does not store or even retrieve items back, when the user press rent the it get the value of the item (the id item) and then it store it on the arryList, and from the basket file it should retrieve the selected item by the user from the database using the item id. the main problem i have is that adding and retrieving seems like not working
class Shop {
static $_item = array();
public function __construct(){
}
public function addItem($id)
{ self::$_item[] = $id;
}
public function getId()
{
foreach(self::$_item->s as $s)
{
return $s;
}
}
}
<?php
require_once('Models/Dvd_sql.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->dd = 'SQL';
$dvd_sql = new Dvd_sql();
$view->dd = $dvd_sql->fetchAllStudents(); //->fetchAllStudents();
if(isset($_POST['rent']))
{
$shop = new Shop();
$shop->addItem($_POST['trying']);
}
require_once('Views/dvdDetails.phtml');
<?php
require_once('Models/Basket.php');
require_once('Models/Shop.php');
$view = new stdClass();
$view->login = 'Homepage';
$view->dd = 'SQL';
$shop = new Shop();
$basket = new Basket();
$d = $shop->getId();
$view->dd = $basket->getFrom($d);
One problem I see is that in the following foreach, it will exit the function on the return and return only the first item. Plus since it is an array, it does not have the 's' property
Since you have multiple items in $shop, you can retrieve the complete array and then process them from outside the class
public function getId()
{
return self::$_item;
}
outside the class
$shop = new Shop();
$basket = new Basket();
$d = $shop->getId();
foreach ($d as $id){
$view->dd = $basket->getFrom($id);
// do whatever you wish to do with the retrieved basket item
// it's not clear what you wish to do with the item.
}
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I just want to back my class properties so that I can use them. In my project I want to fetch all result of mysql table data and return it. Well it works well, but alter returning data the error message has come which is given below:
Trying to get property of non-object in E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
My code ::
nonClass.php
<?php
$doc = new Dortor(); // object
$doc->result("SELECT * FROM doctor"); // function call
foreach( $doc as $doctor ) {
echo $doctor->doc_name; // error msg: Trying to get property of non-object in
//E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
}
?>
dortor.php
<?php
class Dortor extends DatabaseObject {
public $table_name = "doctor";
public $id;
public $doc_name;
public $doc_pass;
public $doc_img; // image directory
public function result($sql="") {
$result = $database->query($sql); // run query from mysql database
$result_array = array();
while( $row = $database->fetch_array($result) ) { // this while loop return actual result
$result_array[] = $this->get_table_value($row);
}
return $result_array;
}
}
?>
databaseObject.php
<?php
class DatabaseObject {
public function get_table_value($record) {
$className = get_called_class(); // get the class name
$object = new $className; // object call
$arrayValue = array();
$i=0; // inital array position
foreach( $object as $key => $value ) {
$arrayValue[$i] = $key; // get the class's object attributes
$i++;
}
$i = 1;
foreach ($record as $key => $value) { // fetch the database result
if(array_key_exists($key, $arrayValue) ) { // check if database's column name is exist on 'arrayValue' array,
$object->$arrayValue[$i] = $value; // if exist then, store value in 'Doctor' class's object attributes
$i++;
}
}
return $object;
}
}
?>
Your error is probably here:
$doc = new Dortor(); // object
$docs = $doc->result("SELECT * FROM doctor"); // you forgot to assign the result
foreach ($docs as $doctor) { // was looping over $doc here, instead of the result
echo $doctor->doc_name;
}
It would also make sense to check the correct variable type before the foreach loop starts:
if ( is_array( $doc) )
{
foreach( $doc as $doctor )
{
// foreach code
}
}
else
{
echo '$doc is no valid object';
var_dump($doc);
}
So you can find out, that's the problem.
I'm very new with PHP and OOP techniques.
So I have no problem creating a object like so:
$member1 = new Member('person 1');
But is there a way to return a bunch of objects. So that I can iterate through them and put them in the DOM?
class Example
{
public function getPeople()
{
$objs = array();
for($i = 0; $i < 10; $i++)
{
$obj[] = new Person('Person ' . ($i + 1));
}
return $objs; // returning an array of Person objects
}
}
$example = new Example();
foreach($example->getPeople() as $person)
{
// Do something with $person
}
In order to get objects, one possible way is to use array :-
$members = new members( array(...));
// you can assess any member via $members->members[$some_id]
// class to return list of members
class members ()
{
public $members = array();
public function __construct( array $ids)
{
foreach ($id as $id)
{
$this->members[$id] = new Member($id);
}
}
}
class member()
{
public function __construct($id)
{
// any other thing you want to do
}
}