doutan6286 2015-02-05 20:27 采纳率: 100%
浏览 50
已采纳

PHP + mysql.Dynamic网站,链接

I'm trying to do links like: test.com/article.php?=$id where $id is from data base

  $sql = "SELECT * FROM articles WHERE writer='$w_name' ";
   $result = mysqli_query($conn, $sql);

   $q = array();
   $l = array();

   if (mysqli_num_rows($result) > 0) {

       while ($row = mysqli_fetch_assoc($result)) {
           $q[]=$row['header'];
           $l[]=$row['id'];
       }
   }

So i have for example: $q=1,2,3 and $l=1st link name,2nd link name, 3d link name.

I tried to do that together this way, but it doesn't work correct:

<? 
    foreach($q as $header)
        { 
            echo'<li>'.$header.'</li>';
        } 
?>


<?
    foreach($l as $id) 
        { 
            echo "<a href='article.php?=$id'>".$header.'<br>'.'</a>'; 
         }
?>

In this case i had 3 different links with the same name like:

<a href='article.php?=1'>1st link name</a>
<a href='article.php?=2'>1st link name</a>
<a href='article.php?=3'>1st link name</a>

Also i have tried :

 <? 
foreach($l as $id)
foreach($q as $header) 
{ echo "<a href='article.php?=$id'>".$header.'<br>'.'</a>'; } ?>



But it gave me all combinations like:

<a href='article.php?=1'>1st link name</a>
<a href='article.php?=1'>2nd link name</a>
<a href='article.php?=1'>3d link name</a>

<a href='article.php?=2'>1st link name</a>
<a href='article.php?=2'>2nd link name</a>
<a href='article.php?=2'>3d link name</a>

<a href='article.php?=3'>1st link name</a>
<a href='article.php?=3'>2nd link name</a>
<a href='article.php?=3'>3d link name</a>

I'm new to php and mysql, and i don't know how to solve this problem

  • 写回答

3条回答 默认 最新

  • duanbei6427 2015-02-05 20:36
    关注

    You just need 1 array:

    $sql = "SELECT * FROM articles WHERE writer='$w_name' ";
       $result = mysqli_query($conn, $sql);
    
       $q = array();
    
       if (mysqli_num_rows($result) > 0) {
    
           while ($row = mysqli_fetch_assoc($result)) {
               $q[$row['id']]=$row['header'];
           }
       }
    

    After that just print the array in the format you want:

    foreach($q as $href => $text) {
        echo '<a href="http://test.com/article.php?=' . $href . '">' . $text . '</a><br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao