我有一个网页,允许用户将特定记录添加为“收藏夹” - 作为此过程的一部分,它将选择记录ID添加到数组中,如下所示:
array_push($_SESSION['selectedArticleIDs'], $recid);
并且他们也可以取消喜欢从阵列中删除它们的记录:
if (($key = array_search($recid, $_SESSION['selectedArticleIDs'])) !== false) {
unset($_SESSION['selectedArticleIDs'][$key]);
}
因此,数组可能如下所示:
[selectedArticleIDs] => Array
(
[0] => 1398
[2] => 851
[3] => 1635
[4] => 65
[5] => 860
)
然后我按照以下步骤将所有选定的收藏夹分组到一个列表中:
for($i = 0; $i < count($_SESSION['selectedArticleIDs']); $i++) {
${'findreq' . ($i+1)} = $fm->newFindRequest('Articles');
${'findreq' . ($i+1)}->addFindCriterion('RecordID', $_SESSION['selectedArticleIDs'][$i]);
$request->add($i+1, ${'findreq' . ($i+1)});
}
当有人取消收藏某个项目时会出现问题 - 您可以在上面的示例中看到没有:
[1] => 1234
数组项,在执行查找时会抛出for循环,因为它会跳过数组中的最后一项(上例中为860
)。
我可以看到问题在这里但不确定如何解决这个问题?
答案 0 :(得分:0)
您可以使用<div class="container">
<ul class="listingResult">
<li>
<div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
<div class="listingResultInfo">
<h4 class="price">$12,000</h4>
<p class="address info">Co Rd AFChampion, MI 49814</p>
<p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1103145">1103145</a></p>
<ul class="basicInfo">
<li class="bed">3</li>
<li class="bath">1</li>
<li class="area">1103 sqft</li>
</ul>
</div>
</li>
<li>
<div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
<div class="listingResultInfo">
<h4 class="price">$12,500</h4>
<p class="address info">GenevaIronwood, MI 49938</p>
<p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1100143">1100143</a></p>
<ul class="basicInfo">
<li class="bed">3</li>
<li class="bath">2</li>
<li class="area">1717 sqft</li>
</ul>
</div>
</li>
<li>
<div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
<div class="listingResultInfo">
<h4 class="price">$12,500</h4>
<p class="address info">SaginawNorway, MI 49870</p>
<p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1104097">1104097</a></p>
<ul class="basicInfo">
<li class="bed">3</li>
<li class="bath">1</li>
<li class="area">828 sqft</li>
</ul>
</div>
</li>
<li>
<div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
<div class="listingResultInfo">
<h4 class="price">$12,900</h4>
<p class="address info">NorthWakefield, MI 49968</p>
<p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1101426">1101426</a></p>
<ul class="basicInfo">
<li class="bed">3</li>
<li class="bath">2</li>
<li class="area">935 sqft</li>
</ul>
</div>
</li>
</ul>
</div>
循环。
foreach
如果它不能解决问题,那么你必须添加if条件来检查密钥是否退出。我不确定你是否包含在foreach($_SESSION['selectedArticleIDs'] as $i => $v) {
${'findreq' . ($i+1)} = $fm->newFindRequest('Articles');
${'findreq' . ($i+1)}->addFindCriterion('RecordID', $v);
$request->add($i+1, ${'findreq' . ($i+1)});
}
对象中。
答案 1 :(得分:0)
有不同的方法可以修复它。
您可以使用foreach
代替for
循环
$i = 1;
foreach($_SESSION['selectedArticleIDs'] as $favourite){
${'findreq' . $i} = $fm->newFindRequest('Articles');
${'findreq' . $i}->addFindCriterion('RecordID', $favourite);
$request->add($i, ${'findreq' . $i});
$i++;
}
或者,您可以使用array_values
来重新索引数组
$selectedArticleIDs = array_values($_SESSION['selectedArticleIDs']);
for($i = 0; $i < count($selectedArticleIDs); $i++) {
${'findreq' . ($i+1)} = $fm->newFindRequest('Articles');
${'findreq' . ($i+1)}->addFindCriterion('RecordID', $selectedArticleIDs[$i]);
$request->add($i+1, ${'findreq' . ($i+1)});
}
答案 2 :(得分:0)
array_search调用是不必要的开销,你可以删除它,效果是一样的。 你不应该担心钥匙。而不是for循环,使用foreach。
$i = 1;
foreach($_SESSION['selectedArticleIDs'] as $selectedArticleID) {
${"findreq$i"} = $fm->newFindRequest('Articles');
${"findreq$i"}->addFindCriterion('RecordID', $selectedArticleID);
$request->add($i, ${"findreq$i"});
$i++;
}
你真的需要变量变量名吗?我想你应该尽量避免它。