Creating a table with mysql, php and ajax (with jquery)

前端 未结 3 1425
一整个雨季
一整个雨季 2021-01-06 04:41

For my new project i want the so modern approach of not needing to reload a page on every database request. :) I want the script to query the database and create a table wit

相关标签:
3条回答
  • 2021-01-06 05:00

    You may invert the logic, and do it on client side, using a library that does that automatically, like http://phery-php-ajax.net Instead of creating the table on the server side, send it as JSON to the browser, which is faster, and build your table:

    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();
    

    then in the client side inside $(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');
    });
    
    0 讨论(0)
  • 2021-01-06 05:03

    Instead of

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

    try

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

    i.e. replace the single quotes with double quotes.

    See the doc here, which says:

    When a string is specified in double quotes ... variables are parsed within it.

    and

    variables ... will not be expanded when they occur in single quoted strings.

    0 讨论(0)
  • 2021-01-06 05:17

    You should just use $value instead of {$value}. You don't need another foreach loop inside the while loop.

    $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>';
    
    0 讨论(0)
提交回复
热议问题