如何为每个循环添加我的JavaScript到我的PHP?

时间:2013-10-20 09:43:54

标签: javascript php jquery foreach

如何为每个循环将以下脚本添加到我的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>

2 个答案:

答案 0 :(得分:1)

id param已经在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我不会在每次用户徘徊时获取这些数据,除非数据可能经常发生变化。

答案 1 :(得分:0)

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

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

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