PHP jQuery正确显示项目


PHP-jQuery Displaying items properly

我有个问题。我想显示页面中的每个"主题"(标题),而不仅仅是像现在这样的第一个。当我添加一条新消息时,索引只显示第一条消息,只有当我点击"打开"按钮时,我才能看到所有消息。

有什么建议吗?

非常感谢。

这是代码:

<?php
require_once("config.php");
if (isset($_SESSION['username']) === FALSE){
    header('location:login.php');
    exit();
}
$where = "";
$searchCriteria = "";
if (isset($_GET['search']) && $_GET['search'] != '') {
    $searchCriteria = mysql_real_escape_string($_GET['search']);
    $where = " WHERE subject LIKE '%" . $searchCriteria . "%'";
    $where .= " OR message like '%" . $searchCriteria . "%'";
}
$sql = "SELECT * FROM notes " . $where . " LIMIT 30";
$result = mysql_query($sql);
?>
<!DOCTYPE html>
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<a href="logout.php">Logout</a><br/><br/>
<div id="wrapper">
    <div id="add-message">
        <a href="add.php"><img src="images/add.png" title="Add"> Add a New   Message</a>
    </div>
    <br>
    <?php
    while ($row = mysql_fetch_assoc($result)) {
    ?>
    <?php echo $row['subject']; ?>
    <input type="button" id="opener" value="Open"/>
        <div id="playbox"> 
            <table id="general">
                <thead>
                    <tr>
                        <th class="general-header"></th>
                        <th class="general-subject">Subject</th>
                        <th class="general-message">Message</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" class="general-foot"><input type="button" id="closer" value="Close"/></td>
                    </tr>
                </tfoot>
                <tbody>
                    <tr>
                        <td>
                            <a href="edit.php?id=<?php echo $row['id']; ?>"><img src="images/edit.png" title="Edit"> Edit</a>
                                                     &nbsp;|&nbsp; 
                            <a href="delete.php?id=<?php echo $row['id']; ?>"><img src="images/delete.png" title="Delete"> Delete</a>
                        </td>
                        <td class="subject"><?php echo $row['subject']; ?></td>
                        <td><?php if ($row['filename']!=''){?>
                            <img align="right" width="300px" src="<?php echo $row['filename']; ?>" />
                            <?php } ?>
                            <?php echo $row['message']; ?>
                        </td>
                    </tr>
                </tbody>
            <?php
            }
            ?>
            </table>
        </div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
    $("#playbox").hide();
    $("#opener").click(function(){
        $("#playbox").slideDown(600); 
    });
    $("#closer").click(function(){
        $("#playbox").slideUp(600); 
    });
});
</script>

我认为您应该使用mysql_num_rows,并且您不需要在while loop中创建许多openclose buttons,只需像添加records then一样添加一次即可,因为您必须像if(mysql-num_rows($result)) 一样添加if condition

完整代码

<?php
    require_once("config.php");
    if (isset($_SESSION['username']) or $_SESSION['username']=== FALSE){
        header('location:login.php');
        exit();
    }
    $where = "";
    $searchCriteria = "";
    if (isset($_GET['search']) && $_GET['search'] != '') {
        $searchCriteria = mysql_real_escape_string($_GET['search']);
        $where = " WHERE subject LIKE '%" . $searchCriteria . "%'";
        $where .= " OR message like '%" . $searchCriteria . "%'";
    }
    $sql = "SELECT * FROM notes " . $where . " LIMIT 30";
    $result = mysql_query($sql);
?>
<!DOCTYPE html>
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<a href="logout.php">Logout</a><br/><br/>
<div id="wrapper">
    <div id="add-message">
        <a href="add.php"><img src="images/add.png" title="Add"> Add a New   Message</a>
    </div>
    <br>
    <?php
    if(mysql_num_rows($result))
    {
        ?>
        <input type="button" id="opener" value="Open"/>
        <div id="playbox"> 
            <table id="general">
                <thead>
                    <tr>
                        <th class="general-header"></th>
                        <th class="general-subject">Subject</th>
                        <th class="general-message">Message</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td colspan="4" class="general-foot"><input type="button" id="closer" value="Close"/></td>
                    </tr>
                </tfoot>
                <?php
                while ($row = mysql_fetch_assoc($result)) {
                ?>
                <?php echo $row['subject']; ?>
                <tbody>
                    <tr>
                        <td>
                            <a href="edit.php?id=<?php echo $row['id']; ?>"><img src="images/edit.png" title="Edit"> Edit</a>
                                                     &nbsp;|&nbsp; 
                            <a href="delete.php?id=<?php echo $row['id']; ?>"><img src="images/delete.png" title="Delete"> Delete</a>
                        </td>
                        <td class="subject"><?php echo $row['subject']; ?></td>
                        <td><?php if ($row['filename']!=''){?>
                            <img align="right" width="300px" src="<?php echo $row['filename']; ?>" />
                            <?php } ?>
                            <?php echo $row['message']; ?>
                        </td>
                    </tr>
                </tbody>
                <?php
                }
            ?>
            </table>
        </div>
        <?php
    }
    ?>
</div>
</body>
</html>
<script>
$(document).ready(function(){
    $("#playbox").hide();
    $("#opener").click(function(){
        $("#playbox").slideDown(600); 
    });
    $("#closer").click(function(){
        $("#playbox").slideUp(600); 
    });
});
</script>

如果你想单独toggle,那么你必须像一样编码

HTML

设有两条记录CreateHTML,类似于使用while loop

<div class="contentSubject">
    <input type="button" class="btnOpener" value="Open" />
    <div class="playbox">
        <!--your table goes here-->
    </div>
</div>
<div class="contentSubject">
    <input type="button" class="btnOpener" value="Open" />
    <div class="playbox">
        //your table goes here
    </div>
</div>

脚本

<script>
    $(document).ready(function(){
        $(".playbox").hide();
        $(".btnOpener").click(function(){
            $(this).closest('.contentSubject')
                   .find(".playbox").slideDown(600); 
        });
        // same code for close
    });
</script>

请尝试该代码

$("#opener").live("click",function(){
    $("#playbox").slideDown(600); 
});