【问题标题】:Display image in server side datatable in php/mysqli and ajax在 php/mysqli 和 ajax 中的服务器端数据表中显示图像
【发布时间】:2016-06-17 06:10:15
【问题描述】:

我正在尝试使用服务器端脚本数据表,我需要在表中添加图像。图像路径存储在数据库中。我需要在我的页面中显示图像。如何在页面中添加图像。这是代码。我希望使用 mysqli 和 ajax 显示图像。

<?php
    //include connection file 
    include_once("connection.php");

    // initilize all variable
    $params = $columns = $totalRecords = $data = array();

    $params = $_REQUEST;

    //define index of column
    $columns = array( 
        0 =>'id',
        1 =>'name', 
        2=>'images',
        3 => 'year',
        4 => 'rank'
    );

    $where = $sqlTot = $sqlRec = "";

    // check search value exist
    if( !empty($params['search']['value']) ) {   
        $where .=" WHERE ";
        $where .=" (name LIKE '".$params['search']['value']."%' ";    
        $where .=" OR year LIKE '".$params['search']['value']."%' ";
        $where .=" OR id LIKE '".$params['search']['value']."%' ";
        $where .=" OR rank LIKE '".$params['search']['value']."%' )";
    }

    // getting total number records without any search
    $sql = "SELECT * FROM `search` ";
    $sqlTot .= $sql;
    $sqlRec .= $sql;
    //concatenate search sql if value exist
    if(isset($where) && $where != '') {

        $sqlTot .= $where;
        $sqlRec .= $where;
    }


    $sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

    $queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));


    $totalRecords = mysqli_num_rows($queryTot);

    $queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch rank holders");

    //iterate on results row and create new index array of data
    while( $row = mysqli_fetch_row($queryRecords) ) { 
    $data[] = $row;
$data[] = "<img src =images/".$data[2].">";


    $json_data = array(
            "draw"            => intval( $params['draw'] ),   
            "recordsTotal"    => intval( $totalRecords ),  
            "recordsFiltered" => intval($totalRecords),
            "data"            => $data   // total data array
            );

    echo json_encode($json_data);  // send data as json format
?>

【问题讨论】:

标签: php ajax mysqli


【解决方案1】:

由于您将图片存储在数据库中,因此您可以在 ajax 中发送您要显示的图片的 base64 编码。

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2014-08-01
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2016-04-05
相关资源
最近更新 更多