php - in_array 语句的问题
问题描述
我想在产品页面上仅针对某些产品和某些国家/地区显示某些 URL 链接。最初,我检索国家/地区的 IP,然后检查产品 ID。如果产品 ID 在数组中,如果用户来自加拿大,则显示链接 1,如果用户来自美国,则显示链接 2。问题是它没有显示任何东西。包含 in_array 的 if 语句中没有任何反应,并且不确定我做错了什么。
<?php
if ($currentCountry =="CA" || $currentCountry =="US")
{
$p_id = $_product->getId();
$include_id = array(546, 125,135);
?>
<style>
.choose-local {
display: flex;
}
.data-section {
min-width: 33%;
}
.choose-local .data-section span {
float: left;
}
</style>
<?php
if (in_array($p_id,$include_id, TRUE))
{
?>
<div class='choose-local'>
<div class='data-section'>
<span style="">Available Locally from :</span>
</div>
<div class="data-section">
<?php
if ($currentCountry =="CA")
{
?>
<span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
<?php
}
if ($currentCountry =="US")
{
?>
<span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
解决方案
检查天气你的 $p_id 是字符串还是整数
<?php
$currentCountry = "CA";
if ($currentCountry =="CA" || $currentCountry =="US")
{
$p_id = 125;
$include_id = array(546, 125,135);
?>
<style>
.choose-local {
display: flex;
}
.data-section {
min-width: 33%;
}
.choose-local .data-section span {
float: left;
}
</style>
<?php
if (in_array($p_id,$include_id, TRUE))
{
?>
<div class='choose-local'>
<div class='data-section'>
<span style="">Available Locally from :</span>
</div>
<div class="data-section">
<?php
if ($currentCountry =="CA")
{
?>
<span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
<?php
}
if ($currentCountry =="US")
{
?>
<span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
<?php
}
?>
</div>
</div>
<?php
}
}
?>
推荐阅读
- typo3 - 升级:TYPO3 9.5 的typo3_console 中的all 无法正常工作
- php - Mysql SELECT自定义字段(post_meta)用于自定义帖子类型
- javascript -
- php - 字段未添加到表中
- c# - 如何将文件系统添加到 Cosmos?
- c++ - 较新版本的 GCC 引发 reinterpret_cast 错误
- javascript - WrappedComponentRef 未定义 - React Ant 设计,表单组件
- python - 如何调用类中的函数?
- reactjs - 在反应中迭代数组
- reactjs - Material-UI - 响应式网格不会在移动宽度上调整大小