简体   繁体   English

MySQL-> PHP-> JS / jQuery

[英]MySQL -> PHP -> JS/jQuery

I'm a bit confused about displaying data from a MySQL database. 我对显示MySQL数据库中的数据有些困惑。 I would like to make a single $.getScript(''); 我想做一个$.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. 我需要通过每个用户的ID号来获取点值。 (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. (这是针对论坛软件ZetaBoards的。基本上是一种积分系统。积分显示在用户帖子数下方。)这是我到目前为止的代码..如果需要,还可以发布PHP内容。 +/- 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 displaying the data. :p我只是停留在显示数据上。 D: Here's the main base of the code.. it's currently being tested on this board. D:这是代码的主要基础。目前正在板上进行测试。

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. 第二个链接适用于2个用户-像我说的那样仅发出一个请求。 I'm guessing this is what I'd need to do? 我猜这是我需要做的吗? but how.. that's the question. 但是怎么..那就是问题。 haha. 哈哈。

Only allowed one hyperlink, sooo.. D: 只允许一个超链接,所以.. 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. 我可能可以通过对php文件的单个请求来执行此操作,但是我不希望在单个页面上触发20个以上的请求。 (one for each user) xD Any ideas? (每个用户一个)xD有什么想法吗? Thanks. 谢谢。 (: (:

Oh: In my MySQL database, I have two fields: 'uid' and 'points'. 哦:在我的MySQL数据库中,我有两个字段:“ uid”和“ 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. 我没有把ID弄乱,而是把它们全部扔在同一页上。 Thanks for the help you three. 感谢您的帮助。 ;D ; 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 使用php,创建您的常规数组,然后使用PHP的Json Encode http://php.net/manual/en/function.json-encode.php创建json数组,可以通过javascript读取json数组,请使用此数组jQuery $。获取

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. 您基本上希望表[uid] => points的数组正确,建立一个页面来执行所有php页面,就是运行查询并以定界的方式打印结果。 So on your php page do something like this: 因此,在您的php页面上执行以下操作:

<? 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. 或者您可以在此处使用数组并在结果中创建视图,而js只会从此处提取html,以便此页面完成所有繁重的工作,但这取决于您的要求。 ie 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: 现在,在另一页上,常规页面只需添加以下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: 如果您愿意,可以使用单击处理程序将变量发送到函数以动态构建查询(使用说出的复选框和一个按钮或指向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. 现在我想当然了一些事情,例如您知道如何连接到sql并以数组的形式返回结果,但是如果您需要在js上获得更多指导,请回击,我将很乐意进一步阐述为你的一部分。

Yes, for handling 1 or more queries, I would call the script like in your question: 是的,对于处理1个或多个查询,我会像您的问题一样调用该脚本:

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

Within PHP, I would have something like the following: 在PHP中,我将具有以下内容:

# 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. 然后应该可以使用jQuery进行解析,以提取与每个uid相关的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM