数组内的未知变量
问题描述:
有没有办法给数组添加某种参数?正如你在这里看到的,我不提前知道用户标识(在mysql提取之前),所以我不能正确地形成一个导向编辑页面的链接。数组内的未知变量
<?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
}
?>
$行[ “编辑”]返回1或2,取决于哪 回报,如果用户是可编辑与否的数据库记录。
答
做它周围这样...
<?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"]]);
为什么不分裂链接到一个前缀和后缀,然后如果$ row [“editable”]为1,则回显前缀,即用户ID和后缀? – mgiuffrida 2012-04-18 23:02:47
查询后建立你的链接。 – Madbreaks 2012-04-18 23:02:52
这就是为什么我们不将业务逻辑与html混合。 – dqhendricks 2012-04-18 23:03:35