Php 数组中的未知变量
有没有一种方法可以向数组中添加某些类型的参数(未知变量)?正如您在这里看到的,我事先不知道用户ID(在mysql获取之前),因此我无法正确地形成指向编辑页面的链接Php 数组中的未知变量,php,Php,有没有一种方法可以向数组中添加某些类型的参数(未知变量)?正如您在这里看到的,我事先不知道用户ID(在mysql获取之前),因此我无法正确地形成指向编辑页面的链接 <?php $box = array ('1'=>"<a href='edit.php?id=/PROBLEM??/'>edit</a>",'2'=>'Cannot edit'); while ($row = mysql_fetch_array($something)) { ?>
<?php
$box = array ('1'=>"<a href='edit.php?id=/PROBLEM??/'>edit</a>",'2'=>'Cannot edit');
while ($row = mysql_fetch_array($something)) {
?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<td><?php echo $box[$row["editable"]]; ?></td>
</tr>
<?php
}
?>
$row[“可编辑”]返回1或2,具体取决于
返回用户是否可编辑
您可以使用sprintf()
:
$box=array('1'=>“”,'2'=>'无法编辑”);
echo sprintf($box[$row[“可编辑”]],此处为ID_)
用这种方法处理
<?php while ($row = mysql_fetch_array($something)) : ?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<?php if( $row["editable"] === 1 ) : ?>
<td><a href='edit.php?id=<?php echo $row["Id"]; ?>'>edit</a></td>
<?php else : ?>
<td>Cannot edit</td>
<?php endif; ?>
</tr>
<?php endif; ?>
无法编辑
试试:
$box=array('1'=>“”,'2'=>'无法编辑”);
$link=str_replace(“%ID%”,$row[“ID”],$box[$row[“可编辑”]);
为什么不将链接拆分为前缀和后缀,然后如果$row[“editable”]为1,则回显前缀、用户ID和后缀?在查询后构建链接。这就是为什么我们不将业务逻辑与html混合使用的原因。
<?php while ($row = mysql_fetch_array($something)) : ?>
<tr>
<td><?php echo $row["Name"]; ?></td>
<?php if( $row["editable"] === 1 ) : ?>
<td><a href='edit.php?id=<?php echo $row["Id"]; ?>'>edit</a></td>
<?php else : ?>
<td>Cannot edit</td>
<?php endif; ?>
</tr>
<?php endif; ?>
$box = array ('1'=>"<a href='edit.php?id=%ID%'>edit</a>",'2'=>'Cannot edit');
$link = str_replace('%ID%', $row["id"], $box[$row["editable"]]);