2012-04-18 3 views
-1

배열에 매개 변수를 추가하는 방법이 있습니까? 당신이 여기에서 볼 수 있듯이, 미리 (mysql 가져 오기 전에) userID를 알지 못한다. 그래서 편집 페이지로 연결되는 링크를 제대로 형성 할 수 없다.아직 알려지지 않은 배열 내부의 변수

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

왜 단지에 링크를 분할하지 prefix와 postfix를 입력하고 $ row [ "editable"]가 1이면 접두어를 echo하고 사용자 ID와 후위? – mgiuffrida

+2

쿼리 후 링크를 ​​작성하십시오. – Madbreaks

+2

이것이 우리가 html과 비즈니스 로직을 섞지 않는 이유입니다. – dqhendricks

답변

4

당신은 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

+0

그냥 완벽 :)) – John

1

주위에 이런 식으로 작업을 수행 ...

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

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"]]); 
관련 문제