remarsh Posted July 19, 2009 Share Posted July 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/ Share on other sites More sharing options...
Gighalen Posted July 19, 2009 Share Posted July 19, 2009 $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). Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878121 Share on other sites More sharing options...
remarsh Posted July 19, 2009 Author Share Posted July 19, 2009 thanks for replying! I tried your tip and again it still only outputs the data that was last stored in the array. its a real head scratcher! tried FOR loops and it simply outputs the same data over and over again. ideas? Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878291 Share on other sites More sharing options...
Gighalen Posted July 19, 2009 Share Posted July 19, 2009 It seems to me as though you are replacing the array instead of adding to it. Can you please post the code that handles this? Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878301 Share on other sites More sharing options...
KevinM1 Posted July 20, 2009 Share Posted July 20, 2009 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']); Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878336 Share on other sites More sharing options...
remarsh Posted July 20, 2009 Author Share Posted July 20, 2009 what a schoolboy error! i cant believe it thank you so much for your advice. i would have stared at the screen for hours before i recognised that. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878876 Share on other sites More sharing options...
J.Daniels Posted July 20, 2009 Share Posted July 20, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-878886 Share on other sites More sharing options...
remarsh Posted July 20, 2009 Author Share Posted July 20, 2009 When i implement your suggestion i get; Fatal error: Cannot use [] for reading in / on line 25 Must be against the syntax or something. Don't quote me on it tho! hehe Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-879043 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 When i implement your suggestion i get; Fatal error: Cannot use [] for reading in / on line 25 Must be against the syntax or something. Don't quote me on it tho! hehe Show us your current code. Quote Link to comment https://forums.phpfreaks.com/topic/166516-php-mysql-arrays-and-oop/#findComment-879407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.