简体   繁体   English

如何从我的数据库中提取变量?

[英]How do I pull variables from my Database?

I have a menu of Home About Blog Templates Contact.我有一个 Home About Blog Templates Contact 菜单。

There is a table in my database called Categories.我的数据库中有一个名为 Categories 的表。

The items in my table are Home About Blog Templates Contact.我表中的项目是主页关于博客模板联系人。

These five items are being pulled from my database by my header.php file for my website.我的网站的 header.php 文件从我的数据库中提取了这五个项目。

What I want is to be able to click on each category and it display a specific.php file I have.我想要的是能够单击每个类别并显示我拥有的特定.php 文件。

for instance if I click home it will display my index.php例如,如果我单击主页,它将显示我的 index.php

clicking about displays my about.php单击关于显示我的 about.php

clicking blog displays my blog.php file etc.单击博客会显示我的博客。php 文件等。

I am not sure how to go about this?我不确定如何 go 关于这个?

I am following a tutorial on Udemy as a guideline but, I need further assistance.我正在遵循 Udemy 的教程作为指导,但我需要进一步的帮助。

This is the php code at the top of my header.php file这是 header.php 文件顶部的 php 代码

<?php

     include("includes/config.php");
     include("includes/db.php");
     
       $query = "SELECT * FROM categories";
       
       $categories = $db->query($query);   
     
?>

This is the code that is pulling my menu from the database这是从数据库中提取我的菜单的代码

<?php if(isset($_GET['category']) || strpos($_SERVER['REQUEST_URI'] , "index.php") === false) { ?>
          <a class="" href="index.php">Home</a>
        <?php }else { ?>
        <a class="" href="index.php">Home</a>
        <?php } ?>
          
          <?php if($categories->num_rows > 0) {
          while($row = $categories->fetch_assoc()){
              if(isset($_GET['category']) && $row['id'] == $_GET['category'] ){ ?>
          <a class="" href="index.php?category=<?php echo $row['id']; ?>"><?php echo $row['text']; ?></a>
              <?php }  else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";
           } }?>
          
      

Essentially, what is going on in this code is when I click home it displays my index.php file and the code in my index.php file display code from another table I have.本质上,此代码中发生的事情是,当我单击主页时,它会显示我的 index.php 文件和我的 index.php 文件中的代码显示来自另一个表的代码。

However, when I click the about blog templates and contact categories it will only display the contact.php file for each category I have.但是,当我单击关于博客模板和联系人类别时,它只会显示我拥有的每个类别的 contact.php 文件。

Thus, when I click About Blog Templates Contact only the Contact.php file displays.因此,当我单击关于博客模板联系人时,只会显示 Contact.php 文件。

I need a starting point on how to build on this where I can tell the code if about, blog, templates, contact is click on then display it's respective file.我需要一个关于如何在此基础上进行构建的起点,我可以告诉代码是否点击了关于、博客、模板、联系人,然后显示它的相应文件。

The question is how do I use the categories in my database as my variables.问题是如何使用数据库中的类别作为变量。

I was thinking using else if statements such as else if categories is = about then display.我在考虑使用 else if 语句,例如 else if categories is = about then display。 about.php关于.php

else if categories is = blog then display blog.php否则,如果类别是 = 博客,则显示 blog.php

If there are any free resources or books I can use to figure this out it would be much appreciated.如果有任何免费资源或书籍我可以用来解决这个问题,将不胜感激。

EDIT编辑

The Table has only is called categories ID Text 1 About 2 Blog 3 Templates 4 Contact该表仅称为类别 ID 文本 1 关于 2 博客 3 模板 4 联系人

There no data stored in the table.表中没有存储数据。 All the table structure is ID & Text.所有的表结构都是 ID 和文本。 The Text is being displayed as the menu and each Text has its associated ID.文本显示为菜单,每个文本都有其关联的 ID。 To be clear the HOME button is apart of the menu but, its not in the category table.需要明确的是,HOME 按钮是菜单的一部分,但不在类别表中。 I hope this is enough information.我希望这是足够的信息。

Where you have this in the loop's else block:你在循环的 else 块中有这个:

}  else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";

Note the href has contact.php for all links.请注意,所有链接的 href 都有contact.php

What I believe that your trying to achieve, based on the categories, is:相信您根据类别尝试实现的是:

}  else echo '<a class="" href="' . $row['text'] . '.php?category=' . $row['id'] . '">' . $row['text'] . '</a>';

...so that you set the link's file name to match the category name in the link? ...以便您将链接的文件名设置为与链接中的类别名称匹配?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从MySQL数据库中提取数据以在页面加载时预先填充表单? - How do I pull data from my MySQL database to pre populate a form on page load? 如何使用php函数从数据库中提取数据 - How do I pull data from the my database using a php function 如何从已登录用户的数据库中提取数据? - How do I pull data from a database from a logged in user? 如何从SQL数据库[PHP]中提取某些值? - How do I pull the value of something from an SQL database [PHP]? 如何使用PDO将数据库中的字段转换为变量 - How do I turn fields from my database into variables using PDO 如何从其他网站提取数据并将其存储在本地数据库中 - How can I pull data from other websites and get it stored in my local database php / sql-如何在变量上使用LIKE运算符从数据库中提取结果? - php/sql- How do I use LIKE operator on variable to pull results from database? 如何为数据库中的每个视频创建单独的目录并从URL中提取其ID? - How do I create a separate directory for each video in a database and pull its ID from the URL? 如何根据下拉菜单结果从数据库中提取信息 - How do I pull information from a database depending on a drop-down menu result 如何从MySQL数据库中提取商品的两个类别AND标签? - How do I pull both categories AND tags from a MySQL Database for an item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM