Title1

Title2

Title3

6-6 課後 練習參考

  1.  引入config.php (資料庫連線)


     
  2. 在nav.php 增加 資料庫查詢的程式碼



     
  3. config.php 資料庫連線資料

  4. nav.php 程式碼

    <?php
    /*
    require_once("nav.php");
    */
    
    #查詢語法
    
    /*
      選擇所有欄位
      從 prod_kind 資料表
      在 kind = 'nav_home' 且 enable = '1'
      排序 sort 欄位 由小到大
    */
    $sql="select *
          from `prod_kind`
          where `kind`='nav_home' and `enable`='1'
          order by `sort`";
    
    #送出查詢,回傳查到的資源
    $result = mysql_query($sql) or die(mysql_error());//die($sql);
    
    #定義 變數 $nav_li
    $nav_li="";
    #開始撈資料
    while($row = mysql_fetch_array($result))
    {
      #三元運算
      $row['target'] = $row['target'] ?" target='_blank'":"";
      #串接字串
      $nav_li .= "<li><a href='{$row['url']}' {$row['target']}>{$row['title']}</a></li>\n";
    }
    
    $nav="
      <!--導覽列 start-->
      <nav class='navbar navbar-default' role='navigation'>
        <div class='container'>
          <!-- Brand and toggle get grouped for better mobile display -->
          <div class='navbar-header'>
            <button type='button' class='navbar-toggle collapsed' data-toggle='collapse' data-target='#bs-example-navbar-collapse-1'>
              <span class='sr-only'>Toggle navigation</span>
              <span class='icon-bar'></span>
              <span class='icon-bar'></span>
              <span class='icon-bar'></span>
            </button>
            <a class='navbar-brand' href='index.php'>育將電腦工作室</a>
          </div>
          <!-- Collect the nav links, forms, and other content for toggling -->
          <div class='collapse navbar-collapse' id='bs-example-navbar-collapse-1'>
            <ul class='nav navbar-nav navbar-right'>
              <!--帶入變數-->
              {$nav_li}
            </ul>
          </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
      </nav><!-- /.nav -->";