{"id":13328,"date":"2022-10-03T18:47:58","date_gmt":"2022-10-03T13:17:58","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/"},"modified":"2022-10-03T18:47:58","modified_gmt":"2022-10-03T13:17:58","slug":"solved-can-someone-explain-me-the-below-code","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/","title":{"rendered":"[Solved] Can Someone Explain me the below Code?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-37525807\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"37525807\" data-parentid=\"37524529\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>He created the Order.php to keep the class elements external from the main code. This is cleaner code and easier to maintain.<\/p>\n<p><em>and how can i store the Refrence of $order with the Object?<\/em> You are already storing this in <code>$newOrders<\/code>?<\/p>\n<p>Added comments to each line for main.php<\/p>\n<pre><code>&lt;?php\n\/\/ these includes are just bringing in code from external files. Nothing much really to say here.\ninclude(\"Order.php\");\ninclude(\"connect.php\");\n\n\/\/$query is just setting up the SQL query to the database for selecting all columns from a table called orders.\n$query=\"SELECT * FROM `orders`\";\n\n\/\/$filter_Results is just saving the query in a variable, this is used later on to fetch the data in a while loop\n$filter_Result=mysqli_query($con,$query);\n\n\/\/$newOrders and $items = array() is just pre-defining these variables as arrays.\n$newOrders=Array();\n$items = array();     \n\n\/\/As said before, we need to use a while loop to extract the data fetched from sql where `mysqli_fetch_array` is the method of retrieving the data.\nwhile($row=mysqli_fetch_array($filter_Result))\n{\n    $order; \/\/Not sure what the next line is doing.. `$order;` doesn't do anything..\n    $orderId= $row['id']; \/\/Saving the column name `id` as a variable $orderID\n    echo \"hello\".$orderId; \/\/echo out the hello# where # is the orderID\n\n    \/\/This is checking if the orderID retrieved from sql has already been placed inside the $newOrders array returning true or false.\n    if(in_array($orderId,$newOrders,true)){\n\n        \/\/Some more code needs to be added here, I'm guessing you need to add in something to find the object relating to the `orderID` that already exists in `$newOrders`\n        $order=&lt;get order object from $newOrders for which id is $orderId&gt;\n    }\n\n    \/\/ If $orderID not in $newOrders\n    else{\n        \/\/ Create a new instance of Order class called $order\n        $order=new Order($row['id'], $row['tableId'], $row['createdDate']);\n\n        \/\/Add this order to the array $newOrders\n        array_push($newOrders,$order);\n    }\n    \/\/ Create a new instance of the Item class called $item takinging in the columns ProductId, ProductName, Quantity\n    $item=new Item($row['ProductId'], $row['ProductName'], $row['Quantity']);\n\n    \/\/ Using a method from orders called AddItem (this can be found in Order.php under the order class\n    $Order.AddItem($item);\n}\n\n\/\/ Looping through each order inside $newOrders (although this seems wrong, should be foreach($newOrders as $order)\nforeach($order as $newOrders)\n{\n\/\/create box\n}\n\n\/ Finally including some more code inside the Modal.php file\ninclude(\"Modal.php\");\n?&gt;\n<\/code><\/pre>\n<p>Order.php<\/p>\n<pre><code>&lt;?php\n\n\/\/ Class called Order\n class Order {\n\n  \/\/ properties of the class.\n  var $orderId;\n  var $orderTime;\n  var $tableNumber;\n  var $items = array();   \n\n  \/\/ Function inside the method which fills the properties upon creating a new instance the class `$order=new Order($row['id'], $row['tableId'], $row['createdDate']);` \n  function __Order($orderId, $orderTime, $tableNumber)\n  {\n      \/\/ Using the parameters passed to the function to fill the properties of the class\n      $this-&gt;$orderId = $orderId;\n      $this-&gt;$orderTime = $orderTime;\n      $this-&gt;$tableNumber = $tableNumber;\n  }   \n\n  \/\/Function called AddItem which takes parameters and fills an items array however this should be using $this-&gt;\n  function AddItem($itemId, $itemName, $quantity, $personalization)   \n  {\n       $item = new Item($itemId, $itemName, $quantity, $personalization);\n      $items[] = $item;\n  }\n}\n\n\/\/ New class called Item\nclass Item {\n\n    \/\/ properties of the class.\n    var $productId;\n    var $productName;\n    var $quantity;\n    var $personalization;\n\n    \/\/ Same as above: Function inside the method which fills the properties upon creating a new instance the class\n    function __Item($productId, $productName, $quantity, $personalization)\n    {\n        $this-&gt;$productId = $productId;\n        $this-&gt;$productName = $productName;\n        $this-&gt;$quantity = $quantity;\n        $this-&gt;$personalization = $personalization;\n    }\n}\n?&gt;\n<\/code><\/pre>\n<p>To answer your question:<\/p>\n<ol>\n<li>\n<p>An array of objects just means you are creating a new instance of the object and then saving the object in an array. You are already doing this with your <code>$newOrders<\/code> array. You have created a new <code>$order<\/code> (the object) and then saved it in the array <code>$newOrders<\/code> using: <code>array_push($newOrders,$order);<\/code><\/p>\n<\/li>\n<li>\n<p>Not really sure what you are asking for here? Where has this code come from? Is this a tutorial of some sort?<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Can Someone Explain me the below Code? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] He created the Order.php to keep the class elements external from the main code. This is cleaner code and easier to maintain. and how can i store the Refrence of $order with the Object? You are already storing this in $newOrders? Added comments to each line for main.php &lt;?php \/\/ these includes are just &#8230; <a title=\"[Solved] Can Someone Explain me the below Code?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\" aria-label=\"More on [Solved] Can Someone Explain me the below Code?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[340,1117,339],"class_list":["post-13328","post","type-post","status-publish","format-standard","hentry","category-solved","tag-mysql","tag-mysqli","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Can Someone Explain me the below Code? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Can Someone Explain me the below Code? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] He created the Order.php to keep the class elements external from the main code. This is cleaner code and easier to maintain. and how can i store the Refrence of $order with the Object? You are already storing this in $newOrders? Added comments to each line for main.php &lt;?php \/\/ these includes are just ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-03T13:17:58+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Can Someone Explain me the below Code?\",\"datePublished\":\"2022-10-03T13:17:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\"},\"wordCount\":141,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"mysql\",\"mysqli\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\",\"name\":\"[Solved] Can Someone Explain me the below Code? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-03T13:17:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Can Someone Explain me the below Code?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Can Someone Explain me the below Code? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Can Someone Explain me the below Code? - JassWeb","og_description":"[ad_1] He created the Order.php to keep the class elements external from the main code. This is cleaner code and easier to maintain. and how can i store the Refrence of $order with the Object? You are already storing this in $newOrders? Added comments to each line for main.php &lt;?php \/\/ these includes are just ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/","og_site_name":"JassWeb","article_published_time":"2022-10-03T13:17:58+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Can Someone Explain me the below Code?","datePublished":"2022-10-03T13:17:58+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/"},"wordCount":141,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["mysql","mysqli","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/","url":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/","name":"[Solved] Can Someone Explain me the below Code? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-03T13:17:58+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-can-someone-explain-me-the-below-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Can Someone Explain me the below Code?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=13328"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13328\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=13328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=13328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=13328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}