[英]php - unwanted session results
我正在開發一個3頁的Web應用程序。
首先是index.php
帶有用於用戶搜索的搜索欄。
第二個是search.php
當用戶單擊任何結果時,它會顯示帶有諸如 info(title,description,url)的搜索結果,如result_1,result_2,result_3,並將其發送到最終頁面,即show.php
第三頁是show.php
有關用戶點擊結果的信息的顯示。
例如(對應的網址內容將使用iframe顯示)
我嘗試使用二維會話數組,它無法正常工作。
當用戶單擊任何結果時,其他結果的信息將顯示在show.php上
我通過print_r
檢查會話數組的內容,它包含不必要的內容。
有人幫助我,我正在分享我的代碼段。
search.php
<?php
session_start();
$id = 1;
while($row = mysqli_fetch_array($sql))
{
$title = $row['title'];
$description = $row['description'];
$url = $row['content_url'];
$icon = $row['thumb_icon_url'];
$_SESSION['result'][] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
?>
<li name="id" ><a href="show.php?id=<?php echo $id;?>&name=<?php echo $title;?>">View doc</a></li>
<?php
$id++;
?>
show.php
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
?>
<div>
<iframe class="embed-responsive-item item" src="<?php echo $_SESSION['result'][$id]['content_url'];?>"></iframe>
</div>
此數組應僅包含查詢結果。請幫助我修復它
您沒有設置數組的鍵:
$_SESSION['result'] = Array();
$_SESSION['result'][$id] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
添加session_start();
在你的show.php中
如果我理解正確,那么您的問題是您得到了不良結果。
這基本上是因為您始終在所有查詢中都向$ _SESSION ['results']添加結果。
每次您運行search.php時,都將不斷向$ _SESSION ['result']添加項目,從而破壞$ id與數組位置的對應關系。
我將初始化$ _SESSION ['result']
<?php
session_start();
$id = 1;
$_SESSION['results'] = [];
while($row = mysqli_fetch_array($sql)){
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.