Bazaar – php example code – part 3 – Manage category script
Article focus on way how we populate category/ subcategory for selling items. Management page contains listing of scucessfully added fileds with removal link as option. Our project will lead from category, trough item, simple login functionality into a editprofile option with more robust user login.
Discuss managecategory.php functionality
Our managecategory.php script must enable these functionalities:
create subcategory of item with existing main category
if we creating subcategory with non existing man category, we must be able to add them along
after all succesfully added category, list of existing category/ subcategory must be displayed with link for removal appropriate category row
after preparing parts for login verification, these scripts can be viable only for user with admin role assigned by page admin
Visual content of this page along with removecategory.php script follow:
Implementation of expected functionality
Our page consist from parts for obtain user category and subcategory names. Error messaging, reporting of successfull category addition into bazaar_category table are next parts of our script.
. $row[‚category‘] . ‚&subcategory=‘. $row[‚subcategory‘] .'“> >> Remove </a></td></tr>‘; //construction of GETable link
// for removecategory.php input
echo „</tr>“;
echo “ </div> “ ;
}
echo „</table>“;
// Free result set
mysqli_free_result($output);
} else{
echo „There is no benchmark result in chart. Please wirite one.“; // if no records in table
}
} else{
echo „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc); // if database query problem
}
// Close connection
mysqli_close($dbc);
?>
removecategory.php script
Remove category script obtain data from GET associative array ofered from url snipet added to name of invoked page. After reading data, cerate removal confirmation form.
After selecting yes and submitting form, appropriate category/ subcategory row is removed with approrpiate sql query.
<?php // leading part of page for simple header securing and basic variable setup
require_once(‚appvars.php‘); // including variables for database
session_start(); // start the session – must be added on all pages for session variable accessing – further description will be available in appropriate article in a row of articles focused on bazaar app
// solution using SESSIONS with COOKIES for longer (30days) login persistency
if(!isset($_SESSION[‚users_id‘])) { // if session is no more active
if(isset($_COOKIE[‚users_id‘]) && isset($_COOKIE[‚username‘])) { // but cookie is set then renew session variables along them
$_SESSION[‚users_id‘] = $_COOKIE[‚users_id‘];
$_SESSION[‚username‘] = $_COOKIE[‚username‘];
}
}
// two variables for message and styling of the mesage with bootstrap
} else if (isset($_POST[‚subcategory_id‘]) && isset($_POST[‚category‘]) && isset($_POST[‚subcategory‘])) { //grab score from POST – different behavior for removal
echo ‚<form method=“POST“ action=“removecategory.php“>‚; //not self but direct this script removecategory.php – we dont want include any GET data tahat previously send
echo ‚<p> <a href = „managecategory.php“> << Back to category management page. </a></p>‘;
?>
</div>
<div class=“footer“>
<a class=“navbar-brand“ href=“https://cdesigner.eu“> Visit us on CDesigner.eu </a>
</div>
</body>
</html>
Conclusion and other thoughts
Our code snipets create baseline for required functionality for category management. From final code was omited parts related to user login and loged users persisency. Full bazaar app code can be obtaned from github here.