eldan88 Posted October 27, 2012 Share Posted October 27, 2012 Hey, I'm a little new to PHP and started developing a basic PHP website through video tutorials, to become proficient at PHP. Below are just categories and items. The items are being displayed below the corresponding categories. Below I am echoing out the ID number when a category or item is being clicked on. That code that GETs the item number is all the way on top. My question is, on the video tutorial, it suggested that when a category is clicked on ($sel_category) set the item ($sel_items) to NULL and when a item is selected set the category to NULL. What would be the purpose behind this? <?php require_once("includes/connection.php"); ?> <?php if(isset($_GET['category_id'])) // This grabs the category_id from the URL from $categories_fetch_array below. {$sel_category = $_GET['category_id']; // If it is set then $sel_category will be assigned to the category ID $sel_items = NULL;}// If somone click on a category then $sel_items will be set to NULL elseif (isset($_GET['item_id'])) // This grabs the item_id number from the $items_fetch_array below. {$sel_items = $_GET['item_id']; // Then the ID gets assigned to the vairbale $sel_items $sel_category = NULL;} // If $sel_items get selected then $sel_category get set to NULL else { //Set both pages to NULL if either get selected. $sel_items = NULL; $sel_category = NULL; } // Here is where we will the cataegories by ID //First we write the query statement ?> <?php // Return data from the DB $category_query = "SELECT * FROM catagories "; $category_result = mysql_query($category_query,$connection); if(!$category_result) { die("mySQL DB read failed" . mysql_error()); } //use the return data while($categories_fetch_array = mysql_fetch_array($category_result)) { echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here $items_query = "SELECT * FROM items WHERE category_id = {$categories_fetch_array["id"]}"; $items_result = mysql_query($items_query, $connection); if (!$items_result) { die("mysql could not make a query to the items table" . mysql_error()); }// end of if (!$items_result) { while ($items_fetch_array = mysql_fetch_array($items_result)) { // Links get outputed here echo "<b>Items:</b>" . "<a href=\"content.php?item_id=" . urlencode($items_fetch_array['id']) . "\">" . $items_fetch_array['item_name'] . "</a>" . "<br />"; } }// End of while statment?> <p> <?php echo $sel_category;// This varible $_GET['category_id'] echo $sel_items; // This varibale echo's out the $_GET['item_name'] ?> <?php mysql_close($connection); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269987-question-about-setting-a-variable-to-null/ Share on other sites More sharing options...
jcbones Posted October 28, 2012 Share Posted October 28, 2012 This is so you do not get warnings about undeclared variables. You could just set both variables to null at the very top of your script, then populate them as needed. That would remove the 'else' clause on the first if/elseif/else statement. I will give this as an example so you can see it. I also took the liberty to point out some other items as well. NOT TESTED! <?php require_once("includes/connection.php"); //include connection. $sel_category = null; //declare variable. $sel_items = null; //declare variable. if(isset($_GET['category_id'])) // This grabs the category_id from the URL from $categories_fetch_array below. { $sel_category = $_GET['category_id']; // If it is set then $sel_category will be assigned to the category ID } elseif (isset($_GET['item_id'])) // This grabs the item_id number from the $items_fetch_array below. { $sel_items = $_GET['item_id']; // Then the ID gets assigned to the vairbale $sel_items } //Here is where we will the cataegories by ID //First we write the query statement // Return data from the DB //Running queries in loops is a really bad idea. Most of the programmers around here will try to steer you clear //of this bad practice. It leads to horribly inefficent code, and loads of problems with data collision. //So, I urge you strongly to learn about MySQL JOIN syntax, and relational database design. $category_query = "SELECT c.category_name, c.id AS category_id, i.id AS item_id, i.item_name FROM catagories AS c JOIN items AS i ON c.id = i.category_id"; $category_result = mysql_query($category_query,$connection); //run the query. if(!$category_result) { die("mySQL DB read failed" . mysql_error()); //error handling. } $cat = array(); //array to hold all categories processed. //use the return data while($categories_fetch_array = mysql_fetch_assoc($category_result)) { //I don't suggest using fetch_array(), use either fetch_row(), or fetch_assoc(). fetch_array() just uses more overhead. //using some logic, we can find out if we already printed the categories to the window. if(!in_array($categories_fetch_array['category_name'],$cat)) { //if the category name is NOT found in the $cat array, then run this block of code. echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['category_id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here $cat[] = $categories_fetch_array['category_name']; //add the category name to the $cat array. } if(!empty($categories_fetch_array['item_name'])) { //if the item name is NOT empty, then run this block of code. // Links get outputed here echo "<b>Items:</b>" . "<a href=\"content.php?item_id=" . urlencode($categories_fetch_array['item_id']) . "\">" . $categories_fetch_array['item_name'] . "</a>" . "<br />"; } }// End of while statment ?> <p> <?php echo $sel_category;// This varible $_GET['category_id'] echo $sel_items; // This varibale echo's out the $_GET['item_name'] mysql_close($connection); //this function is only needed if you desire to change the mysql connection, or the script is so memory intensive that you wish to clear some room. The connection will be closed when the script ends. ?> Quote Link to comment https://forums.phpfreaks.com/topic/269987-question-about-setting-a-variable-to-null/#findComment-1388212 Share on other sites More sharing options...
eldan88 Posted October 28, 2012 Author Share Posted October 28, 2012 (edited) jcbones, Thank you for steering me in the right direction of using PHP. I have a couple questions based on the code you re wrote.. 1) Where can I learn more about mysql JOIN syntax and relational database design 2) Do you not recommend using mysql_fetch_array() on the code above or just not using it in general 3) For the code below. Why would you set a condition to check if the $categories_fetch_array['category_name'] is not in the $cat array? and lastly why would you assign them to an array after the categories have been outputed? if(!in_array($categories_fetch_array['category_name'],$cat)) { // Why check if the category is not already in the array? echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['category_id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here $cat[] = $categories_fetch_array['category_name']; //Why assign the categoties to this array? mysql_close($connection);// I heard its a good progamming habit to get into to always close the mysql_connection even though PHP will do it for you.. This is so you do not get warnings about undeclared variables. You could just set both variables to null at the very top of your script, then populate them as needed. That would remove the 'else' clause on the first if/elseif/else statement. I will give this as an example so you can see it. I also took the liberty to point out some other items as well. NOT TESTED! <?php require_once("includes/connection.php"); //include connection. $sel_category = null; //declare variable. $sel_items = null; //declare variable. if(isset($_GET['category_id'])) // This grabs the category_id from the URL from $categories_fetch_array below. { $sel_category = $_GET['category_id']; // If it is set then $sel_category will be assigned to the category ID } elseif (isset($_GET['item_id'])) // This grabs the item_id number from the $items_fetch_array below. { $sel_items = $_GET['item_id']; // Then the ID gets assigned to the vairbale $sel_items } //Here is where we will the cataegories by ID //First we write the query statement // Return data from the DB //Running queries in loops is a really bad idea. Most of the programmers around here will try to steer you clear //of this bad practice. It leads to horribly inefficent code, and loads of problems with data collision. //So, I urge you strongly to learn about MySQL JOIN syntax, and relational database design. $category_query = "SELECT c.category_name, c.id AS category_id, i.id AS item_id, i.item_name FROM catagories AS c JOIN items AS i ON c.id = i.category_id"; $category_result = mysql_query($category_query,$connection); //run the query. if(!$category_result) { die("mySQL DB read failed" . mysql_error()); //error handling. } $cat = array(); //array to hold all categories processed. //use the return data while($categories_fetch_array = mysql_fetch_assoc($category_result)) { //I don't suggest using fetch_array(), use either fetch_row(), or fetch_assoc(). fetch_array() just uses more overhead. //using some logic, we can find out if we already printed the categories to the window. if(!in_array($categories_fetch_array['category_name'],$cat)) { //if the category name is NOT found in the $cat array, then run this block of code. echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['category_id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here $cat[] = $categories_fetch_array['category_name']; //add the category name to the $cat array. } if(!empty($categories_fetch_array['item_name'])) { //if the item name is NOT empty, then run this block of code. // Links get outputed here echo "<b>Items:</b>" . "<a href=\"content.php?item_id=" . urlencode($categories_fetch_array['item_id']) . "\">" . $categories_fetch_array['item_name'] . "</a>" . "<br />"; } }// End of while statment ?> <p> <?php echo $sel_category;// This varible $_GET['category_id'] echo $sel_items; // This varibale echo's out the $_GET['item_name'] mysql_close($connection); //this function is only needed if you desire to change the mysql connection, or the script is so memory intensive that you wish to clear some room. The connection will be closed when the script ends. ?> Edited October 28, 2012 by eldan88 Quote Link to comment https://forums.phpfreaks.com/topic/269987-question-about-setting-a-variable-to-null/#findComment-1388217 Share on other sites More sharing options...
jcbones Posted October 28, 2012 Share Posted October 28, 2012 1. For relational database design, I suggest watch . It shouldn't take long to watch them, but they will help you in future projects. For JOIN syntax, look at MySQL Manual, although it may seem a little confusing at first, this is the most valuable site for MySQL syntax, and how it works. 2. I don't recommend mysql_fetch_array for anything beyond very special cases. The reason is that it returns redundant data, both associative and numerical array indexes. Since you are only using an associative array, I changed that to mysql_fetch_assoc. 3. The reason we check if the category name is NOT in the cat array, is that it will only be there if we have echo'd it. As we put it in the array right after we do. When you join the tables for a query, the category name will appear for each and every item in the items table. So since you don't want the category name to appear more than once, we echo it, then prevent it from echo'ing (by making sure it is not in the $cat array) until it changes. Hope that helps you understand a little bit more. Quote Link to comment https://forums.phpfreaks.com/topic/269987-question-about-setting-a-variable-to-null/#findComment-1388279 Share on other sites More sharing options...
eldan88 Posted October 29, 2012 Author Share Posted October 29, 2012 Okay I am starting to get a more clear understanding of what you just explained. 1) For the JOIN syntax, do I use PHP to accomplish this, or do I have to change the way the tables are set up in phpmyadmin 2) Why did't you set up a logical condition to see if the items are empty in the array and if empty echo them. Is it because you already joined the tables together? 1. For relational database design, I suggest watch . It shouldn't take long to watch them, but they will help you in future projects. For JOIN syntax, look at MySQL Manual, although it may seem a little confusing at first, this is the most valuable site for MySQL syntax, and how it works. 2. I don't recommend mysql_fetch_array for anything beyond very special cases. The reason is that it returns redundant data, both associative and numerical array indexes. Since you are only using an associative array, I changed that to mysql_fetch_assoc. 3. The reason we check if the category name is NOT in the cat array, is that it will only be there if we have echo'd it. As we put it in the array right after we do. When you join the tables for a query, the category name will appear for each and every item in the items table. So since you don't want the category name to appear more than once, we echo it, then prevent it from echo'ing (by making sure it is not in the $cat array) until it changes. Hope that helps you understand a little bit more. Quote Link to comment https://forums.phpfreaks.com/topic/269987-question-about-setting-a-variable-to-null/#findComment-1388555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.