【问题标题】:What is the most efficient way to use sidebar menu?使用侧边栏菜单最有效的方法是什么?
【发布时间】:2017-12-14 18:43:59
【问题描述】:

因此,如果我必须制作像学校网站这样的侧边栏菜单,在页面加载后会突出显示活动点击的链接。它使用单个页面,例如

<a target="_top" href="default.asp">HTML HOME</a>
<a target="_top" href="html_intro.asp">HTML Introduction</a>
<a target="_top" href="html_editors.asp">HTML Editors</a>
<a target="_top" href="html_basic.asp">HTML Basic</a>
<a target="_top" href="html_elements.asp">HTML Elements</a>

但是我自己构建的那个是用 PHP 创建的,所以菜单项的标题或名称被更改为小写并保存在数据库中的同一行中,称为类,用于活动链接突出显示。所以点击侧边栏链接时会发生什么只是摘要div内容的内容 (在侧边栏右侧)发生更改,而侧边栏和导航栏保持不变。所以它停留在同一页面上并从数据库中检索数据并放入摘要 div 中。

<body class='noPadding'>
     <div class='bigNav'>             <!-- top navbar -->
          <img src="images/logo2.png"/>
     </div>

     <?php
     if(isset($_GET['subject'])){
         $subject=$_GET['subject'];


     ?>
     <div class='container'>
        <div class='leftLayout' style='padding-top:70px;background-color:#EBEBEB'>
            <ul class="fullFit">
              <?php
                 $query="SELECT * FROM `study` WHERE `subject`='$subject'";
                 $run=mysqli_query($conn,$query);
                 while($row=mysqli_fetch_array($run)){
                       $topic=$row['topic'];
                       $class=$row['class'];  ?>

                             <li  class="<?php echo $class?>">
                               <a href='<?php echo $sitename ?>/web.php?subject=<?php echo $subject?>&topic=<?php echo $class?>'>
                                   <?php echo $topic?>
                               </a>
                             </li>

                  <?php  }  ?>

            </ul>
        </div>

     <?php
     if(isset($_GET['topic'])) {
         $active_id=$_GET['topic'];
         $topic=$_GET['topic'];
         $query="SELECT summary FROM `study` WHERE `class`='$active_id'";
         $run=mysqli_query($conn,$query);
         $row=mysqli_fetch_array($run);
         $summary=$row[0];
      ?>
        <div class='summary'> <?php echo $summary ?> </div>
      <?php } ?>


     </div>
  <script>

      $(document).ready(function(){
        $(".<?php echo $active_id ?>").addClass("current");
        });

  </script>

<?php  }  ?>

</body> 

而且,如果我必须像包括 W3Schools 在内的许多网站那样制作单个页面,我是否必须每次都创建一个新文件并在每个文件中包含导航栏和侧边栏(每个链接上应该保持相同) ? 他们是如何管理的?

【问题讨论】:

  • 我推荐includingrequiring 文件。例如,导航中的“HTML 编辑器”可能链接到index.php?page=editors。在您的index.php 文件中,包含一个基于page 变量的editors.php 内容文件。这样您就不需要在每个文件中重复主要站点结构(标题、导航等)。您可能会发现这篇文章内容丰富:Organize Your Next PHP Project the Right Way
  • 我所描述的被称为"front controller" pattern。根据项目的复杂性,为每个页面创建一个单独的文件(intro.phpeditors.php 等)并在每个文件中包含一个通用的navigation.php 可能更简单或更有效。另请参阅这篇文章中的示例:simple php websites structure
  • 感谢您的评论。所以我现在应该制作单独的文件并在每个文件中包含标题吗?
  • 你能告诉我为什么不推荐我这样做的原因吗?谢谢
  • 这是一个简单的例子。这个问题可能过于宽泛或基于观点而无法在此处有效回答。你能告诉我们更多关于你网站内容的信息吗?您是否将每个主要部分(介绍、编辑、基本)称为“主题”?每个主题是否包含多个主题?每个主题都有一个摘要?

标签: javascript php jquery html css


【解决方案1】:

我希望这与您当前的结构没有太大的不同。
我不打算把你带进兔子洞。

话虽如此,听起来您可以将所有内容都编码到一个 PHP 文件中。请注意,这只是众多方法中的一种,高度依赖于您的项目范围和个人编码风格。

