简体   繁体   English

如何显示所选下拉菜单中的类别之一

[英]How do I display one of the categories from selected drop-down

I need help with the code below - I'm trying to display one of the categories like 'genre' or 'platform' when the user selects one of them from the first drop-down menu. 我需要以下代码的帮助-当用户从第一个下拉菜单中选择“流派”或“平台”等类别时,我试图显示其中一个类别。 I don't know how I would do this 我不知道该怎么做

<?php # browser_catalog.php 
include ('includes/session.php');

$page_title = 'Browser Games Catalog';
include ('includes/header.php');

require_once ('mysqli_connect.php'); // Connect to the db.

// Check if the form has been submitted:
if (isset($_POST['submitted'])){

    $errors = array(); // Initialize an error array.

    // Check for a game name:
    if (empty($_POST['cat1'])) {
            $errors[] = 'You forgot to enter game name.';
    } else {
            $where = mysqli_real_escape_string($dbc, trim($_POST['cat1']));
    }

    if (empty($_POST['cat2'])) {
            $errors[] = 'You forgot to enter game name.';
    } else {
            $what = mysqli_real_escape_string($dbc, trim($_POST['cat2']));
    }

    if($where == "Genre"){

        $q = "SELECT game_name AS game, 
        platform AS pf, 
        genre AS ge, 
        game_description AS gd,
        game_price AS gp, 
        game_image AS gi,
        game_headlines AS gh
        FROM game
        WHERE  genre = '$what'";
    }
    else{

        $q = "SELECT game_name AS game, 
        platform AS pf, 
        genre AS ge, 
        game_description AS gd,
        game_price AS gp, 
        game_image AS gi,
        game_headlines AS gh
        FROM game
        WHERE platform = '$what'";
    }

    $r = \mysqli_query($dbc, $q); // Run the query.

    // Count the number of returned rows:
    $num = mysqli_num_rows($r);

    if ($num > 0) { // If it ran OK, display the records.

        echo "<p>There are currently $num number of games for '$search'.</p>\n";

        // Table header.
        echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
            <tr>
            <br>    &#09;<td align="left" padding-left:5px><b>Game Name</b></td>
            <br>    &#09;<td align="left" padding-left:5px><b>Platform</b></td>
            <br>&#09;<td align="left"><b>Genre</b></td>
            <br>    &#09;<td align="left"><b>Game Description</b></td>
            <br>    &#09;<td align="left"><b>Game Price</b></td>
            <br>    &#09;<td align="left"><b>Game Image</b></td>
            <br>    &#09;<td align="left"><b>Game Headlines</b></td>
            </tr>';

        // Fetch and print all the records:
        //<td align="left">' . "<img src = images/web/" . $row['gi']  "/>".  '</td>
        while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

            echo '<tr>

                <br>    &#09;<td align="left">' . $row['game'] . '</td>
                <br>    &#09;<td align="left">' . $row['pf'] . '</td>
                <br>    &#09;<td align="left">' . $row['ge'] . '</td>
                <br>    &#09;<td align="left">' . $row['gd'] . '</td>
                <br>    &#09;<td align="left">' . $row['gp'] . '</td>
                <br> <td align="left">' . $row['gh'] . '</td>

                </tr>'; 
        }       

        echo '</table>'; // Close the table.
        mysqli_free_result ($r); // Free up the resources.  

    } else { // If no records were returned.

        echo '<p class="error">There are currently no registered users.</p>';

    }

    mysqli_close($dbc); // Close the database connection.
}
?>

<div class="page-header">

    <h1 style="font-size:40px"><center></center></h1>
