[thelist] need guidance on arrays and loops in PHP

Kipper Timmins kipper_timmins at yahoo.co.uk
Fri Mar 28 04:08:33 CDT 2008


Hi, if I am understanding this correctly then I think you could do it just
with a for loop,
Erm so before you enter the for loop do the select statement and store the
lo_id and la_id variables locally,
Ie,


$lo_id_local = //select statement here
$la_id_local = //select statement here

//this is the user value input for id which you got,
$id = array(//values);
$counter = $id.length-1; //counter to go through the array
For(Boolean check = true; check = true;){
	//Check to see if the values in the array at position $counter match
to the lo_id and la_id local values
	If($id($counter) == $lo_id_local || $id($counter) == $la_id_local){
		Check = true; //if they do match then set check to be true
so that you iterate through the loop again,
		$counter --; //subtract 1 from the $counter so that you go
back through the array.
	} else {
		//mysql set operation here to input $id into the database
		$check = false;// leaves the loop.
	}
}

Something you might have to think about is; is the array deffinately going
to have the id value in it? If it is then you will be fine, but if not then
you will have an array out of bounds exception which you can deal with using
a quick if statement and then returning a screen saying try again or
something like that.
I think I may have confused a bit of java in there, but the principal is
there.
Hope this helps, and this may not be the most efficient way to do this!

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Jeremy Weiss
Sent: 27 March 2008 17:37
To: Evolt
Subject: [thelist] need guidance on arrays and loops in PHP

I have to write a script to process some values that could have been
submitted via several different forms. One of the values, has the
potiential to be an array. If that value is there, then it's going to
be a number that  happens to be a primary key for a table in a MySQL
db. Up to this point, things seem fairly simple. But the record that
key goes to will be something like:

id (pk) | lo_id (fk) | la_id (fk) | bla bla bla 30+ other fields

If it's just one value in the array, then there's no problem. But if
it's more than one, then I have to pull the lo_id and la_id fields and
IF those match for each value in the array, then I'll do one thing.
However, if even one of the values doesn't match then I do something
totally different.

So I'm thinking a simple  foreach (array_expression as $value)
would let me loop through and run the SELECT statements to pull the
values of lo_id and la_id for each key in the array. But where do I
store them  (the values of lo_id and la_id) so that I can compare them
to what is turned up on the other searches? Or, do I just run through
on the first one, and then make the comparisons in the foreach loop?

-jeremy
-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 





More information about the thelist mailing list