tags:

views:

116

answers:

2

Hello,

I'm new to OOP in PHP, is that to seems correct ?

class whatever {

    Function Maths() {
    $this->sql->query($requete);

   $i = 0;

  while($val = mysql_fetch_array($this)) { 
    $tab[i][average] = $val['average'];
    $tab[i][randomData] = $val['sum'];
    $i=$i+1;
    }
        return $tab;
}

I want to access the data contained in the array

$foo = new whatever();
$foo->Maths();
 for ($i, $i <= endOfTheArray; i++) {

    echo Maths->tab[i][average];
    echo Maths->tab[i][randomData];
 }

Thank you ;)

EDIT: i want to output the result of the SQL query as an array, so i can access it from outside the class

+4  A: 

Please check out PHP OOP basics:

http://www.php.net/manual/en/language.oop5.basic.php

Edit: Thanks for cleaning up the code. Try something along the lines of:

$tabs = array();
while($val = mysql_fetch_assoc($result)) { 
    $tabs[] = $val;
}

And:

$foo = new whatever();
$tabs = $foo->Maths();
for ($tabs as $tab) {
    echo $tab['average'];
    echo $tab['randomData'];
}

http://www.php.net/manual/en/language.oop5.basic.php

webbiedave
ok, i've cleaned up a lot of things. Is that better this way ?
Tristan
No - there are still several problems. For instance, $foo->Maths(); won't do anything and trying to echo Maths->tab... will throw errors. I agree that it is difficult to discern what you're trying to accomplish - can you tell us exactly what you want to do?
ABach
This "answer" should be a comment not an answer.
evolve
I want to output the result of the SQL query as an array, so i can access it from outside the class like <p> <?php print average ?></p>
Tristan
@evolve: Agreed. I edited to make it more an answer.
webbiedave
The PHP manual isn't that great in explaining OOP to beginners. Google "PHP classes and OOP". there are some much better sites explaining OOP to newcomers.
ggfan
thank you ;) i agree that PHP munual isn't kind of friendly to learn from for OOP ;)
Tristan
+4  A: 

In the interest of helping you out, here are some modifications. Please hear this, though: a lot of this might not make sense without a good background in PHP or OOP in general. You should look at @webbiedave's link.

class whatever {

  static function maths() {
    $tabs = array();
    $results = $this->sql->query($requete);

    while($val = mysql_fetch_array($this)) { 
      $tabs = $val;
    }

    return $tabs;
}
  1. This fixes syntax errors and logic errors (for instance, the creation of the $results variable to hold the SQL query run).
  2. I made the maths method static, since there's really no need to instantiate a whatever() object with this current example.

Here are some modifications to how this would be used:

$results = whatever::maths();
foreacho ($results as $result) {
  echo $result['average'];
  echo $result['randomData'];
}
  1. Since maths() returns something, you need to store that in a variable; simply calling it, as you did previously, doesn't do anything.
  2. That convoluted for loop can be replaced with a foreach loop.
ABach
Thank you, obviously i did a lot of confusing things before. I understand very well all your corrected code.does a static call for Maths() function triggers the __construct of the Whatever class which recieve the database link connection in static ? Thanks for your time ;)
Tristan
Aww, gipped! lol
webbiedave
@webbiedave, I shall discover a way to send you points and do that pronto. ;)
ABach
@Tristan - not sure I understand your question, but calling a static method inside a class does *not* call a __construct() function. If you want to do stuff in a constructor (like establish a database link), get rid of the `static` keyword in front of the method and instantiate the class in the manner you did previously.
ABach
perfect, you totally get the question since your answer answers it :D Thanks
Tristan
@Tristan You're welcome!
ABach
@ABach: I was just kiddin' around. Don't worry about it lol.
webbiedave