</div>
<form class="form-signin" role="form" action="browser_catalog.php" method="post">


    <?php
    <p>Choose a Category:   
        <select class="form-control" id="element_6" name="category"  value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" /> 
                <option>Platform</option>
                <option>Genre</option>
        </select>
    </p>

    if($cat1 == "Platform"){        
        <p>Platform:    
            <select class="form-control" id="element_6" name="platform"  value="<?php if (isset($_POST['cat2'])) echo $_POST['cat2']; ?>" /> 
                    <option >PC</option>
                    <option >PS4</option>
                    <option >Xbox One</option>
                    <option >Wii U</option>
                    <option >PS3</option>
                    <option >Xbox 360</option>
                    <option >Wii</option>
                    <option >Mac</option>
                    <option >Linux</option>
                    <option >Nintendo 3DS</option>
                    <option >DS</option>
                    <option >PS2</option>
                    <option >iPhone</option>
                    <option >Android</option>
            </select>
        </p>
    }
    else{
        <p>Genre:   
            <select class="form-control" id="element_6" name="genre"  value="<?php if (isset($_POST['cat2'])) echo $_POST['cat2']; ?>"> 
                <option>Action</option>
                <option>Adventure</option>
                <option>Driving</option>
                <option>RPG</option>
                <option>Strategy</option>
                <option>Sport</option>
                <option>Puzzle</option>
            </select>
        </p>
    }
    ?>
    <button style="display:inline; padding:10px; margin-bottom:5px" type="submit" name="submit" class="btn btn-sm btn-primary" />Search Game</button>
    <input type="hidden" name="submitted" value="TRUE" /></p></center>

</form>

<?php
include ('includes/footer.html');
?>

First you have a typpo here 首先你有错字

<p>Choose a Category:   
        <select class="form-control" id="element_6" name="category"  value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" /> 
                <option value=>Platform</option>
                <option >Genre</option>
        </select>
    </p>

here: 这里:

<option value=>Platform</option>

Then you did not open php tags when writting php. 然后您在编写php时没有打开php标签。 example here 这里的例子

if($cat1 == "Platform"){      

Before this, and after end of html you should open php tags like this 在此之前,以及在html结束之后,您应该打开像这样的php标签

<?php
if($cat1 == "Platform"){   

Also on lots of places you mixing html and php 同样在很多地方,您混合使用html和php

Also here mixing html and php. 同样在这里混合html和php。 No opening and closing php tag 没有打开和关闭php标签

    </p>
    }
    else{
        <p>Genre:

Also I will recommend you to use <input type='submit'> instead of <button type='submit'> for submititng form. 我也建议您使用<input type='submit'>而不是<button type='submit'>表单。

Anyway it will be good to edit your question and fix syntax on your code and then we can focus on your problem and you will need to be more precise what is really a problem with code. 无论如何,最好编辑您的问题并在代码上修复语法,然后我们可以专注于您的问题,您将需要更加精确地了解代码真正存在的问题。 Thanks Edit: Here is one example how to do it 谢谢编辑:这是一个例子

// not here remove this line ->    <?php
<p>Choose a Category:   
    <select class="form-control" id="element_6" name="category"  value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" /> 
            <option>Platform</option>
            <option>Genre</option>
    </select>
</p>
// But instead here put  <?php
if($cat1 == "Platform"){ 
// And here close it ->   ?>  

Rest you must do for yourself since I can not write full code for you, and is also good for you to learn on your own mistakes, and I can be here to provide help if I can. 休息吧,您必须为自己做些事情,因为我无法为您编写完整的代码,并且也有助于您学习自己的错误,如果可以,我可以在这里提供帮助。 Read w3 school website, there you can find basics of how to properly write php and html 阅读w3学校网站,在那里您可以找到如何正确编写php和html的基础知识

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于从下拉菜单中选择的选项以HTML显示文本? - How do I display Text in HTML based on an option selected from a Drop-Down Menu? 如何使用“发布”方法将所有选定的数据从下拉菜单发送到服务器? - How do I send all selected data from the drop-down menu to the server with a “post” method? 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 如何显示特定于所选下拉选项的大量文本? - How to display a chunk of text specific to the drop-down option selected? 如何在不重新加载页面或不使用按钮的情况下显示(选择下拉菜单)中选定的文本值 - How to display the selected text value from (select drop-down) without reload the page or using button 如何从Bootstrap 4下拉项目菜单显示所选项目 - How display selected item from Bootstrap 4 drop-down item menu 如果下拉列表与json键绑定,如何从下拉列表中获取所选值 - how to get selected value from Drop-down, if drop-down is binded with keys of json 如何检查我是否连续两次选择了相同的下拉列表选项? - How do I check if I have selected the same drop-down list option twice in a row? Javascript:如何生成带有循环的下拉列表以及如何使用更改事件来显示数组中的信息 - Javascript: How to generate a drop-down list with loops and how do I use change events to display info from an array 如何使用 jQuery、JavaScript 和 HTML 动态设置下拉列表的选定选项? - How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM