我在我的网站上创建了一个类似按钮(使用ExpressionEngine),它可以正常工作。然而,当我在另一个页面上放置几乎完全相同的代码时,类似按钮的文本总是应该放在最后一页上(与应该是什么相反,虽然刷新页面说得对,所以我不能只是翻转值。这是有效的代码:
<?php
$DB1 = $this->EE->load->database('ext_db', TRUE);
$mapID = "{entry_id}";
$ipAddress = $_SERVER['REMOTE_ADDR'];
$thisquery = "SELECT * FROM mapLikes WHERE ipAddress = '$ipAddress' AND mapID = '$mapID'";
$q = $DB1->query($thisquery);
$results = $q->result_array();
foreach ($q->result() as $row)
{
$liked = $row->liked;
}
$buttontext = 'Like';
$buttonimage = "1";
if ($liked == "1") {
$buttontext = 'Unlike';
$buttonimage = "2";
}
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM mapLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
} else {
$thisquery = "INSERT INTO mapLikes (mapLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
}
}
?>
代码没有:
<?php
$DB1 = $this->EE->load->database('ext_db', TRUE);
$mapID = "{segment_3_category_id}";
$ipAddress = $_SERVER['REMOTE_ADDR'];
$thisquery = "SELECT * FROM categoryLikes WHERE ipAddress = '$ipAddress' AND mapID = '$mapID'";
$q = $DB1->query($thisquery);
$results = $q->result_array();
foreach ($q->result() as $row)
{
$liked = $row->liked;
}
$buttontext = 'Like';
$buttonimage = "1";
if ($liked == "1") {
$buttontext = 'Unlike';
$buttonimage = "2";
}
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM categoryLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
} else {
$thisquery = "INSERT INTO categoryLikes (categoryLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
}
}
?>
正如您所看到的,两段代码之间的唯一区别是$ mapID和查询。但由于某种原因,一个人工作而另一个人没有。任何人都知道可能会发生什么?我认为这是一个php问题,而不是ExpressionEngine问题。
答案 0 :(得分:0)
我认为真正的问题是为什么第一个例子确实有效......
您正在根据$liked
的当前值设置按钮的状态,但如果设置了$_POST['like']
,则您正在更改系统的状态,现在该按钮不正确。如果您切换代码并首先测试$_POST['like']
,那么在对其进行操作后设置$like
的值,您的按钮将显示正确的状态。
答案 1 :(得分:0)
现在想出来,并且还意识到第一位代码与第二位代码有相同的问题。我做的是我将POST脚本设置为按钮状态,如下所示:
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM mapLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
$buttontext = 'Like';
$buttonimage = "1";
} else {
$thisquery = "INSERT INTO mapLikes (mapLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
$buttontext = 'Unlike';
$buttonimage = "2";
}
}