superfly Posted January 22, 2009 Share Posted January 22, 2009 Ok, so i did some searching before posting this question and the closest i got was this http://www.phpfreaks.com/forums/index.php/topic,111688.0.html which didnt exactly solve my problem. Here's my issue. Say that on a page there is a statement that tells a function to fetch some data, the call looks like this: <?php $status = 'live'; $row = $challenges->getChallenges($status); ?> The function getChallenges is in a class included in another page, i've copied it below: <?php function getChallenges($status){ global $database; //The Database Connection $q = "SELECT * FROM ".TBL_CHALLENGES." WHERE status = '$status'"; $result = mysql_query($q, $database->connection); $dbarray = mysql_fetch_assoc($result); return $dbarray; ?> So far so good, i get the $dbarray stored in the $row variable as an array. The problem is that i'm usually used to the following coding: <?php while ($row = mysql_fetch_array($results)) { echo 'Name: ' .$row['name']; echo 'Date: ' .$row['date']; etc... } ?> which will continue with the loop while there are results in the results set. Whereas now since i've "outsorced" the sql to a function within a class, i'm not sure how to construct the while loop? I tried (foolishly) <?php while ($row){ etc... } ?> i'm sure you can guess the outcome there! So any idea how to get those results back so i can populate the table with dynamic data? Many Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/ Share on other sites More sharing options...
trq Posted January 22, 2009 Share Posted January 22, 2009 Two ways. Either have the getChallenges method return all result in an array or store the $result within a class property and create another method to iterate over and return one result at a time. I can tell by the use of the global keyword that its likely this class needs some work. Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743063 Share on other sites More sharing options...
superfly Posted January 22, 2009 Author Share Posted January 22, 2009 Two ways. Either have the getChallenges method return all result in an array or store the $result within a class property and create another method to iterate over and return one result at a time. Isnt that what the $dbarray variable is doing? all the results are in that variable, and if i had another method to return one result at a time, i would have to call it each time i needed some data? no? I am still learning php, and the class itself was downloaded based on it's description... Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743077 Share on other sites More sharing options...
RussellReal Posted January 22, 2009 Share Posted January 22, 2009 your while loop should be something like while ($row = mysql_fetch_assoc($result)) { } HOWEVER you set $result inside a function's scope, not the script's scope, therefore once the function exits the variable is garbage collected. what you could do is ABOVE function getChallenges in your class somewhere after class *** { // like right here } you'd put public $result; (assumingh you're on 5.0+); then when you do: $result = mysql_query($q, $database->connection); you would do $this->result = mysql_query($q, $database->connection); instead so then later in the script for the while loop you'd do while ($row = mysql_fetch_array($challenges->result)) { } Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743079 Share on other sites More sharing options...
superfly Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks man, that works, however, thorpe has got me thinking that the way i'm doing it is not entirely correct and will come back to haunt me later. What is the significance of the "global" keyword? i did some searching, and it has something to do with register globals? or? Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743082 Share on other sites More sharing options...
RussellReal Posted January 22, 2009 Share Posted January 22, 2009 as in global $varName; ? well thats really outdated and hardly practiced anymore, you should instead use references since global you know you want that variable.. so you'd do like function whatever(&$var) { $var = "hello!"; } $abc = ''; whatever($abc); echo $abc; // "hello!" instead of function whatever() { global $abc; $abc = "hello!"; } $abc = ''; whatever(); echo $abc; // "hello!" Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743086 Share on other sites More sharing options...
superfly Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks Russell, i'll work on this some more, Just to add, i just realised that the class was written in 2003!!! so that's why it's probably not the best one to use - or i'll have to modify it! Topic solved for now. Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743087 Share on other sites More sharing options...
RussellReal Posted January 22, 2009 Share Posted January 22, 2009 anytime bro Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743090 Share on other sites More sharing options...
trq Posted January 22, 2009 Share Posted January 22, 2009 Just to give you a better understanding of how it could be done. class challengers { private $db; private $result; public function __construct() { $this->db = mysql_connect(); mysql_select_db('foo'); } public function getChallenges($status) { $q = "SELECT * FROM ".TBL_CHALLENGES." WHERE status = '$status'"; if ($result = mysql_query($q, $this->db)) { if (mysql_num_rows($result)) { $this->result = $result; return true; } } return false; } public function getResult() { if ($this->result) { return mysql_fetch_object($this->result); } } } // and now to call it. $challengers = new challengers; if ($challengers->getChallengers('live')) { while ($row = $challengers->getResult()) { echo 'Name: ' . $row->name; echo 'Date: ' . $row->date; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743108 Share on other sites More sharing options...
superfly Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks Thorpe, appreciate the follow-up. It's much clearer now with the example!! Quote Link to comment https://forums.phpfreaks.com/topic/141914-solved-using-an-array-returned-from-a-function-within-a-class-in-another-php-page/#findComment-743136 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.