Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Return list of object from a Class

Return list of object from a Class

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
javascriptdatabasecsharpphphtml
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    awedaonline
    wrote on last edited by
    #1

    Hi all, I am new to PHP development after a reasonable experience with the .NET. I have just created a class to return list of categories from the category table and display on my home page. Below is my code snippets:

    link = mysqli_connect($this->host, $this->username, $this->password);
    if (!$this->link) {
    // todo: display error
    }
    if (!mysqli_set_charset($this->link, 'utf8')) {
    // todo: display error
    }
    if (!mysqli_select_db($this->link, $this->dbName)) {
    // todo: display error
    }
    }

    public function getAllCategories() {
        $sql = mysqli\_query($this->link, "CALL getCategories()");
        while ($row = mysqli\_fetch\_array($sql)){
            $row\['description'\] . '  
    

    ';
    }
    return $row;
    }
    public function executeQuery ($query) {
    if (!$this->query($query)) {
    // todo: display error
    }
    }
    }

    ?>

    on my home.php page, I have:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <script type="text/javascript" src="script/ref/jquery-1.6.2.min.js" /></script>
    <title></title>
    </head>
    <body>
    
    G 1 Reply Last reply
    0
    • A awedaonline

      Hi all, I am new to PHP development after a reasonable experience with the .NET. I have just created a class to return list of categories from the category table and display on my home page. Below is my code snippets:

      link = mysqli_connect($this->host, $this->username, $this->password);
      if (!$this->link) {
      // todo: display error
      }
      if (!mysqli_set_charset($this->link, 'utf8')) {
      // todo: display error
      }
      if (!mysqli_select_db($this->link, $this->dbName)) {
      // todo: display error
      }
      }

      public function getAllCategories() {
          $sql = mysqli\_query($this->link, "CALL getCategories()");
          while ($row = mysqli\_fetch\_array($sql)){
              $row\['description'\] . '  
      

      ';
      }
      return $row;
      }
      public function executeQuery ($query) {
      if (!$this->query($query)) {
      // todo: display error
      }
      }
      }

      ?>

      on my home.php page, I have:

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

          <script type="text/javascript" src="script/ref/jquery-1.6.2.min.js" /></script>
      <title></title>
      </head>
      <body>
      
      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      Your getAllCategories function will return NULL, because it is returning the last value returned by mysqli_fetch_array($sql). You can return an array of results this way:

      $results = array();
      while($row = mysqli_fetch_array($sql)){
      $results[] = $row['description'];
      }
      return $results;

      or a string:

      $results = '';
      while($row = mysqli_fetch_array($sql)){
      $results .= $row['description'] . '<br />';
      }
      return $results;

      You also have $cat[] = $con->getAllCategories(); - this will add whatever the function returns as a new entry in the $cat array, which probably is not what you want.

      A 1 Reply Last reply
      0
      • G Graham Breach

        Your getAllCategories function will return NULL, because it is returning the last value returned by mysqli_fetch_array($sql). You can return an array of results this way:

        $results = array();
        while($row = mysqli_fetch_array($sql)){
        $results[] = $row['description'];
        }
        return $results;

        or a string:

        $results = '';
        while($row = mysqli_fetch_array($sql)){
        $results .= $row['description'] . '<br />';
        }
        return $results;

        You also have $cat[] = $con->getAllCategories(); - this will add whatever the function returns as a new entry in the $cat array, which probably is not what you want.

        A Offline
        A Offline
        awedaonline
        wrote on last edited by
        #3

        Thank Graham; Your response was correct. So on my home.php page, this is what I finaly did:

        $con = new Connection();
        $cat = $con->getAllCategories();
        foreach ($cat as $c) {
        echo $c['description'] . '
        ';
        }

        But the result of the above was the first character of each record per row. How do I fix this?

        G 1 Reply Last reply
        0
        • A awedaonline

          Thank Graham; Your response was correct. So on my home.php page, this is what I finaly did:

          $con = new Connection();
          $cat = $con->getAllCategories();
          foreach ($cat as $c) {
          echo $c['description'] . '
          ';
          }

          But the result of the above was the first character of each record per row. How do I fix this?

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          This is a bit confusing, but I've worked it out. You've already pulled the "description" field out of the row in the getAllCategories function - so now echo $c['description'] ... is accessing the string $c, and not an associative array. The square brackets are referring to the characters of the string, so 'description' is being evaluated as 0 and you are getting the first character in the string. Try using this instead:

          echo $c . '<br />';

          A 1 Reply Last reply
          0
          • G Graham Breach

            This is a bit confusing, but I've worked it out. You've already pulled the "description" field out of the row in the getAllCategories function - so now echo $c['description'] ... is accessing the string $c, and not an associative array. The square brackets are referring to the characters of the string, so 'description' is being evaluated as 0 and you are getting the first character in the string. Try using this instead:

            echo $c . '<br />';

            A Offline
            A Offline
            awedaonline
            wrote on last edited by
            #5

            Thanks. I am very glad for your response. Now, I am getting it right. Please don't be annoyed. Thanks once again.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups