Solved

Arrays - need to associate value in one with value in another for db insertion

Posted on 2007-11-29
6
263 Views
Last Modified: 2013-12-12
Hi - I've got a form that is generated from db data and returns two separate arrays -

(arrID) Array ( [0] => 208 [1] => 210 [2] => 222 [3] => 225 [4] => 236 )
(arrStatus) Array ( [0] => 2 [1] => 3 [2] => 3 [3] => 4 [4] => 1 )

I need to combine or otherwise associate the data in the arrays so that I can properly update the db. For example, I need to do a query similar to:

update table set status = $arrStatus[0] where ID = $arrID[0]

but I want do be able to do this in a foreach statement, as the number of items returned can  differ each time the form is submitted.

Thanks!
0
Comment
Question by:betagirl
  • 3
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
Comment Utility

if(count($arrID) == count($arrStatus)) {
 
 
 

	for($i=0;$i<count($arrID);$i++;){
 

		mysql_query("update table set status = ".$arrStatus[$i]." where ID = ".$arrID[$i]);
 

	}
 

}else{
 
 

	print "There was an error.<br> Items in Array arrID dont match items in Array arrStatus";
 
 

}

Open in new window

0
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
Opps extra ; on for($i=0;$i<count($arrID);$i++;){

please replace it with

for($i=0;$i<count($arrID);$i++){
0
 
LVL 1

Expert Comment

by:eXman_pl
Comment Utility
If i understood right:


foreach($arrID as $key => $val){

	$sql = "update table set status = $arrStatus[$key] where ID = $value";

	//execute the sql

}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:eXman_pl
Comment Utility
the above sql shoul look like this:
$sql = "update table set status = {$arrStatus[$key]} where ID = $value";

Open in new window

0
 

Author Comment

by:betagirl
Comment Utility
@steelseth12 - perfect, just what I needed.

@exmanpl - i needed to associate the values of one array with the values of the  same key  of the other - your query handles only the one array.

0
 
LVL 1

Expert Comment

by:eXman_pl
Comment Utility
No it handles both, you loop through one array and yose the current key value in the other.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now