开发者

MySQL -> PHP -> JS/jQuery

I'm a bit confused about displaying data from a MySQL database. I would like to make a single $.getScript(''); request to grab point values if possible, but my trouble is getting it to work with multiple members. I need to grab the point values for each user by their ID number. (this is for a forum software- ZetaBoards.. basically a point system. Points are displayed below the users post count.) Here's my code so far.. can also post the PHP stuff if needed. +/- functions work flawlessly, although you don't see those options because you're not a moderator. :p I'm just stuck when it comes to 开发者_如何学JAVAdisplaying the data. D: Here's the main base of the code.. it's currently being tested on this board.

http://209.85.62.24/212/175/0/p348841/main.js

A similar system did something like this... although I don't exactly get how it works. The second link is for 2 users- making only one request like I said. I'm guessing this is what I'd need to do? but how.. that's the question. haha.

Only allowed one hyperlink, sooo.. D:

http://themeszetaboards.com/tokens/getpoints.php?uid=260836

http://themeszetaboards.com/tokens/getpoints.php?uid=260836|195145

I could probably do this with single requests to a php file, but I'd rather not fire 20+ requests on a single page. (one for each user) xD Any ideas? Thanks. (:

Oh: In my MySQL database, I have two fields: 'uid' and 'points'. Sorry if this question is too difficult/specific.

Edit:

Alright, so- I figured it out for the most part. Instead of screwing around with the IDs, I just threw 'em all on the same page. Thanks for the help you three. ;D


use php, create your normal array, then use PHP's Json Encode http://php.net/manual/en/function.json-encode.php to create a json array, json arrays can be read via javascript, get this array use jquerys $.Get

where by you send a normal get request.


Actually if you think of it in the simpler form it is easy, I will explain.

You want basically an array of the table [uid]=>points right, well set up a page to do just that all the php page does is run your query and print the results in a delimited fashion. So on your php page do something like this:

<? if($result > 0){ 
       $string = "";
       foreach($result as $key => $value){
           $string = $key.','.$value.'|';
       }
   }

   //then trim the $string to remove the very last pipe.
   $final_value = trim($string,'|');

   //now echo this to the page and put it into a div say id=results
   //now in your div you can do all the styling here or in your 
   //final page it deosn't matter.
?>
   <div id="results">
       <?= $final_value ?>
   </div>

or you could use the array here and create the view within results and the js will just pull the html from here so that this page does all the heavy lifting but that is dependant on your requirements. i.e. how the results will be displayed and all. if it will be grouped together then doing everything here is fine.

Now on your other page the regular one just add this jQuery:

If you wanted to you could use click handlers to send variables to a function to build the query on the fly (using say checkboxes and a button or link to getScript:

function getScript(){    
//to get all checked boxes and glue as one variable you could do this: 
$("input:checked").each(function(){
    var uids = [];
    var points = $(this).val();
    uids.push(points+'|');
});
//cleanup
var final_string = $.trim(uids);

$('#result').load('your/php/file.php?uid='+final_string+' #results', function() { 
      //the #results tells jquery to get the contents of this page only from 
      //the #results div. so the #results content will be placed into the 
      //result div on  this page when the button is clicked. only thing 
      //you have to do now is in this section call another js function 
      //to parse the returned string or do it on the php page in the #results
      //div and show the html here on this page. your choice.
});

now i took a few things for granted like you know how to connect to sql and return the results as an array that kind of thing but if you need more direction on the js here just comment back and i will be happy to elaborate further on any part for you.


Yes, for handling 1 or more queries, I would call the script like in your question:

http://themeszetaboards.com/tokens/getpoints.php?uid=260836|195145

Within PHP, I would have something like the following:

# Get UIDs from GET Parameter, Only allow numeric values (and separator)
$uids = explode( '|' , preg_replace( '/[^\d\|]/' , '' , $_GET['uid'] ) );
/*
Perform MySQL Query and return Array of uids and points
$out = array( '260836'=>'5pts' , '195145'=>'100pts' );
*/
header( 'Content-type: application/json' );
die( json_encode( $out ) );

This should then be able to be parsed with jQuery to extract the points associated with each uid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