我仍然在学习,我发现我无法在PHP中运行PHP,在阅读了为什么您不能这样做(D‘噢!)之后,我发现的是正确的方法。
我有两个按钮链接到不同的模式,如果我没有在if语句中使用它们,它们会在我添加到IF语句后立即正常工作,它会破坏页面。(显然是因为我试图在php中运行php)
我想要做的是显示一个不同的按钮,这取决于MySQL表中一个名为"status“的列的结果,如果它等于1,它将显示编辑按钮(如果有其他的话),它将显示一个签出按钮。我需要把<?php echo $fetch['id']?>传递给数据目标,我不知道该怎么做。
<?php
$status_code = $fetch["status"];
if("$status_code" == "1")
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?
php echo $fetch['id']?>"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else
{ echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?
php echo $fetch['id']?>"></span>Check-Out</button>';
} ?>任何帮助都是非常感谢的。
发布于 2022-09-06 23:06:14
您只需要简单地与. (点)操作符连接。
例如。
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal'.$fetch['id'].'"><span class="glyphicon glyphicon-plus"></span>edit</button>';...etc。
这用于将任意两个字符串值(无论是硬编码文本还是变量)连接在一起。这里发生的事情是,您的代码是从几个组件构建一个字符串,然后回显它。
文档:https://www.php.net/manual/en/language.operators.string.php
发布于 2022-09-07 01:02:51
您不需要在PHP中运行PHP。
<?php
if ($status_code == "1") {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<'
, $fetch['id']
, '"><span class="glyphicon glyphicon-plus"></span>edit</button>';
} else {
echo '<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal'
, $fetch['id']
, '">Check-Out</button>';
}或者使用short tags
<?php if ($status_code === '1') : ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#update_modal<?= $fetch['id'] ?>">
<span class="glyphicon glyphicon-plus"></span>edit</button>'
<?php else: ?>
<button type="button" class="button-7" data-toggle="modal" data-target="#checkout_modal<?= $fetch['id'] ?>">Check-Out</button>'
<?php endif; ?>您可以在短标签中包含条件(和其他表达式):
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?=
$status_code === '1'
? 'update_modal'
: 'checkout_modal'
?><?= $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>并将字符串与点连接起来:
<?php
$dataTargetPrefix =
$status_code === '1'
? 'update_modal'
: 'checkout_modal';
?>
<button
type="button"
class="button-7"
data-toggle="modal"
data-target="#<?= $dataTargetPrefix . $fetch['id'] ?>">
<?php if ($status_code === '1') : ?>
<span class="glyphicon glyphicon-plus"></span>edit
<?php else: ?>
Check-Out
<?php endif; ?>
</button>https://stackoverflow.com/questions/73628491
复制相似问题