【问题标题】:How to display multiple separate PDF/Excel documents on a website?如何在网站上显示多个单独的 PDF/Excel 文档?
【发布时间】:2014-10-08 13:57:46
【问题描述】:

我有一个客户需要在我为他设计的网站上显示 100 多个价格表,每个用户一个。我知道如何使用 PHP,但我还没有找到一种在网站上显示特定于每个用户的文档的好方法。

我的第一个解决方案是构建一个函数,该函数将在数组中搜索某个经销商 ID。该经销商 ID 将链接到嵌入代码,该代码随后将通过 PHP 插入到页面中。

上述方法的问题是它不适用于超过 3 个条目。至少从我的经验来看。

这是我的旧方法代码。据我所知,语法是正确的。

ini_set('memory_limit', '1024M');
include_once("check_login_status.php");
$log_dealer = $_SESSION['dealer'];
function assignPriceSheet($log_username) {
    $array = array("<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '333333'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '115'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '122'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '136'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '137'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '167'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '169'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '172'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '173'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '199'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '208'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '217'
                    , "<iframe src='pricesheets/Price Sheet.pdf' height='750' width='800'>" => '596017');
    //Returns the price sheet of the logged in user 
    return array_search($log_username, $array);     
}

函数中通常会有大约 400 个条目(我只是想减少代码长度)。

我们将不胜感激另一种解决方案!

【问题讨论】:

  • 因为我不相信他一次想要全部四百个,所以我会根据需要使用 Ajax 一个一个地加载它们。
  • 不,他不想一次全部四百。每个价格表都分配给一个单独的用户。我希望用户能够登录并查看他们的价格表。我已经在网站中实现了 ajax,但是您将如何显示每个用户的每个价格表?
  • 好的,请继续关注,我会尝试解释我的方法;)
  • 您可以将电子表格转换为 HTML 表格吗? IMO 以更好更快的方式呈现数据。
  • @halfer 是的.. 有几个应用程序.. 但这是一个混乱的过程。尤其是图形化的电子表格。

标签: php html pdf iframe web


【解决方案1】:

第一个解决方案(如上使用数组)

// PHP
// define somewhere this array
$price_list = array (
    "username_1" => "/some-folder/price_list_1.php", 
    "username_2" => "/some-folder/price_list_2.php",
    "username_3" => "/some-folder/price_list_3.php"
);

// your price_list.php file
// you may have all of this in one .php file as well

if(isset($_SESSION) && user_is_authorized()){  // check if the user is logged in
  echo "<iframe src='" . $price_list[$_SESSION['username']] . "' height='750' width='800'>";
}

第二种解决方案(无数组)

// you may name the price_list for each user according to their username and you'll have something like this 
if(isset($_SESSION) && user_is_authorized()){  // check if the user is logged in
  echo "<iframe src='/some-folder/" . $_SESSION['username'] . "_price_list.pdf' height='750' width='800'>";
}

在客户端你可能有这样的东西

// you may load the price_list on onload or an onclick event
// don't pass any id with URL due to security issues
window.onload = function(){
  var button = document.geElementById('price_list');      
  button.onclick = function(){      
    var xhr = new XMLHttpRequest();
    var url = "pricelist.php";
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        document.getElementById('price_list_container').innerHTML = xhr.responseText;
      }
    };
    xhr.send();
  };
};

【讨论】:

  • 伙计,我不在乎这是否有效。我会将您的回复设置为我的问题的答案。你这该死的摇滚!!
  • 实际上我今天必须下班,但我明天将实施其中一个解决方案。非常感谢!!
  • @nate_dawg 它必须有效;)如果您需要任何进一步的帮助,请给我留言。
猜你喜欢
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
相关资源
最近更新 更多