如何在网站上显示多个单独的PDF/Excel文档


How to display multiple separate PDF/Excel documents on a website?

我有一个客户,需要在我为他设计的网站上显示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多个条目(我只是想减少代码长度)。

一个替代的解决方案将是感激!

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

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