点击不显示div元素的函数

时间:2015-08-14 13:54:49

标签: javascript php jquery html css

我有一个我正在创建的管理面板。我有一个左侧面板部分,然后右侧显示单击面板按钮时的div。我创造了一个小提琴来展示它的样子,并帮我解释一下......

https://jsfiddle.net/jq8c51c9/

在小提琴中它的工作原理就像它应该的那样,但我拿出了所有的PHP。问题在于公告div,它显示了它下面的联盟会费的div。此外,一旦你点击公告,然后点击另一个面板按钮,如果我再次点击公告,唯一会出现的是这个..

Announcements Current Announcements
League Dues

再次,这不是在小提琴中这样做的。

以下是该问题所在区域的完整代码。我一直坚持这一点,并且无法弄清楚为什么我只有这两个div有困难。

有谁看到我做错了什么?

                通告     

try {
    //Prepare
     $con = mysqli_connect("localhost", "", "", "");
     if ($user_stmt = $con->prepare("SELECT `id` FROM users")) {

        $user_stmt->execute();
        $user_stmt->bind_result($user_id); 

        if (!$user_stmt) {
            throw new Exception($con->error);
        }
        $user_stmt->store_result();
         $user_result = array();
                 //while ($user_row = $user_stmt->fetch()) {
?>               
     <div class="announcement_success"></div>
            <p>Add New Announcement</p>
                <form action="" method="POST" id="insert_announcements">
                <input type="hidden" value="<?php echo $userid; ?>" id="approved_id" name="user_id" />
                    <textarea rows="4" cols="50" id="announcement_message" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
                    <label for="contactButton">
                        <button type="button" class="contactButton" id="submit_announcement">Add Announcement</button>
                    </label>
                </form>
<?php

    if ($announcements_stmt = $con->prepare("SELECT * FROM announcements")) {

        $announcements_stmt->execute();
        $announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date); 

        if (!$announcements_stmt) {
            throw new Exception($con->error);
        }
        $announcements_stmt->store_result();
         $announcements_result = array();

?>

            Current Announcements
            <table>
                <tr>
                    <th>ID</th>
                    <th>Username</th>
                    <th>Message</th>
                    <th>Date</th>
                </tr>   
<?php
        while ($row = $stmt->fetch()) {
?>
                <tr>
                    <td><?php echo $announcements_id; ?></td>
                    <td><?php echo $announcements_username; ?></td>
                    <td><?php echo $announcements_messages; ?></td>
                    <td><?php echo $announcements_date; ?></td>
                </tr>   
            </table>
<?php
        }  
    }
}
}
    catch (Exception $e)
    {
        echo "Error: " . $e->getMessage();
    }   
?>          
            </div>
            <div id='dues'>League Dues</div>
        </div>

1 个答案:

答案 0 :(得分:1)

构建公告表时出错。 结束</table>标签位于while循环中,因此html将被搞砸,使关闭表后的所有内容都消失。

因此将while循环更改为:

....
<?php
    while ($row = $stmt->fetch()) {
?>
            <tr>
                <td><?php echo $announcements_id; ?></td>
                <td><?php echo $announcements_username; ?></td>
                <td><?php echo $announcements_messages; ?></td>
                <td><?php echo $announcements_date; ?></td>
            </tr>   
<?php
    }
?>
    </table>
<?php
....

无论如何,我建议先在变量中构建你的html,然后再将它回显出来。这使得代码更清晰,并降低了html标签不一致的风险。与HEREDOC一起使用时,您甚至不必为报价烦恼。

在您的情况下,例如可以看起来像这样:

<?php

$table_head = <<<EOT 
             <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Message</th>
                <th>Date</th>
            </tr>
EOT;

$table_content = "";

while ($row = $stmt->fetch()) {
    $table_content.= <<<EOT
            <tr>
                <td>$announcements_id</td>
                <td>$announcements_username</td>
                <td>$announcements_messages</td>
                <td>$announcements_date</td>
            </tr>   
EOT;

}

$announcement_table = "<table>".$table_head.$table_content."</table>";

echo $announcement_table;