Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

html - An alphabetical search box using php

I am trying to make an alphabetical search box using php but the problem is in the localhost all the field with all alphabets are showing.I want to show the field with a particular alphabet . I have a database called comment and a table called comment_table .The name of the file is index.php .

My html codes for alphabetical serach bar

<form action="index.php" method="post" name="search" onclick="submit">
        <a href="index.php?letter=A">A</a> |
        <a href="index.php?letter=B">B</a> |
        <a href="index.php?letter=C">C</a> |
        <a href="index.php?letter=D">D</a> |
        <a href="index.php?letter=E">E</a> |
        <a href="index.php?letter=F">F</a> |
        <a href="index.php?letter=G">G</a> |
        <a href="index.php?letter=H">H</a> |
        <a href="index.php?letter=I">I</a> |
        <a href="index.php?letter=J">J</a> |
        <a href="index.php?letter=K">K</a> |
        <a href="index.php?letter=L">L</a> |
        <a href="index.php?letter=M">M</a> |
        <a href="index.php?letter=N">N</a> |
        <a href="index.php?letter=O">O</a> |
        <a href="index.php?letter=P">P</a> |
        <a href="index.php?letter=Q">Q</a> |
        <a href="index.php?letter=R">R</a> |
        <a href="index.php?letter=S">S</a> |
        <a href="index.php?letter=T">T</a> |
        <a href="index.php?letter=U">U</a> |
        <a href="index.php?letter=V">V</a> |
        <a href="index.php?letter=W">W</a> |
        <a href="index.php?letter=X">X</a> |
        <a href="index.php?letter=Y">Y</a> |
        <a href="index.php?letter=Z">Z</a> |
        <a href="index.php?letter=">View All</a>
    </form>

My php codes

 <div class="rounded bg-light p-3 static" class="position-static" id="cont">
                <?php
                if (isset($_GET['letter'])) {
                    $conn = mysqli_connect('localhost', 'root', '',);
                    if ($conn->connect_error) {
                        die("Connection Failed!" . $conn->connect_error);
                    }
                    mysqli_select_db($conn, 'comment');
                    $char = $_GET['letter'];

                    if ($char) {
                        $query = "SELECT * FROM comment_table WHERE field LIKE '$char%' ";
                        $result = mysqli_query($conn, $query);
                        $count = mysqli_num_rows($result);
                        if ($count >= 1) { ?>
                            <?php while ($row = mysqli_fetch_array($result)) { ?>
                                <?php
                                $sql = "SELECT * FROM comment_table ORDER BY id DESC";
                                $result = $conn->query($sql);
                                while ($row = $result->fetch_assoc()) {
                                ?>
                                    <div class="card mb-2 border-secondery" id="myTable">
                                        <div class="card-header bg-secondary py-1 text-light">
                                            <span id="fieldone" class="float-left font-bold fields">Field : <?= $row['field'] ?></span>
                                            <span class="float-right font-bold">On : <?= $row['date_publish'] ?></span>
                                        </div>
                                        <div class="card-body py-2">
                                            <p class="card-text font-bold"> Posted By : <?= $row['name'] ?></p>
                                            <p class="card-text font-bold"><i class="fas fa-map-marker-alt color-light"></i> Location : <?= $row['location'] ?></p>
                                            <p class="card-text font-bold show-read-more"> Description : <?= $row['description'] ?></p>
                                            <p class="card-text font-bold"> E-mail : <?= $row['email'] ?></p>
                                        </div>
                                        <div class="card-footer py-2">
                                            <div class="float-left">
                                            </div>
                                            <div class="float-right">
                                                <a href="action.php?del=<?= $row['id'] ?>" class="text-danger mr-2" onclick="return confirm('Do you want to delete this comment?');" title="Delete"><i class="fas fa-trash"></i></a>
                                                <a href="index.php?edit=<?= $row['id'] ?>" class="text-success" title="Edit"><i class="fas fa-edit"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                <?php } ?>
                            <?php } ?>
                        <?php
                        } else {
                        ?><script>
                                alert("Record not found");
                            </script><?php
                                    }
                                } else {
                                    $query = "SELECT * FROM comment_table";
                                    $result = mysqli_query($conn, $query);
                                        ?>
                        <?php while ($row = mysqli_fetch_array($result)) { ?>
                            <?php $sql = "SELECT * FROM comment_table ORDER BY id DESC";
                                        $result = $conn->query($sql);
                                        while ($row = $result->fetch_assoc()) {
                            ?>
                                <div class="card mb-2 border-secondery" id="myTable">
                                    <div class="card-header bg-secondary py-1 text-light">
                                        <span id="fieldone" class="float-left font-bold fields">Field : <?= $row['field'] ?></span>
                                        <span class="float-right font-bold">On : <?= $row['date_publish'] ?></span>
                                    </div>
                                    <div class="card-body py-2">
                                        <p class="card-text font-bold"> Posted By : <?= $row['name'] ?></p>
                                        <p class="card-text font-bold"><i class="fas fa-map-marker-alt color-light"></i> Location : <?= $row['location'] ?></p>
                                        <p class="card-text font-bold show-read-more"> Description : <?= $row['description'] ?></p>
                                        <p class="card-text font-bold"> E-mail : <?= $row['email'] ?></p>
                                    </div>
                                    <div class="card-footer py-2">
                                        <div class="float-left">
                                        </div>
                                        <div class="float-right">
                                            <a href="action.php?del=<?= $row['id'] ?>" class="text-danger mr-2" onclick="return confirm('Do you want to delete this comment?');" title="Delete"><i class="fas fa-trash"></i></a>
                                            <a href="index.php?edit=<?= $row['id'] ?>" class="text-success" title="Edit"><i class="fas fa-edit"></i></a>
                                        </div>
                                    </div>
                                </div>
                            <?php } ?>
                        <?php } ?>
                <?php
                                }
                            }
                ?>
            </div>
        </div>
        <div class="send bg-light">
            <div class="post_job">
