Home > Software engineering >  How to iterate through retuned called function variable using PDO?
How to iterate through retuned called function variable using PDO?

Time:11-21

I am currently struggling to iterate through a result set that is stored in a function from a separate file, how would I go to iterate through this variable set?

Please see the example code below.

list.classes.php

class Lists extends Dbh {
    public function getCountries() {
      $stmt = $this->connect()->prepare("EXEC spl_countries");

      if(!$stmt->execute()) {
        $stmt = null;
        header("location: ../index.php?error=stmtfailed");
        exit();
      }

      if($stmt->rowCount() == 0) {
        $stmt = null;
        header("location: ../index.php?error=countrynotfound");
        exit();
      }
      return $stmt;
    }
}

spl_countries

IF EXISTS(SELECT * FROM dbo.sysobjects
          WHERE ID = OBJECT_ID(N'spl_countries')
          AND OBJECTPROPERTY(ID, N'IsProcedure') = 1)

BEGIN
    PRINT 'DROPPING THE STORED PROCEDURE spl_countries';
    DROP PROCEDURE spl_countries;
END
GO

PRINT 'CREATING THE STORED PROCEDURE spl_countries';
GO

CREATE PROCEDURE spl_countries
AS 
    SELECT countryID, country, iso2, iso3, phoneCode, niceName, numCode FROM CO_Country GROUP BY countryID, country, iso2, iso3, phoneCode, niceName, numCode ORDER BY country ASC

RETURN

test.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unicons.iconscout.com/release/v3.0.6/css/line.css">
    <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg p" crossorigin="anonymous"/>
</head>
<body style="background-color:#404258;">
    <?php
    //require('admin.sidebar.php');
    include "classes/dbh.classes.php";
    include "classes/list.classes.php";

    $listCountry = new Lists();
    $listCountry->getCountries();

    ?>
    <div class="col">
                    <div class="form-outline">
                      <select class="form-select" aria-label="Default select example" id="form-contactType">
                        <?php
                        while($row = $listCountry->fetch(PDO::FETCH_ASSOC)) {
                          echo "<option value=".$row['countryID'].">".$row['phoneCode']."</option>";
                        }
                        ?>
                        <!--<option selected>Open this select menu</option>
                        <option value="1">One</option>
                        <option value="2">Two</option>
                        <option value="3">Three</option>-->
                      </select>
                      <label for="form-contactType" class="form-label" >Contact Type</label>
                    </div>
                  </div>
</body>
</html>

I receive the following error when opening the PHP file

Fatal error: Uncaught Error: Call to undefined method Lists::fetch() in C:\xampp\htdocs\BusinessSolution\test:128 Stack trace: #0 {main} thrown in C:\xampp\htdocs\BusinessSolution\test.php

I am not certain what the correct steps would be to loop through the returned variable in getCountries()?

CodePudding user response:

You either need to store the returned value in a variable, or simply iterate over the results of the method call.

$listCountry = new Lists();
$countries = $listCountry->getCountries();
foreach($countries as $country) {

}

or

$listCountry = new Lists();
foreach($listCountry->getCountries() as $country) {

}
  • Related