stevieontario Posted May 6, 2009 Share Posted May 6, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/ Share on other sites More sharing options...
Mark Baker Posted May 6, 2009 Share Posted May 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/#findComment-827714 Share on other sites More sharing options...
stevieontario Posted May 6, 2009 Author Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/#findComment-827737 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/#findComment-827744 Share on other sites More sharing options...
stevieontario Posted May 6, 2009 Author Share Posted May 6, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/#findComment-827802 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Promiso, Nice! Quote Link to comment https://forums.phpfreaks.com/topic/157110-help-looping-through-an-array/#findComment-827804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.