Jump to content

Recommended Posts

Hey guys:

 

I have a database that is full of items. Each item lives in a box, with various accessories. I'm trying to loop through this DB 

to print out a label for each case that shows the amount of items that are in the case, along with the accessories that belong to it.

 

I currently have a rather convoluted bit of code, that kind of works, but doesn't have the order that I want.

 

My result is currently this:

4 X Generic lighting hanging clamp
4 X Generic lighting hanging clamp
4 X Generic lighting hanging clamp
4 X Generic lighting hanging clamp
4 X Generic lighting hanging clamp
2 X 25kg Safety Bond
2 X 25kg Safety Bond
2 X 25kg Safety Bond
2 X 25kg Safety Bond
2 X 25kg Safety Bond
2 X 16a to Powercon
2 X 16a to Powercon
2 X 16a to Powercon
2 X 16a to Powercon
2 X 16a to Powercon
**********
12 X Generic lighting hanging clamp

6 X 25kg Safety Bond
**********

All the information is there, but due to the way that it loops through it's in the wrong order. I'd like it to be grouped so it looks like this so that it is grouped by box:

4 X Generic lighting hanging clamp
2 X 25kg Safety Bond
2 X 16a to Powercon

4 X Generic lighting hanging clamp
2 X 25kg Safety Bond
4 X Generic lighting hanging clamp
2 X 16a to Powercon

4 X Generic lighting hanging clamp
2 X 25kg Safety Bond
2 X 16a to Powercon

4 X Generic lighting hanging clamp
2 X 25kg Safety Bond
2 X 16a to Powercon

**********
12 X Generic lighting hanging clamp

6 X 25kg Safety Bond
**********

Here is the code that I'm using. At the moment I'm just concentrating on the if ($item_type == '2') part, but have shown the whole lot so you can see what I'm doing.

foreach ($distinct_path as $path) {
$sql = "SELECT * FROM current_items WHERE path = '$path'";
$result = $con->query($sql);

if ($result->num_rows > 0)
{
	// output data of each row
	while($row = $result->fetch_assoc()) {
		$item_name = $row['item_name'];
		$item_type = $row['item_type'];
		$item_qty = $row['qty'];
		$case_qty = $row['case_qty'];
		
		
		
		
		if ($item_type == '1') 	// THESE ARE MAIN ITEMS 
								{ 
			$total_main_qty = $row['qty'];  //main item QTY;
			$main_item_name = $item_name;
				
								}
								
								// This code will tell us number of full boxes, and number of extra lights left over
								$amtoffull = $total_main_qty/$case_qty;
								$amtoffull = floor($amtoffull); // we round down to the nearest whole number
								$total_left = $total_main_qty - ($case_qty * $amtoffull);
								
								
		if ($item_type == '2') // THESE ARE ACCESSORIES
								{
									
									$asc_qty = $row['qty'];  //main item QTY;
									$asc_qty = $asc_qty/$total_main_qty;  // THIS GIVES US THE TOTAL OF ONE Item			
									$asc_qty = $asc_qty * $case_qty; // This is a full case amount

									//echo $asc_qty ." x ". $item_name ."</p>";
									
								while ($i < $amtoffull)
								{
									echo $asc_qty ." X ". $item_name ."</p>";
									++$i;
								}
								}
								
		$i = 0;
	}
	echo "**********";
	echo "<p>";
	
}

I think I need to either shift the loops around so they work in a different order, or store everything into an array and then loop through that. Can any one help?

Link to comment
https://forums.phpfreaks.com/topic/302261-php-sorting-help/
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.