在不使用Session或Include的情况下从另一个文件调用PHP变量

时间:2016-01-08 13:23:30

标签: javascript php jquery ajax pagination

所以我正在制作一个使用3个不同文件的分页。页面本身(index.php),包含用于更改页面的JS Ajax脚本的头文件,包含在index.php(header.php)中,以及包含在通过AJAX脚本调用的单独PHP文件中的分页脚本(pagination.php)。

在index.php中我有一个变量,它定义了用户当前名为$ category的类别,我希望在pagination.php中使用此变量来选择显示的结果(仅在子类别2 = $类)。

因为pagination.php是通过文档就绪的ajax脚本调用的,所以无法看到该变量。有没有办法让两人在没有使用Session的情况下进行通信(在更改为其他类别时会更糟)或包含(最终会调用脚本两次)。

的header.php:

<script type="text/javascript">
$(document).ready(function() {
$("#results").load("/includes/pagination.php");
$(".pagination").bootpag({
   total: <?php echo $pages; ?>,
   page: 1,
   maxVisible: 5
}).on("page", function(e, num){
    e.preventDefault();
    $("#results").prepend('<div class="loading-indication"><img src="/images/ajax-loader.gif" style="width: 2em; margin: 0 auto;" /><br />Loading...</div>');
    $("#results").load("/includes/pagination.php", {'page':num});
});
});
</script>

Pagination.php

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/db_connect.php');
//sanitize post value
if(isset($_POST["page"])){
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT,                   FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of   invalid page number
}else{
$page_number = 1;
}

echo $category;
//get current starting point of records
$position = (($page_number-1) * $item_per_page);

//Limit our results within a specified range. 
$results = mysqli_query($mysqli, "SELECT ProductID, SupplierID, ProductName, ProductDesc, ProductURL, Image1URL, Image2URL, Image3URL, Image4URL, Image5URL, ProductCondition, Stock, AvailabilityDate, ProductGTIN, ProductMPN, ProductBrand, ProductGroupID, ProductColour, ProductGender, ProductAgeGroup, ProductMaterials, ProductSize, ProductPSize, Feature1, Feature2, Feature3, Feature4, Feature5, Feature6, Feature7, Feature8, Feature9, Feature10, CostPrice, Markup, Offer, Shipping, ShippingWeight, ShippingLabel FROM products ORDER BY productid ASC LIMIT $position, $item_per_page");

//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '
<table id="productbox">
<tr>
    <th class="producthead" colspan="3"><a href="product.php?id='.$row["ProductID"].'">'.$row["ProductName"].'</a></th>
</tr>
<tr>
    <td class="productimgcell"><img src="'.$row["Image1URL"].'" class="productimg" /></td>
    <td class="productinfo">'.$row["Feature1"].'<br />'.$row["Feature2"].'<br />'.$row["Feature3"].'</td>
    <td class="productprice"><div class="pricebg">'; echo price_calc($mysqli, $row["ProductID"], $row["CostPrice"], $row["Markup"], $row["Offer"]); echo '<span class="priceinfo">inc. VAT</a></div><div style="clear:both;"></div><div class="addtocartbg">Add To Cart</div></td>
</tr>
<tr>
    <td class="productfoot" colspan="3">5/5 Stars - <a href="review.php?id='.$row["ProductID"].'">Write A Review</a></td>
</tr>
</table><br />
';
}
echo '</ul>';

?>

的index.php

<?php
$category = 'AMD';
global $category;
$page_title = 'AMD Motherboards - Motherboards - PC Components';
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/db_connect.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
    $results = mysqli_query($mysqli,"SELECT COUNT(*) FROM products WHERE SubCategory2 = '$category'");
    $get_total_rows = mysqli_fetch_array($results);
    $pages = ceil($get_total_rows[0]/$item_per_page);
include_once($_SERVER['DOCUMENT_ROOT'].'/template/header.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/template/sidemenu.php');
?>

        <div class="contentboxcontainer">
            <div class="centercontentbox">
        <div class="halfcontentboxcontainer">
            <div class="halfcontentbox">
                <div class="contenthead">Deals</div>
                <div class="content">
                    <div class="contentcontainer">
Test
                    </div>
                </div>
            </div>
        </div>
                <div class="halfimgcontentboxl">
                <img src="https://assets.vg247.com/current//2015/07/battlefront_leaked_alpha_tatooine_4.jpg" style="border-radius: 5px; width: 100%;" />
                </div>
            </div>
        </div>
        <div class="contentboxcontainer">
            <div id="contentbox">
                <div class="contenthead">Products</div>
                <div class="content">

                    <div id="results"></div>

                    <div class="pageswrap"><div class="pagination"></div>    <div style="clear:both;"></div></div>

                </div>
            </div>
        </div>

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/template/footer.php');
?>

2 个答案:

答案 0 :(得分:0)

在加载命令中将类别ID作为post变量发送。

var cat = <?php echo $category; ?>
$("#results").load("/includes/pagination.php", {'page':num , 'category':cat});

答案 1 :(得分:0)

对于其他任何对这个问题感兴趣的人,我会发布我的工作,万一有人会发现它有用。

在我的pagination.php中,我添加了对用户当前页面的检查,并将其与我定义的网址进行了比较。如果用户在所述页面上,那么我在那里定义类别。

pagination.php

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/db_connect.php');

if ($_SERVER['HTTP_REFERER'] == $domainurl.'/store/pc-components/motherboards/amd/') {
    $category = 'AMD';
}

我必须使用$ _SERVER [&#39; HTTP_REFERER&#39;],因为它是从JS调用的,$ domainurl是在我的配置文件中定义的(包含在db_connect.php中)。

我现在可以在pagination.php上的mysql查询中调用我的变量

FROM products WHERE SubCategory2 = '".$category."'

不是最干净的工作,但它让我担心不得不重新考虑我这样做的方式。