php未加引号的属性值

时间:2013-11-29 10:42:02

标签: php html

您好我正在尝试创建我的第一个REST服务,并且花了一些时间阅读教程,我正在关注一个,在客户端代码中,我在一个不带引号的属性值中得到“=”。在最后一个<li>条目之间的href行中。我看不出什么是错的。我想让这个例子工作,然后为我正在处理的解决方案构建它。

<?php

/*** this is the client ***/

if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the   get parameter action is get_user and if the id is set, call the api to get the user information
{
 $user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);

// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
  <tr>
    <td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
  </tr>
  <tr>
    <td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
  </tr>
  <tr>
    <td>Age: </td><td> <?php echo $user_info["age"] ?></td>
  </tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
  <li>
    <a href=<?php echo "http://localhost/RestClient/index.php?action=get_user&id=" . $user   ["id"]  ?> alt=<?php echo "user_" . $user_["id"] ?>><?php echo $user["name"] ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?> 

3 个答案:

答案 0 :(得分:1)

<a href="<?php echo "http://localhost/RestClient/index.php?action=get_user&id=" . $user   ["id"]  ?>" alt="<?php echo "user_" . $user_["id"] ?>"><?php echo $user["name"] ?></a>

OR

<a href=<?php echo "'http://localhost/RestClient/index.php?action=get_user&id=" . $user   ["id"]."'"  ?> alt=<?php echo "'user_" . $user_["id"]."'" ?>><?php echo $user["name"] ?></a>

答案 1 :(得分:1)

我已更新您的代码,但效果很好

<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>

答案 2 :(得分:0)

可能会有帮助。

<a herf="http://localhost/RestClient/index.php?action=get_user&id="<?php echo $user["id"]; ?> alt="user_"<?php echo $user_["id"]; ?> > <?php echo $user["name"]; ?> </a>

<a herf="http://localhost/RestClient/index.php?action=get_user&id=<?php echo $user['id']; ?>" alt="user_<?php echo $user_['id']; ?>" > <?php echo $user["name"]; ?> </a>