Jump to content

PHP MySQL Arrays and OOP


remarsh

Recommended Posts

I've searched high and low for pointed but im driving myself bonkers in getting this working!

 

Building a website and trying to keep everything efficient and modular however and have problems getting my head around OOP and Arrays with MySQL.

 

Class ABC {

 

  static $taken = array();

 

  function setArray() {

 

  //connect and query etc..

  // extract all the mysql and store it in an array of taken urls

 

  while($row = mysql_fetch_array($results)) {

$taken = $row['the_url'];

  }

  }

 

  function getArray() {

  //return the array so that a btree can search through them.

  return $taken;

  }

 

}

 

$hello = new ABC;

 

$hello->setArray();

 

$hello->getArray();

 

echo $hello;

 

 

however i only every seem to get the first result and doesnt seem to stay as an array

 

i tried seaching this website but keep getting 'daemon error' so any pointers to the right direction would be greatly appreciated!!! thankyou

 

Link to comment
https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/
Share on other sites

$hello->setArray(); and $hello->getArray(); are variables in and amongst themselves. 

You can almost think of classes as arrays, only using "->function" as a 'parking place' instead of [0], [1], etc.

Try echo $hello->getArray(); (Assuming that this will return data you want to output).

When you write:

$taken = $row['the_url'];

 

You're not adding that value to the array.  Instead, you're overwriting the entire thing.

 

The simplest way to add a value to an array is to simply put brackets at the end of the array variable's name:

$taken[] = $row['the_url'];

 

You can also explicitly push the value onto the array:

array_push($taken, $row['the_url']);

Also, you need to specify $this when referencing a class variable:

 

Class ABC {

   static $taken = array();

   function setArray() {

   // connect and query etc..
   // extract all the mysql and store it in an array of taken urls

       while($row = mysql_fetch_array($results)) {
           $this->taken[] = $row['the_url'];
       }
   }

   function getArray() {
   // return the array so that a btree can search through them.
       return $this->taken;
   }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.