Array and MySQL Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Array and MySQL Help

Post by 3hhh »

Hi,

I have 2 same-sized arrays ($array and $array1) such that they are :

Array ( [0] => VIP09,VIP10,VIP11,VIP12 [1] => VIP13 )
Array ( [0] => 1 [1] => 2 )

The problem is that in $array, I have 4 values in index 0 and in $array2, I have 1 value in index 0.

That means to say that I would like to achieve this:

UPDATE seat SET reservationId='".$id."' WHERE id="VIP09" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP10" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP11" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP12" AND matchId="1"

UPDATE seat SET reservationId='".$id."' WHERE id="VIP13" AND matchId="2"

Can anyone help pls? Thanks
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Array and MySQL Help

Post by papa »

Explode on array[0] ?
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: Array and MySQL Help

Post by 3hhh »

papa wrote:Explode on array[0] ?
Hmmm... but if I explode on array[0], the values will not tally.

That means it would be:
[0] =>VIP09
[1] =>VIP10
[2] =>VIP11
[3] =>VIP12

and my the other array will be:
[0] =>1

that being said, I won't be able to achieve this (dynamically. The values are listed there to show what I intend to do. I intend to do a for loop):

UPDATE seat SET reservationId='".$id."' WHERE id="VIP09" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP10" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP11" AND matchId="1"
UPDATE seat SET reservationId='".$id."' WHERE id="VIP12" AND matchId="1"

UPDATE seat SET reservationId='".$id."' WHERE id="VIP13" AND matchId="2"
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Array and MySQL Help

Post by Ziq »

I think papa told about something like this:

Code: Select all

 
<?
$array = Array ('VIP09,VIP10,VIP11,VIP12', 'VIP13' ) ;
$array1 = Array (1 , 2 );
 
foreach ($array as $k=>$v)
{
    $v = explode(',', $v);
    foreach ($v as $v2)
    {
        echo 'UPDATE seat SET rese=... WHERE id="'.$v2.'" AND matchId="'.$array1[$k].'"'."<br>\r\n";
    }
}
?>
 
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: Array and MySQL Help

Post by 3hhh »

Ziq wrote:I think papa told about something like this:

Code: Select all

 
<?
$array = Array ('VIP09,VIP10,VIP11,VIP12', 'VIP13' ) ;
$array1 = Array (1 , 2 );
 
foreach ($array as $k=>$v)
{
    $v = explode(',', $v);
    foreach ($v as $v2)
    {
        echo 'UPDATE seat SET rese=... WHERE id="'.$v2.'" AND matchId="'.$array1[$k].'"'."<br>\r\n";
    }
}
?>
 
ahhh yes... an inner for loop! Thanks man :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Array and MySQL Help

Post by onion2k »

Ziq wrote:I think papa told about something like this:

Code: Select all

 
<?
$array = Array ('VIP09,VIP10,VIP11,VIP12', 'VIP13' ) ;
$array1 = Array (1 , 2 );
 
foreach ($array as $k=>$v)
{
    $v = explode(',', $v);
    foreach ($v as $v2)
    {
        echo 'UPDATE seat SET rese=... WHERE id="'.$v2.'" AND matchId="'.$array1[$k].'"'."<br>\r\n";
    }
}
?>
 
If you have the ids as a comma separated list, why not just use IN in the SQL? Then you don't need an inner loop at all. Eg..

Code: Select all

<?php
$id = 1;
$array1 = Array ('VIP09,VIP10,VIP11,VIP12', 'VIP13' );
$array2 = Array (1 , 2 );
 
for ($i=0; $i<count($array1); $i++) {
  echo "UPDATE seat SET reservationId=\"".$id."\" WHERE id IN (".$array1[$i].") AND matchId=".$array2[$i];
}
 
?>
Post Reply