适用于Android应用的PHP脚本中的问题

时间:2018-08-07 07:20:41

标签: php

我有一个 showProduct.php 文件,我想从该文件中的另一个文件中调用函数<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout ======== put all the rest of your settings from scrollvide here =====> <CONTENT> add here the rest of your code </CONTENT> </RelativeLayout> </Scrollview> 。在showProduct()中,我想从数据库中提取所有行并提取到 showProduct.php 文件中。问题是,当我返回数组时,仅显示最后一行。我想显示所有行。 showProduct.php 是:

showProduct()

和showProduct()函数是:

<?php
require_once '../includes/DbOperations.php';
$response = array();
$result = array();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$db = new DbOperations();
$result = $db->showProduct();
if(!empty($result))
{
    $response["prod_name"] = $result["prod_name"];
    $response["prod_desc"] = $result["prod_desc"];
    $response["prod_image"] = $result["prod_image"];
}
else
{
    $response["error"] = true;
    $response["message"] = "products are not shown";
}
} 
echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:2)

在您的函数中,您每次都只是覆盖最后一个数据,因此您需要构建此数据。使用新数据创建一个数组,然后使用$menu[]将此新数据添加到菜单列表中...

public function showProduct(){
    $menu = array();
    $query = mysqli_query($this->con,"SELECT * FROM `products` WHERE 1");
    while ($row = mysqli_fetch_array($query)) {
            $newMenu = [];    // Clear array to ensure no details left over
            $newMenu['prod_name'] = $row['prod_name'] ;
            $newMenu['prod_desc'] = $row['prod_desc'] ;
            $newMenu['prod_image'] = $row['prod_image'];

            $menu[] = $newMenu;
    }
    return $menu;
}