dongyao4003 2013-06-06 17:14
浏览 37
已采纳

HREF PHP中的循环和逗号

I have to echo a list in a loop and remove last comma, what Ive done so far works with comma issue but when I try to echo a href it gives me all the results in one href tag.

This is my code

$select_actors=mysql_query("SELECT * FROM actors_in_movies WHERE  movie_ref='$movie_ref'");
if (mysql_num_rows($select_actors)>=1) {
    while ($row=mysql_fetch_array($select_actors)) {
        $actor_ref=$row['actor_ref'];  
        $select_actor_name=mysql_query("SELECT * FROM actors WHERE actors_ref='$actor_ref' AND     active='1'");
        while ($row_actor=mysql_fetch_array($select_actor_name)) {
            $actor_name .= $row_actor['actors_name'] . ", ";
        }
    }
    $actor_name = substr(trim($actor_name), 0, -1);
    echo'<a href="../actors/index.php?name='.$actor_name.'">'.$actor_name.'</a>';
}

All I need to do is list it list name1, name2, name3 but each need to have their own url (href).

  • 写回答

3条回答 默认 最新

  • dsij89625 2013-06-06 17:25
    关注

    I suggest using your loop to construct an array of links. Then you can output the links using implode.

    Something like this:

    $actor_links=array();
    
    // build array of links from db result
    while ($row_actor=mysql_fetch_array($select_actor_name)) {
      $actor_links[]= '<a href="../actors/index.php?name='.
                        $row_actor['actors_name'].'">' .
                        $row_actor['actors_name'] .
                      '</a>';
    }
    
    
    // output each link inside a <p> tag
    if (!empty($actor_links)) {
      echo "<p>".implode("</p><p>",$actor_links)."</p>";
    }
    
    // alternatively, output links separated by commas
    if (!empty($actor_links)) {
      echo implode(",",$actor_links);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)