PHP Mini Project for Beginners
PHP Mini Project for Beginners
Here is a small PHP Project, which will displays a list of items from the selected category.
This project gives you how to use a multidimensional array, function, and switch statement in
Real-time scenario.
Description:
There is only one PHP file called main.php. Which implements above concepts in this project.
1. Nested array : array is a multidimensional array contains different category of products
2. Function : There is a function called display(). This function takes products array as
an argument and display all the products from the given array.
3. Switch statement : Identifies the category and calls display() function and passes the
products array(nested array key element which points to another array of products.
And some CSS Code
main.php
<html> <head> <title>PHP Mini Project</title> <style type="text/css"> body{ align: center; margin: 50px; } li{ background-color : #f1f15f; } div{ background-color : #8FDD84; height : 800 px; width : 50%; padding: 25px 50px 75px 100px; border: 1px solid #000000; } h1{ text-align : center; } </style> </head> <body> <?php $title = ""; // products Array
$store = array( "Electronics" => array("Fridge", "AC", "Fan", "Telivision"), "Furniture" => array("Sofa", "Study Table", "Shelf", "Chairs") , "Groceries" => array("Sugar", "Dal", "Oil", "Rice","Atta") ); // function with array argument
function display($products) { print "<div><h1>ABC Store</h1><ol>"; global $title; echo "<br /><b>Category:</b>". $title; echo "<br /><br />"; foreach ($products as $p) { print "<li>".$p."</li><br />"; } print "</ol></div>"; print "</br>"; } if(isset($_POST['submit'])) { $category = $_POST['category']; switch($category){ case 1 : { $title ="Electronics"; display($store["Electronics"] ); break; } case 2 : { $title ="Groceries"; display($store["Groceries"] ); break; } case 3 : { $title ="Furniture"; display($store["Furniture"] ); break; } default : echo "No category "; } } ?> <form id="categoryform" action="" method="post"> <select name="category"> <option value="">Select Category...</option> <option value="1">Electronics</option> <option value="2">Groceries</option> <option value="3">Furniture</option> </select> <input type="submit" value="Go" name="submit" /> </form> </body> </html>
Comments
Post a Comment