Jump to content

Multiple Level Array


Adamhumbug

Recommended Posts

Hi All,

I have a function that creates an array using a few sql quereys.  I then use the array to output to html.  I am wanting to add more data to the array to populate another column in the created table.

I am unsure how to add $miqty to the array and then output it in the Qty On Order column. 

I feel like i need to add the [] to

$menuItemsInCat[$mcatname][$miid] = $miname;

section but i am not sure where in there it should be.

 

<?php if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['user_id'])){
	header("location: index.php");
	exit;
}
//name of the pagea
$_SESSION['this_page'] = 'new-menu';

function getMenuItems($conn){
	$output = '';
	$stmt = $conn -> query("
	                         SELECT menu_category_name
	                         FROM ssm_menu_items
	                         INNER JOIN  ssm_menu_category on menu_item_category_id = menu_category_id
	                         ORDER BY menu_category_display_order
	                         ");
	//create an empty array of all of the menu categories that are in use
	foreach ($stmt as $item){
		$menuItemsInCat[$item['menu_category_name']] = [];
	}
	$stmt = $conn -> prepare("
	                         	SELECT menu_item_id, menu_item_name, menu_category_name, sum(menu_item_qty)
								FROM ssm_menu_items mi
								INNER JOIN ssm_menu_category mcat ON mi.menu_item_category_id = mcat.menu_category_id
								left join ssm_menu_order USING (menu_item_id)
								GROUP BY menu_item_id
	                         ");
	$stmt -> execute();
	$stmt -> bind_result($miid, $miname, $mcatname, $miqty);
	while ($row = $stmt -> fetch()) {
		//put items into the blank array created above under their correct category
		
		$menuItemsInCat[$mcatname][$miid] = $miname;
	}

	echo "<pre>";
	print_r($menuItemsInCat);
	echo "</pre>";

		//foreach thing in $menuItemInCat array there is $menucat array associated with $menuit(ems) array
		//we want the menu cat
	foreach ($menuItemsInCat as $menucat => $menuit) {
		$output .= "<tbody>";
		$output .= "<tr class='bg-secondary text-white text-center'><th>$menucat</th>";
		$output .= "<th>Qty On Order</th><th>Manage</th></tr>";
		//foreach thing in menu items array there is an array of ids and an array of items
		foreach ($menuit as $itemId => $itemName) {
			$output .= "<tr><td>$itemName</td>";
			$output .= "<td></td>";
			$output .= "<td><div class='btn btn-primary'>Manage</div></td></tr>";
		}
		$output .= "</tbody>";
	}
	return $output;
}



?>
<?php include '_includes/head.php'; ?>
<div class="container-fluid">
	<div class="row">
		<?php include '_includes/header.php'; ?>
	</div>
	<div class="row" >
		<div class="col-sm-2 p-0 bg-dark text-light">
			<?php include '_includes/nav.php'; ?>
		</div>
		<div class="col-sm-10" style="height: calc(100vh - 80px);overflow:scroll;">
			<div class="mt-3">
				<table class="table table-striped table-hover table-bordered text-center align-middle">
					<?= getMenuItems($conn) ?>
				</table>
			</div>
			<div class="col-sm-12"><?php include '_includes/footer.php'; ?></div>
		</div>
	</div>
	
</div>
<script>
	//set sidebar active indicator
	//XX = name of parent if in dropdown eg "sheet"
	if(document.getElementById('menu')){
		document.getElementById('menu').classList.add('show')
	}
	//nav button ID
	if(document.getElementById('newMenu')){
		document.getElementById('newMenu').classList.add('blOrange')
	}
</script>

As always your help is very appreciated.

Edited by Adamhumbug
Link to comment
Share on other sites

Turns out it wasnt as complicated as i thought

 

function getMenuItems($conn){
	$output = '';
	$stmt = $conn -> query("
	                         SELECT menu_category_name
	                         FROM ssm_menu_items
	                         INNER JOIN  ssm_menu_category on menu_item_category_id = menu_category_id
	                         ORDER BY menu_category_display_order
	                         ");
	//create an empty array of all of the menu categories that are in use
	foreach ($stmt as $item){
		$menuItemsInCat[$item['menu_category_name']] = [];
	}
	$stmt = $conn -> prepare("
	                         	SELECT menu_item_id, menu_item_name, menu_category_name, sum(menu_item_qty)
								FROM ssm_menu_items mi
								INNER JOIN ssm_menu_category mcat ON mi.menu_item_category_id = mcat.menu_category_id
								left join ssm_menu_order USING (menu_item_id)
								GROUP BY menu_item_id
	                         ");
	$stmt -> execute();
	$stmt -> bind_result($miid, $miname, $mcatname, $miqty);
	while ($row = $stmt -> fetch()) {
		//put items into the blank array created above under their correct category
		
		$menuItemsInCat[$mcatname][$miid] = [$miname, $miqty];
	}
		//foreach thing in $menuItemInCat array there is $menucat array associated with $menuit(ems) array
		//we want the menu cat
	foreach ($menuItemsInCat as $menucat => $menuit) {
		$output .= "<tbody>";
		$output .= "<tr class='bg-secondary text-white text-center'><th>$menucat</th>";
		$output .= "<th>Qty On Order</th><th>Manage</th></tr>";
		//foreach thing in menu items array there is an array of ids and an array of items
		foreach ($menuit as $itemId => $itemName) {
			$output .= "<tr><td>$itemName[0]</td>";
			$output .= "<td>$itemName[1]</td>";
			$output .= "<td><div class='btn btn-primary'>Manage</div></td></tr>";
		}
		$output .= "</tbody>";
	}
	return $output;
}

Look at me solving my own problems

Edited by Adamhumbug
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.