Hi everyone,
Here I have a php code, and i want to understand one part of it - like what is actually going on over there.

(Based on Model View Controller(MVC) format)

This is my 'model/category_db.php' - file
It connects to the database and has some functions that have a couple of queries

<?php

$db = mysql_connect("localhost","romiaujla","password") or die ("could not connect to mysql");
mysql_select_db("ShoppersMart", $db);

function get_categories() 
{
    global $db;
    $query = 'SELECT *
              FROM categories
              ORDER BY categoryID'; 

    $result = $db->($query);

    return $result; 
}

function get_category_name($category_id) 
{ 
    global $db;
    $query = 'SELECT *
              FROM categories
              WHERE categoryID = $category_id'; 

    $category = $db->($query);
    $category = $category->fetch(); 
    $category_name = $category['categoryName']; 

    return $category_name;
}

?>

Here Next is the 'controller/index.php' file

<?php

//require("../model/database.php");
require("../model/category_db.php");
require("../model/product_db.php");

if(isset($_POST['action']))
{
    $action = $_POST['action'];
}
else if(isset($_POST['action']))
{
    $action = $_GET['actions'];
}
else
{
    $action = 'list_products';
}

if($action == 'list_products')
{
    $category_id = $_GET['categoryID'];
    if(isset($category_id))
    {
        $category_id = 1;
    }

    $category_name = get_category_name($category_id);
    $categories = $get_categories();

    include("../view/product_list.php");
}

?>

Third is the view/product_list.php

<?php include'header.php' ?>

<div id="main">
<h1>Product List</h1>
<div id="sidebar">
    <h2>Categories</h2>
    <ul class="nav">
        <?php foreach($categories as $category): ?>
            <li>
                <a href="?category_id = <?php echo $category['categoryID']; ?>">
                    <?php echo $category['categoryName']?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
</div>

<?php include'footer.php' ?>

Now I would really appreciate if anybody could tell me what the following peice of code in the 'product_list.php' file does

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

Somewhat i understand that this will repeat and print categoryName's in the category database, but i just dont know how it does that.. Some explanation would help my head understand this and thanks in advance.

Recommended Answers

All 3 Replies

this code list cateries, with link
when user clicks that link, then new page will open shwoing the prodcuts of the clicked categories

Yup even i understood that, but thing is like from where does it pick up "$categories"
from where does it get access to category table??

         <?php foreach($categories as $category): ?>

Ok, is it the same $categories assigned a value in the index.php?? If it is, then can we use :

 <?php echo $category_name; ?> 

instead of

<?php echo $category['categoryName']?>
$categories = $get_categories();
include("../view/product_list.php");


you can see two lines in index.php
here you set categories first and then include productList.php, 
so here you using categories array in productlist.
if you run http://servername/view/product_list.php directly in browser then it will not display anything
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.