for looping in jquery

user2374946

in my php I have such code in a while loop...

$result = mysql_query("SELECT * FROM wallpost ORDER BY wallpostid DESC");
while($row = mysql_fetch_assoc($result)){
$rate = "<div class=\"example-".$id." \" data-postid=\"".$id."\"></div></br>Post id: <span class=\"example-rating-".$id."\">".$id."</span>";
}

jquery is...

$(document).ready(function() {
$('[class^="example-"]').not('[class^="example-rating-"]').ratings(3).bind('ratingchanged', function (event, data) {
var child = $(this).find('[class^="example-rating-"]');
child.text(data.rating);

$.post('count.php', {
     rate: data.rating,
     wallpostid: jQuery(this).data("postid")
}, function (data) {
    alert(data);
});
});

for the value A I get the null value, but if i replace

var a = $('.example-rating-50').html();  //let say the wallpostid is 50

it only can pass the value 50 to count.php If now let say I have 2 wallpostid which is 22 and 50 (loop it with while loop ) if I rate wallpostid is 22 then I want pass the value of $id=22 from php to jquery and $.post to count.php. Do the same this if I rate wallpostid=50.

Patrick Evans

You dont need a for loop in the javascript, there are selectors that can handle this, for instance class^=

PHP

$rate = "<div class=\"example-".$id." \" data-postid=\"".$id."\"></div></br>Post id: <span class=\"example-rating-".$id."\">".$id."</span>";

JS

//Find elements that have class starting with 'example-' but Not example-rating- 
$('[class^="example-"]').not('[class^="example-rating-"]').ratings(3).bind('ratingchanged', function (event, data) {
    //Find Child element that has class starting with 'example-rating-'
    var child = $(this).find('[class^="example-rating-"]');
    child.text(data.rating);

    $.post('count.php', {
         rate: data.rating,
         wallpostid: jQuery(this).data("postid")
    }, function (data) {
        alert(data);
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related