1

I am learning PHP from from Murach's book and I am facing some issues to understand this code,

<nav>
  <ul>
     <?php foreach ($categories as $category) : ?>
     <li>
        <a href="?category_id=<?php echo $category['categoryID']; ?>">
                    <?php echo $category['categoryName']; ?>
        </a>
     </li>
     <?php endforeach; ?>
  </ul>
</nav>

My problem is with this part,

<a href="?category_id=<?php echo $category['categoryID']; ?>">
     <?php echo $category['categoryName']; ?>
</a>

All this is trying to do is to display the category list. So each time user clicks this pass the category ID.Why this is written like that ? why the question mark with category_id (?category_id)in the beginning ? is that a variable and assigning it with remaining code ? and why is an echo statement for categoryID in anchor tag ? Because in the webpage it displays only the categoryName. I am just beginner so please excuse me if it sounds so stupid.

4
  • it appears what your doing is effectively refreshing the page with the categoryID as a $_GET variable (php.net/manual/en/reserved.variables.get.php). Your assumption is correct; the ? denotes that there is variables to be found. Multiple can be added like so example.com?foo=bar&bar=foo Commented Jan 7, 2016 at 6:32
  • 2
    Please learn HTML first, then learn what is GET and POST methods, you will get all your answers. Commented Jan 7, 2016 at 6:32
  • href does not show up in viewport directly. You can only see it if you view the page source. Commented Jan 7, 2016 at 6:32
  • Thanks Joshua that was my confusion. Commented Jan 7, 2016 at 6:50

5 Answers 5

1
<a href="?category_id=<?php echo $category['categoryID']; ?>">
     <?php echo $category['categoryName']; ?>
</a>

So each time user clicks this pass the category ID.Why this is written like that ?

When user clicks on category, it should display only the category which is clicked by users. i.e. By taking that category id from url it query the database and fetch the category as

select * from category where category_id='$category_id'

and show users the category on which the users click

why the question mark with category_id ? is that a variable and assigning it with remaining code ? and why is an echo statement for categoryID in anchor tag ?

The question mark is there because the question mark introduces the "query string" part of a URL.

The echo statement echo the category id which is the query string for that url and the name is displayed because the name of each category is echoed in the href tag.So, it is echoed each time as

<a href="?category_id=1234">Watch</a>

when user click on this link, user will get the page with a single category and the url will be

your_url?category_id=1234
Sign up to request clarification or add additional context in comments.

Comments

1

Basically in this chunk of code it displays category list.It start with a for each loop to check how many categoires it has

<a href="?category_id=<?php echo $category['categoryID']; ?>">
                    <?php echo $category['categoryName']; ?>

for every category it makes anchor link with two parameter category id and name.In php we use echo to display view as it is server side for rendering view we use echo.In url address bar you have category id and in page you have category Name

Comments

1

Your answers:

1) Why this is written like that:

-- Because, HTML and PHP can be embedded in each other, here we are embedding PHP in HTML.

2) why the question mark with category_id (?category_id)in the beginning ?

-- Because, we are passing some data over url, therefore, query string is needed.

-- We can get the data passed over url (query string) as $_GET in respective page. Current page (self) in our case.

3) and why is an echo statement for categoryID in anchor tag ?

-- Because, HTML and PHP can be embedded in each other, here we are embedding PHP in HTML to print category id.

Comments

1

in this code

<a href="?category_id=<?php echo $category['categoryID']; ?>">
                <?php echo $category['categoryName']; ?>

Basically ? is refered to the query string through which we can send the particular categoryId to another page also and can use it there.

main purpose of query string is to transfer the value to another page through URL and can be used there, like we do from the session.

your code will display all the categories and when you will hover on that category , it will show you the corresponding category id which is assigned to it.

Comments

1

The goal of those lines is to produce something like:

<a href="?category_id=1234">Men's Suits</a>

The question mark is there because the question mark introduces the "query string" part of a URL (in this case the rest of the URL is implied as the current page).

In HTML, the "tags" which look like this:

<tagname>
<tagname attribute="value"...>
</tagname>

are, in a sense, instructions to the web browser on how to format the visible elements of the page (text, images, etc). You don't see the tags themselves, you see their effect.

The <a>...</a> tags produce an anchor, a clickable link, but what you see is what appears between the <a> and the </a> -- in my example the words Men's Suits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.