How to find appropriate data from the first table and put it in the suitable position in the second table?

Hello everyone,
I have some code like this:

     $query = "SELECT M.BEGINNING_INVENTORY AS BEGINNING_INVENTORY, 
    S.MATERIAL_CODE  AS MATERIAL_CODE, S.MATERIAL_NAME AS MATERIAL_NAME, 
    SUM(S.IMPORTATION_QUANTITY) AS SUM_IMPORTATION, 
    SUM(S.EXPORTATION_QUANTITY) AS SUM_EXPORTATION  FROM STOCK_INFORMATION AS 
    S LEFT JOIN MATERIALS AS M ON M.MATERIAL_CODE = S.MATERIAL_CODE WHERE 
    S.MATERIAL_NAME != ''  GROUP BY M.MATERIAL_NAME ";
    $results2 = mysqli_query($db, $query);

	<thead>
	<tr>
	<th>Material Code</th>
	<th>Material Name</th>
	<th>Beginning Inventory</th>

	<th>Sum(Importation)</th>
	<th> Sum(Exportation)</th>
	<th>Remainder</th>
	</tr>
	</thead>
<tr>

	     <?php 

	if(isset($results2) ) {
		while($row1 = mysqli_fetch_array($results2)) {
        global $db;		
	?>
	<td><?php echo $material_code1 = ($row1['MATERIAL_CODE']); ?></td>
	<td><?php echo $material_name1 = ($row1['MATERIAL_NAME']); ?></td>
	<td><?php echo $beginning_inventory = ($row1['BEGINNING_INVENTORY']); ?></td>
	<td><?php echo $sum_importation = ($row1['SUM_IMPORTATION']); ?></td>
	<td><?php echo $sum_exportation = ($row1['SUM_EXPORTATION']); ?></td>
	<td><?php echo $net = (($beginning_inventory + $sum_importation) - ($sum_exportation)); ?></td>
	</tr>
	<?php }} ?>
	</table>	 

Also, in this file I have another code like this:

     $search_query = "SELECT SUM(RESULT) AS FINAL, MATERIAL_NAME, UNIT, MATERIAL_TYPE 
     FROM(
    SELECT P.PRODUCT_NAME AS PRODUCT_NAME, SUM((B.ACTUAL_QUANTITY * 
    P.PRODUCT_QUANTITY)) AS RESULT
    ,B.MATERIAL_NAME AS MATERIAL_NAME,B.ACTUAL_QUANTITY, B.PRODUCT_CODE AS 
    PRODUCT_CODE,B.UNIT AS UNIT ,B.MATERIAL_TYPE,B.BOM_NAME FROM BOM AS B   LEFT 
    JOIN PRODUCTION_PLAN AS P ON P.PRODUCT_CODE = B.PRODUCT_CODE AND 
    P.BOM_NAME = B.BOM_NAME WHERE MATERIAL_NAME <> ''  GROUP BY MATERIAL_NAME, 
      PRODUCT_CODE ) AS RESULT GROUP BY MATERIAL_NAME ";
       $results = mysqli_query($db, $search_query);

	<thead>
	<tr>
	<th>Material Name</th>
	<th>Requirement </th>
	<th>Inventory</th>
	</tr>
	</thead>
	<tr>
	<?php 	if(isset($results) ) {
			while(($row = mysqli_fetch_array($results))) {

     global $db;		
	  ?>
  
	      <td><?php echo $material_name = ($row['MATERIAL_NAME']); ?></td>
     	<td><?php echo  number_format($row['FINAL'],0); ?></td>
	<td><?php
	global $material_name1;
	global $net;
	if($material_name == $material_name1){
		 echo  $net;	} ?>
     </td>
     </tr>
     <?php }} ?>
	
   </table>

I want to have the remainder ($net) from the first table in front of every material_name from the second table. (It must be compatible with material_name). How to do it? Now, I have just one of them.

Thanks in advance

Regards

Well, I am not sure about the code in the first one, you assign variables inside an echo when you already have them in a $row1 array. Waste of time on the server. Just use the already obtained version.
Also, you could just create the $net right inside the query and just display that from the $row array.

Not sure what you mean by put the first table in front of? Do you mean you want one query that creates both of them in one and displays all of the data from both tables in one? Or do you mean you just want to keep the data from the first query to use in the second display?

1 Like

Thanks for getting back to me
To be honest, the first table in php(not Mysql), should return the remainder of every material. So it should be appeared in the second table of php.
I do not know how to get $net and put in the second table. ( for example, the value of salt for $net must be put in front of salt record in the second table)
No, I do not want to left join 2 table. Because, maybe in future,I will get the $net from other application via API
Thanks in advance

Your reply did not show to us, please repost it…

1 Like

Well, I am still not sure of the logic of this. If you have two routines and the first creates a result, you normally can recreate it in another query joining the two together.

But, if you want to just β€œmemorize” the data, that is easy enough to do. You can create an array, let’s call it $net_array for example. Then, just save the results in that. Or, just keep the results array from the first query. Then, for a normal array like $net_array, you would save as the index and net as the value. So, in your first query, inside the loop, just save it something like:
$net_array["$row[β€˜MATERIAL_NAME’]"]=(($beginning_inventory + $sum_importation) - ($sum_exportation));
This would be easy to pass on to a later function or to another page using SESSION array. Or, if you want to just use the already created previous results from the first query, you can just pass that array onto the next section and do a reverse-search for the record that is needed. You just need to know the layout of the result from the first query to create a search so that you can get to the correct data needed.
To know the layout of an array you can test it with this process:
$temp = print_r($row1, 1);
$echo ("< pre>".$temp."< /pre>");
What this will do is print the array in a format that you can see how the indexing of the output from the query. You probably can just access it like this
echo $row1[β€˜BEGINNING_INVENTORY’] + $row1[β€˜SUM_IMPORTATION’] - $row1[β€˜SUM_EXPORTATION’];
Since plus and minus is accumulative, you do not need the parens around them ( ( ))…
If this is what you mean, you can just keep the $row1 array. It is just an array, so it can be used in another page using SESSION array or you could save it somewhere else if needed.

Not sure if this answers your question or not. Hope it does.

1 Like

Dear Ernie, Thanks for your kind reply
Your code can return my targeted data, but I have another problem.
I use this code at two separated part of a PHP file. When I use your code in the first code position( where is the first table), it returns right values.
But when I use this code in the second table, it returns just value 0.

 $search_query = "SELECT SUM(RESULT) AS FINAL, MATERIAL_NAME, UNIT, MATERIAL_TYPE 
     FROM(
    SELECT P.PRODUCT_NAME AS PRODUCT_NAME, SUM((B.ACTUAL_QUANTITY * 
    P.PRODUCT_QUANTITY)) AS RESULT
    ,B.MATERIAL_NAME AS MATERIAL_NAME,B.ACTUAL_QUANTITY, B.PRODUCT_CODE AS 
    PRODUCT_CODE,B.UNIT AS UNIT ,B.MATERIAL_TYPE,B.BOM_NAME FROM BOM AS B   LEFT 
    JOIN PRODUCTION_PLAN AS P ON P.PRODUCT_CODE = B.PRODUCT_CODE AND 
    P.BOM_NAME = B.BOM_NAME WHERE MATERIAL_NAME <> ''  GROUP BY MATERIAL_NAME, 
      PRODUCT_CODE ) AS RESULT GROUP BY MATERIAL_NAME ";
       $results = mysqli_query($db, $search_query);

	<thead>
	<tr>
	<th>Material Name</th>
	<th>Requirement </th>
	<th>Inventory</th>
	</tr>
	</thead>
	<tr>
	<?php 	if(isset($results) ) {
			while(($row = mysqli_fetch_array($results))) {

     global $db;		
	  ?>
  
	      <td><?php echo $material_name = ($row['MATERIAL_NAME']); ?></td>
     	<td><?php echo  number_format($row['FINAL'],0); ?></td>
	<td><?php
         global $row1;
	echo $row1['BEGINNING_INVENTORY'] + $row1['SUM_IMPORTATION'] - $row1['SUM_EXPORTATION'];?>
     </td>
     </tr>
     <?php }} ?>
	
   </table>

I do not know how to put the corresponding value($row1[β€˜BEGINNING_INVENTORY’] + $row1[β€˜SUM_IMPORTATION’] - $row1[β€˜SUM_EXPORTATION’]) at

Inventory?
Thanks in advance.
Best Regards

Well, if the code works correctly at one place and not a second place, then the inputs may not be correct.
If you mean you need to change the query to use something else, then I need more info…

I mean, you said the query works in the first place and the same code does not in the second place.
What is different in the second place with the same query? OR, did you mean that you altered the query
and it does not work? Thanks…

1 Like

Dear Ernie,
Again thank you for your support.
I’m nearly sure, I did not manage to explain whatever I wanted. Hence, I have to draw my requirement from the first(maybe you have a better solution)
There are two different queris(you can see them in the first post of the topic).
First query returns remainder of materials. (This is for warehouse)
Second query returns the consumption of materials based on their recipe(BOM).
So,I want to compare consumption material(second query) with remainder($net) from the first query.
For instance, now there are 2000kg sugar in warehouse. (This number is reported by the first query). And we need to produce 1500 kg sugar. (This(1500) is reported by the second query).
But, user must compare Inventory(2000) with sugar requirement(1500).
I want to that 2000 ( first query) is put in the second table and in front of the same material(sugar).
And the point is that 2 queries are right. But I dont prefer to use left join technic. Because probably I will get the $net from other application via API.
As a result, I can guess I need to get value from the first Query in an array. But I dont know how to do it.
I was wondering if you could support me with this.
Very best regards,

Thank you for the description. I understand better now. I have to leave for a few hours soon.
When I get back I will create a new query for you. Just short on time, lots of work and have to leave
for at least 6 hours… Talk to you later! Sorry for the delay, just very busy today! …

1 Like

I finally got some free time and was looking at this last query problem. I noticed it was throwing out an error because of a table named β€œproduction_plan”. I have tables bom and production_data, but, no production_plan. Was that supposed to be _data or if there is a third table, let me know.

1 Like

If I change it to _data, then it runs and gives me results.

1 Like

Dear Ernie,
Thank you very much for your kind reply,
Well, production_plan is different from production_data. However, there is no error in my both queries.
As I said, I can guess I need to get value($net) from the first Query in an array and put it in the second table based on the material_name. I don’t know how to do it?
If you want to have the production_plan, I can send it here.
Best Regards

Well, I can not run queries against it without some of the data. I would not need the entire table.
If you want to private-message it to me, I will add it to the database and do some testing for you.
Also, my private messages do not keep the ones I sent to you. Can you post the fixed first query
from early on where I combined the code all into the query? That would help me too. Thanks.

1 Like

Thank you very much for your consideration.
I think I lost one of your previous message about my query problem.
Anyway, would you please read again the 7th reply by me. (explanation about the whole story of my goal).
To be honest the first query is not important for me.Even it is better not to show for clients. But, I do not know how to get ($net).
My issue is this. " I want to have update inventory($net) in order to compare it with its consumption formulation ".
The second query returns materials consumption formulations.but, it can not return the update inventory($net). So, I think I can get that by the first query.
The problem is that, I don’t know how to populate $net objects so that its objects find corresponding materials. For example, the value for milk should be put in front of milk. the salt should be put in front of salt.
As I said, I prefer not to use join Technic. because, maybe I will get the $net via rest API.

And, my data is fake and they are just for testing.
Here is the code:

REATE TABLE production_plan (
id int(11) NOT NULL,
product_code text NOT NULL,
product_name text NOT NULL,
product_entrance text NOT NULL,
unit_entrance text NOT NULL,
product_quantity text NOT NULL,
unit text NOT NULL,
others text NOT NULL,
product_line text NOT NULL,
week_day text NOT NULL,
pdate text NOT NULL,
shift text NOT NULL,
start_time text NOT NULL,
finish_time text NOT NULL,
duration text NOT NULL,
description text NOT NULL,
username text NOT NULL,
first_date text NOT NULL,
editor text NOT NULL,
edit_date text NOT NULL,
bom_name text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

–
– Dumping data for table production_plan

INSERT INTO production_plan (id, product_code, product_name, product_entrance, unit_entrance, product_quantity, unit, others, product_line, week_day, pdate, shift, start_time, finish_time, duration, description, username, first_date, editor, edit_date, bom_name) VALUES
(1, β€˜533’, β€˜milk’, β€˜150’, β€˜ton’, β€˜150000’, β€˜bottle’, β€˜-’, β€˜sidel’, β€˜β€™, β€˜2020/3/21’, β€˜2’, β€˜16:30’, β€˜22:30’, β€˜06:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜2020/07/03’, β€˜1’),
(2, β€˜251’, β€˜doogh’, β€˜150’, β€˜ton’, β€˜85000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/21’, β€˜2’, β€˜17:30’, β€˜22:30’, β€˜05:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜2020/07/05’, β€˜default’),
(3, β€˜251’, β€˜doogh’, β€˜β€™, β€˜β€™, β€˜136000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/21’, β€˜3’, β€˜22:30’, β€˜06:30’, β€˜08:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜2020/07/05’, β€˜default’),
(4, β€˜582’, β€˜milk’, β€˜β€™, β€˜β€™, β€˜144000’, β€˜bottle’, β€˜-’, β€˜sidel’, β€˜β€™, β€˜2020/3/22’, β€˜1’, β€˜06:30’, β€˜12:30’, β€˜06:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(5, β€˜581’, β€˜milk’, β€˜β€™, β€˜β€™, β€˜144000’, β€˜bottle’, β€˜-’, β€˜sidel’, β€˜β€™, β€˜2020/3/22’, β€˜2’, β€˜14:30’, β€˜20:30’, β€˜06:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(6, β€˜441’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜88000’, β€˜bottle’, β€˜-’, β€˜N2’, β€˜β€™, β€˜2020/3/22’, β€˜2’, β€˜18:30’, β€˜22:30’, β€˜04:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(7, β€˜441’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜176000’, β€˜bottle’, β€˜-’, β€˜N2’, β€˜β€™, β€˜2020/3/22’, β€˜3’, β€˜22:30’, β€˜06:30’, β€˜08:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(8, β€˜254’, β€˜doogh’, β€˜β€™, β€˜β€™, β€˜68000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/22’, β€˜3’, β€˜22:30’, β€˜02:30’, β€˜04:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(9, β€˜252’, β€˜doogh’, β€˜β€™, β€˜β€™, β€˜68000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/22’, β€˜3’, β€˜02:30’, β€˜06:30’, β€˜04:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜2020/07/05’, β€˜default’),
(10, β€˜251’, β€˜doogh’, β€˜β€™, β€˜β€™, β€˜136000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/22’, β€˜1’, β€˜06:30’, β€˜14:30’, β€˜08:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜2020/07/05’, β€˜default’),
(11, β€˜251’, β€˜doogh’, β€˜β€™, β€˜β€™, β€˜51000’, β€˜bottle’, β€˜-’, β€˜serak’, β€˜β€™, β€˜2020/3/22’, β€˜2’, β€˜14:03’, β€˜17:30’, β€˜03:27:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(12, β€˜123’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜60000’, β€˜bottle’, β€˜-’, β€˜Cheshme’, β€˜β€™, β€˜2020/3/22’, β€˜2’, β€˜16:30’, β€˜22:30’, β€˜06:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(13, β€˜123’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜60000’, β€˜bottle’, β€˜-’, β€˜Cheshme’, β€˜β€™, β€˜2020/3/22’, β€˜3’, β€˜22:30’, β€˜04:30’, β€˜06:00:00’, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™, β€˜β€™),
(14, β€˜-’, β€˜-’, β€˜β€™, β€˜β€™, β€˜-’, β€˜-’, β€˜CIP’, β€˜sidel’, β€˜β€™, β€˜2020/5/23’, β€˜1’, β€˜14:30’, β€˜18:30’, β€˜04:00:00’, β€˜β€™, β€˜β€™, β€˜2020/06/17’, β€˜β€™, β€˜β€™, β€˜β€™),
(15, β€˜-’, β€˜-’, β€˜β€™, β€˜β€™, β€˜-’, β€˜-’, β€˜CIP’, β€˜sidel’, β€˜β€™, β€˜2020/5/23’, β€˜1’, β€˜14:30’, β€˜18:30’, β€˜04:00:00’, β€˜β€™, β€˜β€™, β€˜2020/06/17’, β€˜β€™, β€˜2020/06/17’, β€˜β€™),
(16, β€˜101’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜31000’, β€˜bottle’, β€˜-’, β€˜N1’, β€˜β€™, β€˜2020/7/4’, β€˜1’, β€˜06:30’, β€˜18:30’, β€˜12:00:00’, β€˜β€™, β€˜β€™, β€˜2020/07/03’, β€˜β€™, β€˜β€™, β€˜1’),
(17, β€˜121’, β€˜soft-drink’, β€˜β€™, β€˜β€™, β€˜200000’, β€˜bottle’, β€˜-’, β€˜Cheshme’, β€˜β€™, β€˜2020/7/25’, β€˜1’, β€˜07:30’, β€˜14:30’, β€˜07:00:00’, β€˜β€™, β€˜β€™, β€˜2020/07/25’, β€˜β€™, β€˜β€™, β€˜β€™);

Very best regards.

Well, with that data in place, I ran your last posted query now and it works. I get this as a result:

FINAL MATERIAL_NAME UNIT MATERIAL_TYPE
1785000 hashtgiah gram consumable
13940 label gram packaging
591150000 milk gram consumable
26775000 nana gram consumable
425000 preform unit packaging
150000 priform unit packaging
26775000 salt gram consumable
76500000 water gram consumable

Now, I see you used some sort of Global’s in your code for $db and $row1. Can you explain these to me?
Is this code because you have more than one function and need to use the data inside a function?
I am unclear why you need to use globals at all.

1 Like

Thank you,
World should be happy because of you. You present your valuable time for developing knowledge. I really adore you.
yes. Global is used for that reason.
db is the same my connection to database which exists in another php.file.
and in this table(you posted here), I want to have $net i in front of material_name at the new column (Inventory). It should be corresponding with material_name.
How can I do that?
Kind Regards

Well, I would put it right into the query. Since you did it in an earlier query, you can just add that too.
I will play with it and post a solution for you.

And, you are very welcome. I like to help people. I am not the best at queries and join’s, but, think this is
a simple one. More in a bit…

1 Like

I went back to your first query. You gave me the following tables:
bom
production_data
production_plan
stock_information

But, in the first query it uses a table called material. I can’t run a query against that as I do not have it.
Didn’t I send you an updated version of the first two, I think during a private message? I do not have those. So, either send me the materials table here or in a private message or I can just guess how it
might work.

1 Like

Dear Enrie,
thanks again
here is the material table code:

CREATE TABLE materials (

id int(11) NOT NULL,

material_code text NOT NULL,

material_name text NOT NULL,

material_group text NOT NULL,

material_packing text NOT NULL,

order_point text NOT NULL,

beginning_inventory text NOT NULL,

company text NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

–

– Dumping data for table materials

–

INSERT INTO materials (id, material_code, material_name, material_group, material_packing, order_point, beginning_inventory, company) VALUES

(3, β€˜1’, β€˜milk’, β€˜consumable’, β€˜kg’, β€˜1000’, β€˜10’, β€˜β€™),

(4, β€˜2’, β€˜sugar’, β€˜consumable’, β€˜kg’, β€˜5’, β€˜220’, β€˜β€™),

(5, β€˜3’, β€˜nana’, β€˜consumable’, β€˜kg’, β€˜200’, β€˜850’, β€˜β€™),

(6, β€˜10’, β€˜label’, β€˜packing’, β€˜unit’, β€˜1000’, β€˜10’, β€˜β€™),

(7, β€˜4’, β€˜hashtgiah’, β€˜consumable’, β€˜kg’, β€˜100’, β€˜350’, β€˜β€™),

(8, β€˜5’, β€˜salt’, β€˜consumable’, β€˜kg’, β€˜3000’, β€˜5000’, β€˜β€™);

Regards

Thanks. I will get a new query figured out for you this morning…

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service