如何从echo(functions.php)中取出锚元素并将其放到body(index.php)? [英] How to take out the anchor element from echo (functions.php) and place it to body (index.php)?

查看:124
本文介绍了如何从echo(functions.php)中取出锚元素并将其放到body(index.php)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从echo(functions.php)中取出锚元素并将其放置到body(index.php)?
PS:新手在这里,大约两周前试图了解HTML,CSS,Javascript和PHP ..想象一下我到目前为止学到的东西,请不要让它难以理解..只需写下来:-)
谢谢。

How to take out the anchor element from echo (functions.php) and place it to body (index.php)? PS: Newbie here guys, just trying to understand about HTML, CSS, Javascript and PHP about 2 weeks ago.. just imagine what i learned so far, please don't make it difficult to understand.. just write it :-) Thanks.

<!--index.php-->
    <!doctype html>
    <?php include ("functions/functions.php"); ?>
    <html>
    <head>
    <link rel="stylesheet" href="styles/style.css" type="text/css" />
    </head>
    <body>

        <ul id="cats">
          <?php getCats(); ?>
        </ul>

    </body>
    </html>


//functions.php
    <?php
    $con = mysqli_connect("localhost","root","","justLearning");
    if (mysqli_connect_errno())
      {
       echo "The connection was not established: " . mysqli_connect_error();
      }

    function getCats(){
        global $con;
        $get_cats = "select * from categories";
        $run_cats = mysqli_query($con, $get_cats);
        while ($row_cats=mysqli_fetch_array($run_cats)){
            $cat_id = $row_cats['cat_id'];
            $cat_title = $row_cats['cat_title'];
        echo "<a href='index.php?cat=$cat_id'>$cat_title</a>";
        }
    }
    ?>


推荐答案

您忘了< li> ;< / li> 在您的代码中

您需要学习返回一个数组并使用foreach():

You need to learn to return an array and to use foreach():

  <!doctype html>
  <?php include_once("functions/functions.php"); ?>
  <html>
  <head>
  <link rel="stylesheet" href="styles/style.css" type="text/css" />
  </head>
  <body>
<ul id="cats">
<?
     $cats = getCats();        
     foreach($cats as $cat){
        echo "<li><a href='index.php?cat=$cat[cat_id]'>$cat[cat_title]</a></li>";
     }    
?>
<ul>
</body>
</html>
    function getCats(){
    global $con;
    $get_cats = "select * from categories";
    $run_cats = mysqli_query($con, $get_cats);
    while ($row_cats=mysqli_fetch_array($run_cats)){
        $cats[]=$row_cats;
    }
  return $cats;
}

这篇关于如何从echo(functions.php)中取出锚元素并将其放到body(index.php)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