php 如何在数据表中添加每个国家的国旗

9q78igpj  于 2023-01-19  发布在  PHP
关注(0)|答案(2)|浏览(171)

这是我的javascript和标志

<script type="text/javascript">
$(document).ready(function() {

var dataTable =  $("#example").DataTable( {
                processing: true,
                bSort: false,
                serverSide: true,
                iDisplayLength: 10,
    "ajax": {
            "url": "json/j-t.php",
            "type": "POST"
        },
    
} );

              $("#example_filter").css("display","none");  // hiding global search box
               
                 $(".example-search-input").on("keyup click change", function () {
                    var ixoh =$(this).attr("id");  // getting column index
                    var vxoh =$(this).val();  // getting search input value
                    dataTable.columns(ixoh).search(vxoh).draw();
                    
                    if(ixoh != null || ixoh != null){ dataTable.columns(ixoh).search(vxoh).draw(); };
                } );

</script>

下面是j-t.php文件

$sql = "SELECT  *";
$sql.=" FROM info WHERE type='1' AND sold='0'";
$query=mysqli_query($conn, $sql) or die;
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; 

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();
    


    $nestedData[] = $row["types"];
    $nestedData[] = $row["country"];
    $nestedData[] = $row["infos"];
    $nestedData[] = $row["price"];
    $nestedData[] = $row["email"];

    $data[] = $nestedData;
}

$json_data = array(
            "draw"            => intval( $requestData['draw'] ), 
            "recordsTotal"    => intval( $totalData ), 
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => $data 
            );

echo json_encode($json_data);

这是我数据库

SELECT * FROM `info` WHERE `country` LIKE 'CA' ORDER BY `date_added` DESC

我需要添加数据表,就像国家名称CA US DE GB等...我喜欢在数据表国家行中添加标志和国家名称

9udxz4iz

9udxz4iz1#

您可以从这样的地方下载所有SVG标志...
https://observablehq.com/@slattery/iso-3166-svg-flags
...然后使用一个ALTER TABLE info ADD COLUMN查询,后跟一组UPDATE查询,向数据库中添加一列,其中包含与标记文件关联的名称。
PHP逻辑(或Twig、Blade,或您正在使用的任何东西)会将该名称转换为一个实际的<img>标记,以包含在表中。

wfveoks0

wfveoks02#

您可以按如下所述使用列呈现:https://datatables.net/examples/advanced_init/column_render.html
在您的情况下,这可能是这样的:

var dataTable = $("#example").DataTable({
  processing: true,
  bSort: false,
  /*serverSide: true,*/
  iDisplayLength: 10,
  /*"ajax": {
    "url": "json/j-t.php",
    "type": "POST"
  },*/
  columnDefs: [{
    render: function(data, type, row) {
      return '<img src="https://flagcdn.com/16x12/' + data.toLowerCase() + '.png">';
    },
    targets: 1,
  }]

});
<html>

<head>
  <link href="//cdn.datatables.net/1.13.1/css/jquery.dataTables.min.css" rel="stylesheet" />
</head>

<body>

  <table id="example" class="display" style="width:100%">
    <thead>
      <tr>
        <th>Types</th>
        <th>Country</th>
        <th>Infos</th>
        <th>Price</th>
        <th>E-Mail</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Type A</td>
        <td>US</td>
        <td>Some info</td>
        <td>$1000,00</td>
        <td>someone@example.com</td>
      </tr>
      <tr>
        <td>Type B</td>
        <td>DE</td>
        <td>Some info</td>
        <td>&euro;1000,00</td>
        <td>someone@example.com</td>
      </tr>
      <tr>
        <td>Type C</td>
        <td>GB</td>
        <td>Some info</td>
        <td>&pound;1000,00</td>
        <td>someone@example.com</td>
      </tr>
    </tbody>
  </table>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="//cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>
</body>

</html>
  • 在本例中,我注解掉了服务器端部分,并使用了一些固定的HTML数据。您可以删除注解以供使用。*

相关问题