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.