Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-03-2005, 01:43 PM Merge arrays?
Novice Talker

Posts: 8
Trades: 0
Hello

I have two arrays with data that relates directly to each other.
Key 0 in the first array relates to key 0 in the second and so on.

Array1 ( [0] => 4 [1] => 9 [2] => 12 [3] => 16 [4] => 18 [5] => 20 )
Array2 ( [0] => 1 [1] => 3 [2] => 1 [3] => 1 [4] => 1 [5] => 2 )

I need a way to loop through these arrays in order to add/subtract amounts stored in a database. Array1 is the product id and Array 2 is the amount.

What is the best way of doing this?


Thanks in advance.
davedark is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-03-2005, 05:40 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
This'll do what you want dave;

PHP Code:

// script controller

if(isset($_POST['Submit']))
{

// count number of indexes in array1 and set limit

$limit count($Array1);

// begin loop through array1

for ($i=0$I<$limit$i++)

{

//get adjustment amt from array2

$adjustAmt $Array2[$i];

// select product 'Amount' from db

$sql 'SELECT Amount FROM table_gifts WHERE product_ID = '.$Array[$i];
$query mysql_query($sql$dbConn) or die(mysql_error());

// get amount from resultset

$amount mysql_fetch_assoc['Amount'];

// do calc

$updateAmt $amount $adjustAmt;

// update amount

$sql2 'UPDATE table_gifts SET Amount = '.$updateAmt.' WHERE product_ID = '.$Array1[$i];
$query2 mysql_query($sql2$dbConn) or die (mysql_error());

// end for loop

}

// redirect after successful updates

$redirect 'someURL.php?updates=ok';
header('Location: '.$redirect);
exit();

// end script controller


__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 07-03-2005, 07:06 PM
Novice Talker

Posts: 8
Trades: 0
Cheers, played around with the code a bit...........works well now.
Slowly getting my head around arrays!
davedark is offline
Reply With Quote
View Public Profile
 
Old 07-03-2005, 07:38 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Dave, I'd split the time you have for learning PHP to include learning SQL for MySql as well. Quite often, fairly complex manoevers in php are done very simply in sql. For example, the above update query could have had mysql do the arithmetic instead of php.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 07-03-2005, 07:40 PM
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Dave, I'd split the time you have for learning PHP to include learning SQL for MySql as well. Quite often, fairly complex manoevers in php are done very simply in sql. For example, the above update query could have had mysql do the arithmetic instead of php.

I'm glad you got it working, I didn't see the "$I" typo, that woulda thrown an undefined notice and screwed up the loop.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Reply     « Reply to Merge arrays?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.65944 seconds with 12 queries