简体   繁体   中英

How do I pull variables from my Database?

I have a menu of Home About Blog Templates Contact.

There is a table in my database called 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.

What I want is to be able to click on each category and it display a specific.php file I have.

for instance if I click home it will display my index.php

clicking about displays my about.php

clicking blog displays my blog.php file etc.

I am not sure how to go about this?

I am following a tutorial on Udemy as a guideline but, I need further assistance.

This is the php code at the top of my header.php file

<?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.

However, when I click the about blog templates and contact categories it will only display the contact.php file for each category I have.

Thus, when I click About Blog Templates Contact only the Contact.php file displays.

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. about.php

else if categories is = blog then display 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

There no data stored in the table. All the table structure is ID & Text. The Text is being displayed as the menu and each Text has its associated ID. To be clear the HOME button is apart of the menu but, its not in the category table. I hope this is enough information.

Where you have this in the loop's else block:

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

Note the href has contact.php for all links.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM