duanpao4522 2019-04-11 14:37
浏览 51
已采纳

什么是组合函数而不是为每个数据重复它们的首选方法? [关闭]

I have created a set of functions to retrieve and display data from a database using a mysqli connection.

The functions basically do the same thing but for different fields of data requested. The functions below retrieve data from one table but i also need to get data from another table but would rather not just rewrite the same functions with a different query for each table.

For example the functions below display data for breakfast menu, i now need to create the evening menu which will use similar data but stored in a different table.

Im sure there is a much more efficient way of doing this but i'm inexperienced and would like some guidance or pointers towards a better solution. I am not asking for someone to do it for me, i want to learn for me myself with guidance :)

I guess the SQL query needs to be made dynamic where it selects a group and the function somehow needs to able to call the required group but i'm not sure how: function call would end up looking like this:

get_menu(breakfast_menu);

Where get_menu retrieves all the data and then the argument is where the group in this case breakfast_menu is selected.


function connect () {
    include_once ("config.php");
    static $conn;
    if ($conn===NULL){ 
        $conn = mysqli_connect ("$host", "$username", "$password", "$db");
    }
    return $conn;
}

function get_breakfast_menu() {
    $conn = connect();

        $query = "SELECT * FROM menuitems where `group`='breakfast_menu'";

            if ($result = mysqli_query($conn, $query)) {

            /* fetch associative array */
            while ($row = mysqli_fetch_assoc($result)) {

                    // data inside html goes here
                        }
                 }
            /* free result set */
            mysqli_free_result($result);
        }
        else{
    echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}


        }

function get_sandwiches() {
            $conn = connect();

        $query = "SELECT * FROM menuitems where `group`='sandwiches'";

            if ($result = mysqli_query($conn, $query)) {

            /* fetch associative array */
            while ($row = mysqli_fetch_assoc($result)) {

                    // data inside html goes here

                }
            }
            /* free result set */
            mysqli_free_result($result);
        }
        else{
    echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}

        }

function get_lunch_favourites() {
            $conn = connect();

        $query = "SELECT * FROM menuitems where `group`='lunch_favourites'";

            if ($result = mysqli_query($conn, $query)) {

            /* fetch associative array */
            while ($row = mysqli_fetch_assoc($result)) {

                    // data inside html goes here

                }
            }
            /* free result set */
            mysqli_free_result($result);
        }
        else{
    echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}

        }


function get_light_bites() {
            $conn = connect();

        $query = "SELECT * FROM menuitems where `group`='light_bites'";

            if ($result = mysqli_query($conn, $query)) {

            /* fetch associative array */
            while ($row = mysqli_fetch_assoc($result)) {

                    // data inside html goes here
}
            }
            /* free result set */
            mysqli_free_result($result);
        }
        else{
    echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}

        }


function get_sides() {
            $conn = connect();

        $query = "SELECT * FROM menuitems where `group`='sides'";

            if ($result = mysqli_query($conn, $query)) {

            /* fetch associative array */
            while ($row = mysqli_fetch_assoc($result)) {

                    // echo'd data inside html goes here
}
            }
            /* free result set */
            mysqli_free_result($result);
        }
        else{
    echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}

        }

  • 写回答

2条回答 默认 最新

  • dsgm5631 2019-04-11 14:54
    关注

    This should be pretty simple assuming that you are specifying $group and it's not user supplied data:

    function get_menu($group) {
        $conn = connect();
        $query = "SELECT * FROM menuitems where `group`='$group'";
        //other code
    }
    

    If $group is user supplied ($_GET, $_POST etc..) then:

    function get_menu($group) {
        $conn = connect();
        $query = "SELECT * FROM menuitems where `group`= :group";
        $stmt = $conn->prepare($query);
        $stmt->bind_param('group', $group);
        $stmt->execute();
        //other code
    }
    

    Call with: get_menu('breakfast_menu');

    You would be better off refactoring into a class so that you can do other things like only call connect() once:

    class myClass {
        private $conn;
    
        public function __construct() {
            $this->conn = $this->connect();
        }
    
        //other functions that use $this->conn
    }
    

    Also you really should separate the logic and the presentation, so maybe build a menu_template to display.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题