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

时间:2014-08-14 19:29:33

标签: php html pdf iframe web

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

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

1 个答案:

答案 0 :(得分: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();
  };
};