数据库

主题

`id`
`name`

主题

`id`
`subject_id`
`name`
`summary`

PHP

<?php

// define database functions

function getAllSubjects($conn) {
  $sql="SELECT * FROM `subject` WHERE 1;";
  $q=$conn->query($sql);
  return $q; 
}

function getSubject($conn,$subject_id=false) {
  $r=false;
  if (is_numeric($subject_id)) {
    $sql = "SELECT * FROM `subject` WHERE `id`=?;";
    $q = $conn->prepare($sql);
    $q->bind_param('i',$subject_id);
    $q->execute();
    $r=$q->fetch_assoc();
  }
  return $r;
}

function getSubjectTopics($conn,$subject_id=false) {
  $q=false;
  if (is_numeric($subject_id)) {
    $sql = "SELECT * FROM `topic` WHERE `subject_id`=?;";
    $q = $conn->prepare($sql);
    $q->bind_param('i',$subject_id);
    $q->execute();
  }
  return $q;
}

function getTopic($conn,$topic_id=false) {
  $r=false;
  if (is_numeric($topic_id)) {
    $sql = "SELECT * FROM `topic` WHERE `id`=?;";
    $q = $conn->prepare($sql);
    $q->bind_param('i',$topic_id);
    $q->execute();
    $r=$q->fetch_assoc();
  }
  return $r;
}


// get URL parameters

$subject_id = !empty($_GET['s']) && is_numeric($_GET['s'])
  ? $_GET['s']
  : false;

$topic_id = !empty($_GET['t']) && is_numeric($_GET['t'])
  ? $_GET['t']
  : false;


// fetch all subjects from database
$subjectsQ=getAllSubjects($conn);

// fetch current subject
$thisSubject = $subject_id
  ? getSubject($conn,$subject_id)
  : false;

// fetch current subject topics
$thisSubjectTopicsQ = !empty($thisSubject)
  ? getSubjectTopics($conn,$subject_id)
  : false;

// fetch current topic
$thisTopic = $topic_id
  ? getTopic($conn,$topic_id)
  : false;


// display subject navigation
while($subject = $subjectsQ->fetch_assoc()){

  // determine if this subject is selected
  $sel = !empty($thisSubject) && $thisSubject['id']==$subject['id']
    ? true
    : false;

  // define the URL
  $url = '?s='.$subject['id'];

  // display nav item for this subject
  echo '<a href="' . $url . '"'
         . ($sel?' class="sel"' : '')
         . '>' . $subject['name']
         . '</a>';

}

// if there are subject topics to display...
if ($thisSubjectTopicsQ && $thisSubjectTopicsQ->num_rows > 0) {

  // loop through this subject's topics
  while($topic=$thisSubjectTopicsQ->fetch_assoc()){

    // determine if this topic is selected
    $sel = !empty($thisTopic) && $thisTopic['id']==$topic['id']
      ? true
      : false;

    // define the URL
    $url = '?s='.$thisSubject['id'].'&t='.$topic['id'];

    // display nav item for this topic
    // (alternate way to display the link)
    ?><a href="<?=$url?>"<?=$sel?' class="selected"':''?>><?php
      echo $topic['name'];
    ?></a><?php

  }

}

// if there is a topic to display
if (!empty($thisTopic)) {
  // display topic summary
  echo $thisTopic['summary'];
}

这是一个基本的例子。当然,它不包含任何 HTML 结构。

也可以连接这两个表,这样您就不需要做太多的提取了。例如,获取一个主题也可以返回它的主题:

$sql="SELECT s.`id` AS `subject_id,
             s.`name` AS `subject_name`,
             t.`id` AS `topic_id,
             t.`name` AS `topic_name`,
             t.`summary` AS `topic_summary`
      FROM `topic` t
      LEFT JOIN `subject` s ON (s.`id`=t.`subject_id`)
      WHERE t.`id`=?";

【讨论】:

  • 投反对票是因为我不应该尝试回答如此广泛的问题吗?还是因为我没有回答有关如何组织文件的问题?还是别的什么?
猜你喜欢
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2019-11-20
  • 2017-05-29
  • 2014-03-26
  • 2016-04-13
相关资源
最近更新 更多