foreach loop, assistance request


Posted on 16th Feb 2014 07:03 pm by admin

I would like some guidance on the usage of foreach as I try to parse through a large database and whittle down to a few select stocks. I'm currently doing this with vb, but I'm wanting to get it working in php. I think I'm just struggling with the syntax, but I'm unsure. After a week of trial and error, I've decided it's time to reach out for help.

Currently, I can connect with the db, run a simple query, and return the results to a web page. The db is made up of the past 2 weeks' stock symbols, prices, volumes. I want to go through each symbol and check against 2 conditions. If both conditions are true for that symbol, I want to put it (and the 2 weeks of data) in another db for further parsing later. So in plain english, here's what I'm trying to accomplish:

for each unique symbol,
if condition a is true on every day and
condition b is true on every day, then
write all the data for the past 2 weeks for that symbol to db_x

I'm not sure if it's the most efficient method, but I was thinking I'd loop through the db checking the conditions and meanwhile saving the data to an array, then if the conditions are true when it gets to a different symbol it runs thrrough the array and adds the data to db_x....if it gets a false anywhere along the way for a particular symbol, it clears the array and moves on to the next symbol to check it. I don't want to hard code the 2 week's (ie loop through each stock 10 times, because of holidays, some stocks do not trade every day, etc). I need to check for the symbol changing to know when it's time to make a decision on whether to add or drop the previous symbol.

I've got it loosely structured to use a while loop to cycle through the entire list:
while ($row = mysql_fetch_assoc($result)) {

Then I suppose the foreach would be used, but I'm running into complex commands that I'm needing guidance on. Is ArrayIterator current to PHP5, and if so, is this the direction I should be studying up on? Any recommendations on a good online tutorial for someone who is not well versed in programming, but can get by through dogged determination (and a little help from my friends )?

It should be noted that my array I'm iterating through (the symbols) has several associated elements (volume, closing price, high, low).

As for providing code, I really don't have anything to go by yet. I have not delved too far past "foreach ($arr as $key => $value). I'm using mysql 5 and php 5 and apache2 on an Ubuntu 9.04 machine.

Thank you,

No comments posted yet

Your Answer:

Login to answer
335 Like 22 Dislike
Previous forums Next forums
Other forums

ereg_replace issue
hi there people

i have this code happening with regards to my wamp server. is this something

Loop Through Date Range
Hi guys,
I have date range as parameter like 01/JAN/2009 TO 16/JAN/2009 now i want to loop thro

show the direct link into href
I want to display link on the page. i am fetching the data(URL) from database,actually before that i

Php If in MySql query (hiding labels if a field is empty)
Okay, I've been trying to do this for a while, and I'm finally going to ask for help so I can get th

Taxonomy? Classification? Categorisation?
Not sure if there is a way around this classification problem
I have a supplier who produces

How to copy a part of a vector in a raw memory
Hi,

How can I copy a part of a vector into a memory:

1
2
3
4
5

Email to a friend script problems
I have this send-to-a-friend script with 2 issues:


1) When you open the form popup on a p

Troubles with a spider class
I am building a spider that will crawl through random whitepages (eg. anywho.com, switchboard.com, w

mysql_real_escape_string
Let me preface this that I am very much a PHP noob, but I have some SQL training (not necessarily My

Phase Error I can't seem to find :(`
Hey guys, I can seem to find my phase error on this. I am getting this error

[error]
Pars

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash