使用mysql、php和ajax(带jquery)创建表格

4

对于我的新项目,我希望采用现代方法,即不需要在每次数据库请求时重新加载页面。 :) 我希望脚本能够查询数据库并创建一个包含查询信息的表格。

我尝试了在互联网上找到的不同脚本。下面这个脚本最接近我的需求。

index.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>
<title>Display Page</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script language='JavaScript' type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
</head>

<body>
<button type='button' name='getdata' id='getdata'>Get Data.</button>

<div id='result_table'>

</div>

<script type='text/javascript' language='javascript'>
$('#getdata').click(function(){

$.ajax({
        url: 'getdata.php',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#result_table').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call    

});
</script>
</body>
</html>

getdata.php

<?php
include('conn.inc.php');
//Query of facebook database
$facebook = mysql_query('SELECT * FROM users')
or die(mysql_error());

//Output results
if(!$facebook)
{
    mysql_close();
    echo json_encode('There was an error running the query: ' . mysql_error());
}
elseif(!mysql_num_rows($facebook))
{
    mysql_close();
    echo json_encode('No results returned');
}
else
{
    $output_string = '';
    $output_string .=  '<table border="1">';
    while($row = mysql_fetch_assoc($facebook))
    {
        $output_string .= '<tr>';
        foreach($row as $value)
        {
            $output_string .= '<td>{$value}</td>';
        }
        $output_string .= '</tr>';
    }
    $output_string .= '</table>';
}

mysql_close();
// This echo for jquery 
echo json_encode($output_string);

?>

但我只得到了一个表格,里面有一堆{$value}。我试过只用$value,但得到了一堆零。

我尝试了一个简单的脚本。

$query = "SELECT users_name, users_password FROM users WHERE users_name = 'name'";
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result);

echo $row['users_name'];

我得到了一些结果,但是使用这个脚本需要在每次搜索时刷新页面。明确一下,我希望能够创建一个表格,从MySQL数据库中获取信息并在屏幕上显示,而不需要重新加载页面。

有什么想法吗?


重新加载页面??...那你为什么要使用ajax...创建一个表单..提交它并获取结果..它将显示带有重新加载的表格... :) - bipen
我猜他的意思是“在不重新加载页面的情况下在屏幕上显示它”。 - Joseph
4个回答

2
你应该直接使用$value而不是{$value}。你不需要在while循环内部再使用另一个foreach循环。
$output_string = '';
$output_string .=  '<table border="1">';
while($row = mysql_fetch_assoc($facebook))
{
    $output_string .= '<tr>';
    $output_string .= '<td>'.$row['Your table column name here'].'</td>';
    $output_string .= '</tr>';
}
$output_string .= '</table>';

谢谢你们两个解决并改进了我的脚本。:) 两个大拇指! - Nicklas
请原谅我,我是这个网站的新手。我该怎么做? - Nicklas
你看到答案旁边有个勾号了吗?请打勾。谢谢。 - Joseph
已勾选;)再次感谢! - Nicklas

0

而不是

$output_string .= '<td>{$value}</td>';

尝试

$output_string .= "<td>{$value}</td>";

即用双引号替换单引号。

请参阅此处的文档here,其中提到:

当在双引号中指定字符串时...其中的变量将被解析。

当它们出现在单引号字符串中时,变量将不会被扩展。


0

百分之百有效 只需删除闭合标签。例如,当打开标签并将其存储在$output_string中时,永远不要包括关闭部分...

然后不要将$value放在引号中... 将其放在引号外,然后用点和分号分隔$value和闭合引号。


0

您可以反转逻辑,在客户端上使用自动执行此操作的库,例如http://phery-php-ajax.net。不要在服务器端创建表格,而是将其作为JSON发送到浏览器,这样更快,并构建您的表格:

Phery::instance()->set(array(
'load' => function(){
  /* rest of mysql code */
  $rows = array();
  while($row = mysql_fetch_assoc($facebook)) { $rows[] = $row; }
  return PheryResponse::factory()->json($rows);
})->process();

然后在客户端内部的 $(function(){});

$(function(){
  var $result_table = $('#result_table'); 
  $result_table.phery('make', 'load');
  $result_table.bind('phery:json', function(e, data){
    var $this = $(this);
    for (var i = 0; i < data.length; i++) {
      $this.append($('<tr/>', {
        'html': '<td>' + data[i].row_name + '</td>'
      }));
    }
  });
  $result_table.phery('remote');
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接