【问题标题】:How can I add my javascript to my php for each loop?如何为每个循环将我的 javascript 添加到我的 php 中?
【发布时间】:2013-10-20 09:43:54
【问题描述】:

如何为每个循环将以下脚本添加到我的 php 中?以便 who_likes?id 传递正确的参数?

任何帮助或示例代码将不胜感激!

我已经包含了有效的脚本和有效的 php,我只是不确定我将如何组合这两个文件。

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script>
$(document).ready(function(){

$(".button").hover(function () {
    $.ajax({
        url: "who_likes.php?id='$id'",
        success: function (result) {
            $("#div1").html(result);
        }
    });
}, function () {
    $("#div1").html("");
});


});
 </script>

 <style type="text/css">
 #div1 {
background:#666;
color:#fff;
width:300px;
position:absolute;
}
 </style>

</head>

<body>
<a href="none.html" class="button">none</a>
<button>Button!</button>
<div id="div1"></div>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div>
</body>

</html>

然后是这个文件

<?php
include './authenticate.php';
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery-1.9.1.js"></script>

<?php
$user_type = $_SESSION["user"]["user_level"];
$user_id   = $_SESSION["user"]["user_id"]; //displays 1008
?>

<h1>Public TipsM/h1>
<?php include 'connection.php';

if ($mysqli->connect_error) {
         die('Connect Error: ' . $mysqli->connect_error);
        }

$liker_id = $_SESSION['user']['username'];
$mysqli = new mysqli('connection here');

if ($mysqli->connect_error) {
    die('Connect Error: ' . $mysqli->connect_error);
}

$query = "SELECT * FROM public_tips ORDER by date"; 
$result = $mysqli->query($query);
while($row = $result->fetch_array()) 
{ $rows[] = $row; }

foreach($rows as $row) 
{ 
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$id = $row['id'];
$booky = $row['booky'];
$sp = $row['sp'];
$placed = $row['placed'];
// if ($sp === 'yes') {$start = ' SP';

$query2 = "select count(distinct liker_id) as counted from likes where id = $id";
$result2 = $mysqli->query($query2);
while($row2 = $result2->fetch_array()) { 
//$likes = $row2['count(liker_id)'];
$likes = $row2['counted'];
}

echo  
'<div style="float:left; width:700px; margin-bottom:10px; margin-top: 10px; margin-left:150px; padding:10px; border: solid 1px grey;">
  <h5>
        ('.$date.") " .
         $row['time'] . " " . 
         $row['course'] . " - " . 
         $row['horse'] . " " . 
         $row['odds1'] . "/" . $row['odds2'] . $row['sp'] . "-" . $row['place'] .
         '</h4><br>'. 
         $row['description'] . '<br/>' . $likes . ' likes - 
         <a href="process_likes.php?table_id='.$table_id.'&id='.$id.'&liker_id='.$liker_id.'" />LIKE</a> | 

//
//I need to add script below here so that each who_likes.php has the right parameter??           
//

         <a href="who_likes.php?id='.$id.'"  class="button" target="_blank">who likes?</a> // script here???



         <FORM METHOD="LINK" ACTION="'.$booky.'"><br/><INPUT TYPE="submit" VALUE="Best Odds"></FORM><br/><span style="font-size:10px">Added ' . 

         $row['date_added'] . ' at ' . $row['time_added'] . ' by ' . $row['username'].  '</span></div>' ;

         $horse = $row['horse'];
         $date = $row['date']; 

        echo '</div><div style="clear:both"/>';

}
$result->close(); 
$result2->close();
$mysqli->close(); 

?>

</div>

【问题讨论】:

  • 看起来您的链接中已经包含正确的 ID。 &lt;a href="who_likes.php?id='.$id.'" class="button" target="_blank"&gt;who likes?&lt;/a&gt;你的脚本不能用吗?
  • 您听说过 javascript 模板吗?您从 PHP 返回 JSON 并将其嵌套在 JS 模板中。
  • Moob - 如何将脚本包含到我的 foreach 循环中?每次返回新行时是否必须回显脚本?
  • pbibergal - 不,我没有,你有什么好的参考资料吗?
  • @EdWright 不,您应该只有一个适用于所有人的脚本。它可以使用href中提供的url。尚不清楚返回的“谁喜欢”数据是显示在单个通用元素中,还是显示在每个实例的单独元素中。

标签: javascript php jquery foreach


【解决方案1】:

id 参数已经在a href 中。只需使用它:

$(".wholikesbutton").hover(function () {
    var dataSource = $(this).attr("href");
    var dataDisplay = $(this).siblings(".wholikesdata");
    dataDisplay.text("fetching data from "+dataSource+"...");
    $.ajax({
        url: dataSource,
        cache: false
    })
    .done(function (html) {
        dataDisplay.html(html);
    })
    .fail(function (html) {
        dataDisplay.html("something went wrong (not suprisingly as the data source url doenst exist in this example)");
    });
}, function () {
    //seems a bit silly to actually remove the data when you could just hide it but if that's what you want...
    var dataDisplay = $(this).siblings(".wholikesdata");
    dataDisplay.text("");
});

我模拟了an example to show you the principles。 FWIW 我不会在每次用户悬停时都获取此数据,除非数据可能会非常频繁地更改。

【讨论】:

  • 好吧,但是我的数据肯定会在每次不同的悬停时发生变化,因为我会在返回的每一行上传递不同的参数?
  • 是的,您的数据不同,但正如您将在 the fiddle 中看到的那样,每个数据都在自己的实例中。
【解决方案2】:

正如 Moob 所说,您的链接中似乎已经有了正确的 URL。要访问它,您可以执行以下操作:

$(".button").hover(function (evt) {
    var url = $(evt.target).attr("href");

    $.ajax({
        url: url,
        success: function (result) {
            $("#div1").html(result);
        }
    });
}, function () {
    $("#div1").html("");
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多