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データベースからの情報を使用してテーブルを作成し、ページをリロードして画面に表示できるようにしたいと考えています。

何か案は?

4

4 に答える 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>';
于 2012-11-08T07:11:36.520 に答える
0

サーバー側でテーブルを作成する代わりに、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');
});
于 2012-11-12T03:04:22.437 に答える
0

それ以外の

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

試す

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

つまり、一重引用符を二重引用符に置き換えます。

こちらのドキュメントを参照してください。

文字列が二重引用符で囲まれている場合 ... 変数はその中で解析されます。

変数 ... は、単一引用符で囲まれた文字列内にある場合、展開されません

于 2012-11-08T07:07:02.583 に答える