Solved

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

Posted on 2007-11-29
6
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20372254

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
ID: 20372257
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
ID: 20372265
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Expert Comment

by:eXman_pl
ID: 20372296
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
ID: 20375406
@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
ID: 20380502
No it handles both, you loop through one array and yose the current key value in the other.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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