简体   繁体   English

如何打印页面上的单个字段数据而不是整个表格内容

[英]How to print out individual field data on the page instead of the entire table contents

So I have this php code that gets results from a MySQL database and cache them using Memcached.所以我有这个 php 代码,它从 MySQL 数据库获取结果并使用 Memcached 缓存它们。 I need help taking control of how I print the results on the page.我需要帮助来控制如何在页面上打印结果。

From the code below, i need help on how I can print something like this:从下面的代码中,我需要有关如何打印以下内容的帮助:

        echo "<br> id: ". $row["product_id"]
. " - Name: ". $row["product_name"]. " "  
. " - Price: ". $row["retail_price"] . "<br>";

This is the code I need to be modified:这是我需要修改的代码:

<?php

header("Content-Type:application/json");

try {

    $db_name     = 'test_db';
    $db_user     = 'test_db_user';
    $db_password = 'EXAMPLE_PASSWORD';
    $db_host     = 'localhost';

    $memcache = new Memcache();
    $memcache->addServer("127.0.0.1", 11211);

    $sql = 'SELECT
            product_id,
            product_name,
            retail_price
            FROM products
           ';

    $key = md5($sql);

    $cached_data = $memcache->get($key);

    $response = [];

    if ($cached_data != null) {

        $response['Memcache Data'] = $cached_data;

    } else {

        $pdo = new PDO("mysql:host=" . $db_host  . ";dbname=" . $db_name, $db_user, $db_password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

        $stmt = $pdo->prepare($sql);
        $stmt->execute();

        $products = [];

        while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
            $products[] = $row;
        }

        $memcache->set($key, $products, false, 5);

        $response['MySQL Data'] =  $products;

    }

    echo json_encode($response, JSON_PRETTY_PRINT) . "\n";

} catch(PDOException $e) {
    $error = [];
    $error['message'] = $e->getMessage();
    echo json_encode($error, JSON_PRETTY_PRINT) . "\n";
}

Just like you normally would print results of SELECT query.就像您通常会打印 SELECT 查询的结果一样。 Although here it seems there's an ajax call invovled.虽然这里似乎有一个 ajax 调用涉及。

header("Content-Type:application/json");

try {

    $db_name     = 'test_db';
    $db_user     = 'test_db_user';
    $db_password = 'EXAMPLE_PASSWORD';
    $db_host     = 'localhost';

    $memcache = new Memcache();
    $memcache->addServer("127.0.0.1", 11211);

    $sql = 'SELECT
            product_id,
            product_name,
            retail_price
            FROM products
           ';

    $key = md5($sql);

    $cached_data = $memcache->get($key);

    $response = [];

    $result = null;

    if ($cached_data != null) {

        $response['Memcache Data'] = $cached_data;
        $result = $cached_data;

    } else {

        $pdo = new PDO("mysql:host=" . $db_host  . ";dbname=" . $db_name, $db_user, $db_password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

        $stmt = $pdo->prepare($sql);
        $stmt->execute();

        $products = [];

        while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
            $products[] = $row;
        }

        $memcache->set($key, $products, false, 5);


        $response['MySQL Data'] =  $products;
        $result = $products;

    }

    // echo json_encode($response, JSON_PRETTY_PRINT) . "\n";
    $html = "";
    foreach ($result as $row) {
      $html .= "<br> id: ". $row["product_id"]
        . " - Name: ". $row["product_name"]. " "  
        . " - Price: ". $row["retail_price"] . "<br>";
    }

    // return as HTML (won't work in ajax)
    echo $html;

    // or for ajax:
    echo json_encode($html, JSON_PRETTY_PRINT) . "\n";

    // or
    echo json_encode(["html" => $html], JSON_PRETTY_PRINT) . "\n";


} catch(PDOException $e) {
    $error = [];
    $error['message'] = $e->getMessage();
    echo json_encode($error, JSON_PRETTY_PRINT) . "\n";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 joomla 1.6:$ app对象如何打印整个页面内容 - joomla 1.6 : how $app object print out the entire page contents 如何通过php从mysql表中获取所有数据,并打印出每个单元格的内容? - How do I get all data from a mysql table via php, and print out the contents of every cell? 如何使用表格数据打印php页面 - how to print a php page with table data 如何创建表并将 csv 的全部内容导入/插入到该表中 - How to create a table and import/insert the entire contents of csv into that table 如何从第二个表中打印出我的所有数据 - How to print out all of my data from the second table 如何在此 Codeigniter 应用程序中通过 jQuery Ajax 加载特定数据,而不是整个页面? - How do I load specific data via jQuery Ajax in this Codeigniter application, instead of the entire page? 如何使用flush()或ob_flush()使表逐列而不是逐行打印 - How to use flush() or ob_flush() to make a table print out column by column instead of row by row PHP:突破了HTML的PHP​​,但返回值而不是在页面上打印? - PHP: Breaking out of PHP for HTML but return as value instead of print on page? 如何在没有打印对话框窗口的情况下自动打印数据内容(在Div,表格,表单等上)-(WebClientPrint-Neodynamic) - How to print contents of data (on a Div, table, form etc) automatically without a print-dialog window- (WebClientPrint-Neodynamic) 如何在每个打印页面上打印表格的主题? - how to print thead of the table on each print page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM