ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • php 1차원 배열로 점심 메뉴 추천 만들기
    back-end/php 배열 2023. 6. 29. 22:29
    반응형

    ▶ 점메추 만들러 가보기!

    https://flusteredexoticcommas--yppeu.repl.co/

     

    점메추

     

    flusteredexoticcommas--yppeu.repl.co

     

     

    <?php 
        // $_SERVER['REQUEST_METHOD']는 현재 요청이 어떤 HTTP 메서드를 사용하는지를 확인하기 위해 사용
        if ($_SERVER['REQUEST_METHOD'] === 'GET') {
            if (isset($_GET['menu1']) && isset($_GET['menu2']) &&     
                isset($_GET['menu3']) && isset($_GET['menu4'])) {
                $menu1 = $_GET['menu1'];
                $menu2 = $_GET['menu2'];
                $menu3 = $_GET['menu3'];
                $menu4 = $_GET['menu4'];
    
            $menuall = array($menu1, $menu2, $menu3, $menu4);
            $arr_cnt = count($menuall) - 1;
            $rand = strval(rand(0, $arr_cnt));
        }
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>점메추</title>  
        <style>
          body{
            text-align:center;
            letter-spacing:-1px;
            background:pink;
            padding-top:40px;
          }
          h1{
            text-align:center;
          }
          input{
            border:none;
            border-radius:10px;
            padding:6px;
            margin-bottom:10px;
          }
    
          button{
            border:none;
            border-radius:10px;
            padding:10px 20px;
            font-weight:bold;
            cursor:pointer;
          }
        </style>
    </head>
    <body>
        <h1>점메추 해드립니다.</h1>
        <form method="GET" action="">
            메뉴1 : <input type="text" name="menu1"/><br/>
            메뉴2 : <input type="text" name="menu2"/><br/>
            메뉴3 : <input type="text" name="menu3"/><br/>
            메뉴4 : <input type="text" name="menu4"/><br/><br>
            <button type="submit">결과</button>
            
        </form>
        <h2>오늘의 점심 메뉴는?</h2>
        <?php 
    
      // $menuall 변수를 출력할 때 배열의 요소를 개별적으로 출력하거나, implode() 함수를 사용하여 배열의 요소들을 문자열로 결합
        if (!empty($menuall)) {
            echo "전체메뉴 : "  . implode(", ", $menuall) . '<br>'.'<br>';
            echo "오늘의메뉴 : " . $menuall[$rand];
        }
        ?>
    </body>
    </html>
    반응형
Sua by Tistory.