数组内的未知变量

问题描述:

有没有办法给数组添加某种参数?正如你在这里看到的,我不提前知道用户标识(在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,取决于哪 回报,如果用户是可编辑与否的数据库记录。

+1

为什么不分裂链接到一个前缀和后缀,然后如果$ row [“editable”]为1,则回显前缀,即用户ID和后缀? – mgiuffrida 2012-04-18 23:02:47

+2

查询后建立你的链接。 – Madbreaks 2012-04-18 23:02:52

+2

这就是为什么我们不将业务逻辑与html混合。 – dqhendricks 2012-04-18 23:03:35

您可以使用sprintf()

$box = array ('1'=>"<a href='edit.php?id=%d'>edit</a>",'2'=>'Cannot edit'); 

echo sprintf($box[$row["editable"]], ID_HERE) 
+0

+1我刚刚输入类似的东西... – jeroen 2012-04-18 23:03:59

+0

只是完美:)) – John 2012-04-18 23:26:30

做它周围这样...

<?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; ?> 

尝试str_replace()

$box = array ('1'=>"<a href='edit.php?id=%ID%'>edit</a>",'2'=>'Cannot edit'); 
$link = str_replace('%ID%', $row["id"], $box[$row["editable"]]);