简体   繁体   中英

foreach loop in a php function

I'm trying to make a foreach loop, that runs through the Db table, and echoes it out in a function, but when i try to do this, it takes the rows i have in the object, and echoes the object out as many times as i have rows, and doesn't proceed to the next object in the table. help is appreciated. Here is my code:

function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";

$result = $mysqli->query($sql);

$row = $result->fetch_object();

    foreach ($row as $value) {   
    $foundhotel = "<h1>" . $row->hotel_name . "</h1></br>";
    $foundhotel.= $row->hotel_adress . "</br>";
    $foundhotel.= $row->hotel_postal_code . "</br>";
    $foundhotel.= $row->description;
    echo "$foundhotel";
    }  
  }

tried doing this aswell, but this only displays the Last hotel in the table.

function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";

$result = $mysqli->query($sql);

$row = $result->fetch_object();
while($row = $result->fetch_object()){           
    $foundhotel = "<h1>" . $row->hotel_name . "</h1></br>";
    $foundhotel.= $row->hotel_adress . "</br>";
    $foundhotel.= $row->hotel_postal_code . "</br>";
    $foundhotel.= $row->description;
    echo "$foundhotel";
    }  

}

->fetch_object retrieves only one row

Try this:

function hotels_from_db() {
include 'DbConnection.php';
$sql = "select * from hotels";
$result = $mysqli->query($sql);

while($row = $result->fetch_object()) {   
    $foundhotel = "<h1>" . $row->hotel_name . "</h1></br>";
    $foundhotel.= $row->hotel_adress . "</br>";
    $foundhotel.= $row->hotel_postal_code . "</br>";
    $foundhotel.= $row->description;
    echo "$foundhotel";
}  

}

It should be:

function hotels_from_db() {
    include 'DbConnection.php';
    $sql = "select * from hotels";

    $result = $mysqli->query($sql);

    $row = $result->fetch_all();

    foreach ($row as $value) {   
        $foundhotel = "<h1>" . $value->hotel_name . "</h1></br>";
        $foundhotel.= $value->hotel_adress . "</br>";
        $foundhotel.= $value->hotel_postal_code . "</br>";
        $foundhotel.= $value->description;
        echo "$foundhotel";
    }  
}
$row = $result->fetch_all(MYSQLI_ASSOC); // MAKE ARRAY ASSOCIATIVE ARRAY

foreach ($row as $value) {   
    $foundhotel = "<h1>" . $value->hotel_name . "</h1></br>";
    $foundhotel.= $value->hotel_adress . "</br>";
    $foundhotel.= $value->hotel_postal_code . "</br>";
    $foundhotel.= $value->description;
    echo "$foundhotel";
}  

}

fetch_object needs to be called for each row fetch. Try the following:

function hotels_from_db() {
    include 'DbConnection.php';
    $sql = "select * from hotels";

    if ($result = $mysqli->query($sql)) {

        /* fetch object array */
        while ($obj = $result->fetch_object()) {
            $foundhotel = "<h1>" . $obj->hotel_name . "</h1></br>";
            $foundhotel.= $obj->hotel_adress . "</br>";
            $foundhotel.= $obj->hotel_postal_code . "</br>";
            $foundhotel.= $obj->description;
            echo "$foundhotel";        
        }

        /* free result set */
        $result->close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM