Jump to content

Help looping through an array


Recommended Posts

Hi everyone,

 

I have successfully tested the following code:

<?php
		foreach ($machine as $k=>$v){

			if ($k == "MachineName"){	$unit = $v; }
			if ($k == "Outputs"){
				foreach($v as $outputs)	{
					foreach($outputs as $k=>$v)	{

						if ($k == "Widgets"){

								$totalwidget = $v;

						}elseif(is_array($v)){

								foreach ($v as $kk=>$vv){
									if ($kk == "Widgets") $totalwidgets = $vv;
								}
						}
						}
					}

				}
				if ($k == "Capabilities"){
				foreach($v as $capabilities){
					foreach($capabilities as $k=>$v){

						if ($k == "Widgets"){

								$potentialwidgets = $v;

						}elseif(is_array($v)){

								foreach ($v as $kk=>$vv){
									if ($kk == "Widgets") $potentialwidgets = $vv;
								}

						}
					}

				}
				}
				}

			if ($totalwidgets != 0)	
				$capabilityfactor = $totalwidgets/$potentialwidgets;
			else
				$capabilityfactor = 0;

 

It now outputs 100 or so lines, representing the total number of machines in the plant. Each line contains four items, as follows:

 

Machine1, [a number representing Machine1's $totalwidgets in that hour], [a number representing Machine1's $potentialwidgets in that hour], [a number representing Machine1's $capabilitfactor in that hour]

 

i.e., the first item in each line is a particular machine; the next three items are that machine's performance numbers in that hour -- output, capability, capability factor.

 

I now want to put each line into its own separate database table. i.e., I'll have 100 or so separate tables, each representing one machine. That way I can track each machine's performance hour after hour.

 

I figure I'll need to build 100 or so individual queries to insert the contents of each line into the corresponding table.

 

So my question: what loop statement(s) should I use to accomplish this?

 

Thanks in advance!

Link to comment
Share on other sites

I now want to put each line into its own separate database table. i.e., I'll have 100 or so separate tables, each representing one machine. That way I can track each machine's performance hour after hour.

Not sensible

You want a single table with the machine ID and date/hour as a composite primary key. The whole point of databases is that you don't split what should be a single table into multiple tables

Link to comment
Share on other sites

Thanks Mark,

 

I have considered this but can't see an alternative. I have 100 machines of various makes and models, many running continuously almost all the time. I want to be able to report each machine's output/capability, to be able to report total output by machine type (make, model, etc.) as well as by other system metrics.

 

I have one table that has the make, model, etc. of each machine (i.e., 100 records). I have another table that records hourly performance (this contains the date/time key you suggest).

 

But I can't see how to be able to report machine-level info unless I (1) create one table for each machine, or (2) create one table for each hour.

 

The first alternative seems the best; it's big but at least it's finite.

Link to comment
Share on other sites

But I can't see how to be able to report machine-level info unless I (1) create one table for each machine, or (2) create one table for each hour.

 

You need to read into Relation Database Management Systems before you continue or take a SQL Class/read RDMS books.  As you obviously do not have the experience with such.

 

It will make your life so much easier understanding that, as stated 100 tables is ludacris and completely defeats the purpose of a database. A table can have as many rows as you want, so you setup the table with the common items needed, machine name, make, model etc. Then if you want to "log" information setup another table "computer logs" which olds the machine id for the first table so they link then you write a query to pull up this information.

 

I explained it rough and brief because yea, I do not have the time to fully explain relational databases especially when there are books/classes out there that do it a ton better than I can.

Link to comment
Share on other sites

Promiso,

 

You may be right, and I won't argue. Regardless of which database, I still have to figure out the loop problem. i.e., I still have to pick through the results of the initial loop (described above) and insert each machine's hourly numbers into a table. Any suggestions?

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.