我在我的网站(使用 ExpressionEngine)上做了一个点赞按钮,它可以工作。但是,当我将几乎相同的代码放在另一个页面上时,like 按钮的文本始终是它应该在最后一页上的文本(与它应该的相反,尽管刷新页面会正确,所以我不能只是翻转值。这是有效的代码:
<?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 问题。