question from:https://stackoverflow.com/questions/65885882/an-alphabetical-search-box-using-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Both of your SQL queries were doubled for no reason and as @Professor Abronius already mentioned the second query was the reason for the wrong result. A similar double SQL problem happened again down below in the code. I have deleted both redundant + wrong sSQL queries and it should produce the desired result now. (not tested)

<div class="rounded bg-light p-3 static" class="position-static" id="cont">
                <?php
                if (isset($_GET['letter'])) {
                    $conn = mysqli_connect('localhost', 'root', '',);
                    if ($conn->connect_error) {
                        die("Connection Failed!" . $conn->connect_error);
                    }
                    mysqli_select_db($conn, 'comment');
                    $char = $_GET['letter'];

                    if ($char) {
                        $query = "SELECT * FROM comment_table WHERE field LIKE '$char%' ";
                        $result = mysqli_query($conn, $query);
                        $count = mysqli_num_rows($result);
                        if ($count >= 1) { 
                                while ($row = $result->fetch_assoc()) {
                                ?>
                                    <div class="card mb-2 border-secondery" id="myTable">
                                        <div class="card-header bg-secondary py-1 text-light">
                                            <span id="fieldone" class="float-left font-bold fields">Field : <?= $row['field'] ?></span>
                                            <span class="float-right font-bold">On : <?= $row['date_publish'] ?></span>
                                        </div>
                                        <div class="card-body py-2">
                                            <p class="card-text font-bold"> Posted By : <?= $row['name'] ?></p>
                                            <p class="card-text font-bold"><i class="fas fa-map-marker-alt color-light"></i> Location : <?= $row['location'] ?></p>
                                            <p class="card-text font-bold show-read-more"> Description : <?= $row['description'] ?></p>
                                            <p class="card-text font-bold"> E-mail : <?= $row['email'] ?></p>
                                        </div>
                                        <div class="card-footer py-2">
                                            <div class="float-left">
                                            </div>
                                            <div class="float-right">
                                                <a href="action.php?del=<?= $row['id'] ?>" class="text-danger mr-2" onclick="return confirm('Do you want to delete this comment?');" title="Delete"><i class="fas fa-trash"></i></a>
                                                <a href="index.php?edit=<?= $row['id'] ?>" class="text-success" title="Edit"><i class="fas fa-edit"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                  <?php } ?>
                        <?php
                        } else {
                        ?><script>
                                alert("Record not found");
                            </script><?php
                                    }
                                } else {
                                    $query = "SELECT * FROM comment_table ORDER BY id DESC";
                                    $result = mysqli_query($conn, $query);
                                 while ($row = $result->fetch_assoc()) {
                            ?>
                                <div class="card mb-2 border-secondery" id="myTable">
                                    <div class="card-header bg-secondary py-1 text-light">
                                        <span id="fieldone" class="float-left font-bold fields">Field : <?= $row['field'] ?></span>
                                        <span class="float-right font-bold">On : <?= $row['date_publish'] ?></span>
                                    </div>
                                    <div class="card-body py-2">
                                        <p class="card-text font-bold"> Posted By : <?= $row['name'] ?></p>
                                        <p class="card-text font-bold"><i class="fas fa-map-marker-alt color-light"></i> Location : <?= $row['location'] ?></p>
                                        <p class="card-text font-bold show-read-more"> Description : <?= $row['description'] ?></p>
                                        <p class="card-text font-bold"> E-mail : <?= $row['email'] ?></p>
                                    </div>
                                    <div class="card-footer py-2">
                                        <div class="float-left">
                                        </div>
                                        <div class="float-right">
                                            <a href="action.php?del=<?= $row['id'] ?>" class="text-danger mr-2" onclick="return confirm('Do you want to delete this comment?');" title="Delete"><i class="fas fa-trash"></i></a>
                                            <a href="index.php?edit=<?= $row['id'] ?>" class="text-success" title="Edit"><i class="fas fa-edit"></i></a>
                                        </div>
                                    </div>
                                </div>
                            <?php } ?>
                        <?php } ?>
                <?php
                                }
                            }
                ?>
            </div>
        </div>
        <div class="send bg-light">
            <div class="post_job">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...