0

I have a PHP class that is meant to get a mysqli query, make a multidimensional array, and currently just echo the whole array. It is this code:

include("connect.php");

class database
{
    public $motto;
    public $motto_array = array();
    public $rating;
    public $rating_array = array();
    public $category;
    public $score;
    private $mysqli;
    public $counter_array = array();
    public $multi_dim_values = array();
    public $multi_dim_category = array();


    function setMysqli($mysqli)
    {
        $this->mysqli = $mysqli;
    }

    function setCategory($category)
    {
        $this->category = $category;
    }

    function query_category()
    {
        if ($stmt = $this->mysqli->prepare("SELECT motto, score FROM mottos WHERE category=? ORDER BY score DESC"))
        {
            $stmt->bind_param("s", $this->category);
            $stmt->execute();
            $stmt->bind_result($motto, $ranking);
            while ( $stmt->fetch() ) {
                $this->motto_array[] = $motto;
                $this->rating_array[] = $ranking;
            }
            $stmt->close();
        }
    }

    function multi_dim_array()
    {
        $multi_dim_values = array($this->motto_array, $this->rating_array);
        $counter_array = range(0,count($this->motto_array)-1);
        foreach($counter_array as $index => $key) {
            $foreach_array = array();
            foreach($multi_dim_values as $value) {
                $foreach_array[] = $value[$index];
            }
            $multi_dim_category[$key]  = $foreach_array;
        }
        return $multi_dim_category;
    }


}

$class = new database;
$class->SetMysqli($mysqli);
$class->SetCategory("person");
$class->query_category();
print_r($class->multi_dim_array);
print_r($class->multi_dim_category);

connect.php has the database connection information for the mysqli.

I am learning OOP, so I did this in procedural, and it works fine, with this code:

include("connect.php");

    function category($mysqli, $cat)
    {
        if ($stmt = $mysqli->prepare("SELECT motto, score FROM mottos WHERE category=? ORDER BY score DESC"))
        {

        $stmt->bind_param("s", $cat);

        $stmt->execute();

        while($stmt->fetch())
        {
            printf ("[%s (%s) in %s] \n", $motto, $ranking, $category);
            $array .= compact("motto", "category", "ranking");
        }
        print_r($array);*/

        $a = array();
        $b = array();
        $c = array();
        $stmt->bind_result($motto, $ranking);
        while ( $stmt->fetch() ) {
            $a[] = $motto;
            $b[] = $ranking;
        }
        $result = array();
        $values = array($a, $b);
        $c = range(0,count($a)-1);

        foreach($c as $index => $key) {
            $t = array();
            foreach($values as $value) {
                $t[] = $value[$index];
            }
            $result[$key]  = $t;
        }

        return $result;

        $stmt->close();

        }
    }
    $cat = "person";
    $array_one = category($mysqli, $cat);
    print_r($array_one);

This prints the multidimensional array just like I want it to.
What am I doing wrong in the OOP code?

Thank you.

4
  • What is happening in your OOP code? Do you get an error message? Is the output wrong? If so, in what way? Commented Oct 30, 2013 at 1:52
  • Yes, we need to see what happens when you instantiate it and call each method. Make sure each method returns TRUE/FALSE so you can debug it locally on a method-basis. Commented Oct 30, 2013 at 1:58
  • Going waaayyy off topic but... is it just me or should the acronyms PHP and OOP never go next to each other like that? Particularly when teamed up with the phrase "not passing"! I swear I'm old enough not to giggle at this, but still. Commented Oct 30, 2013 at 2:53
  • How would I go about making each method return TRUE/FALSE? Commented Oct 30, 2013 at 10:54

1 Answer 1

3

Your code:

print_r($class->multi_dim_array);

You forgot the (), so you're not invoking the method. You're accessing a (non-existent) property. Try this:

print_r($class->multi_dim_array());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.