tags:

views:

165

answers:

3

I am a .net programmer vb & c# but i can't seem to figure out how to get my objects into a list or array in php.

var mylist = new List<myobject>();

mylist.add(myobject1);
mylist.add(myobject2);

what i have tried. Products being a property for a collection of orderitems

$this->Products = getOrderItems();

public function getOrderItems()
{
    $items = array();
    $count = 0;

      // connect to db, query.....

    while($row = mysql_fetch_array($result, MYSQL_BOTH)){
     $count++;
     $items[$count] = ($row);
    }
    echo 'Count of Order Items...' . $count;

    return $items;
}

Am i even close?

+5  A: 
$items = array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $items[] = $row;
}
echo 'Count of Order Items...', count($items);
orlandu63
Will this increment for every row that is fetch so at the end all the rows are in $items?
OneSmartGuy
It also looks like i'm have issues using $this->Products = getOrderItems(); Is that legal in php?
OneSmartGuy
It depends on what $this is and whether it has the var $Products.
Nicholas
+2  A: 

What orlandu63 posted is correct - using $items[] = $row means that $row is appended numerically as the next element of $items.

Another option is that if there's an id field in $row, you can do $items[$row->id] = $row;, which has the advantage of indexing your array and making it easier to find a given item.

I really suggest reading through http://www.php.net/manual/en/language.types.array.php, which will explain to you some of the cool things PHP allows with arrays.

John Fiala
+3  A: 
  • $this->Products = getOrderItems(); is legal in PHP, but it refers to the (global) function getOrderItems() instead of the class method. class methods and variables always have to be prefixed with $this-> (or self::, if they're static vars) when called from inside the class.
    in your sample-code, you have that wrong. getOrderItems is defined as class method, but your call is not $this->-scoped, thus php assumes a function. it should throw an function not found-error.

  • the [] notation adds an element to the end of an array.

  • the index of the first element in your sample code is 1 (isn't that the standard case for VB?). php normally starts at 0 - though it's possible (because php-arrays are not real arrays) to start at arbitrary indices i'd recommend to stick with zero.

  • mysql_fetch_array is an ancient way of working with mysql. nowadays you're better of with mysqli or (even better) PDO.

  • (...) a list or array in php.

    lists, arrays, stacks, whatever: in php everthing is an ordered map (misleadingly called array):

    PHP: Arrays: An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

update:

sorry, i haven't got enough time right now to explain the finer nuances of pdo/mysqli over mysql.

so here are just the basics:

  • oop: pdo and mysqli are object oriented (tough mysqli got functional aliases)

  • prep statements: most important: pdo/mysqli got prepared statements. that means, you first prepare the query with placeholders once, then fill in the values later (without the need to prepare the query a second time). this approach has 3 obvious advantages:

    • performance: it's faster, because the database only has to analyze, compile and optimize the query once (at least with complex queries)

    • security: no need for quoted strings (happens automatically!), making sql-injection attacks harder

    • maintainability: the logic and data part of the query are separated, thus easier to read and you don't have to do a lot of string concenation

  • driver driven: pdo is not database specific. there are several supported db-systems, making it easier to port your code to other db-backends (but it's not an db-abstraction layer like ODBC, so the SQL still has to be compatible) and increasing reusability

of course, there's a lot more to it

Schnalle
+1 really good answer. covers the example in the question, covers php-internals.
knittl
Wow man what a great response! So can you elaborate more on mysqli and PDO?
OneSmartGuy