Jump to content

[SOLVED] Using an Array returned from a function within a Class in another php page.


superfly

Recommended Posts

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!

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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)) {

}

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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!"

Link to comment
Share on other sites

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;
  }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.