Julez
Julez

Reputation: 1369

Multidimensional Array for making 2 sql update query

EDIT: i adjusted the array to understand it easyer.

I'm struggling with this a couple of days now and it drives me crazy...

What I basically want is 2 sql update queries that look like this. note that the where statement is from the array.

Code:

"UPDATE gewasregistratie SET lengtegroei= 1, vruchten_geaborteerd= 1, etc.. WHERE kenmerk = 'standaard' AND user_id = ".$user""

"UPDATE gewasregistratie SET lengtegroei= 2, vruchten_geaborteerd= 2, etc.. WHERE kenmerk = 'natugro' AND user_id = ".$user""

I got this multi-level array (its a $_POST["type"] array).

Array
(
    [lengtegroei] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [vruchten_geaborteerd] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [plantbelasting_geteld] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [uitgroeiduur] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [percentage_85ers_en_95ers] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [oogstfrequentie_per_week] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [gezette_vruchten] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [gemiddeld_vruchtgewicht] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [vruchten_geoogst] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

    [drain] => Array
        (
            [standaard] => 1
            [natugro] => 2
        )

)

Upvotes: 0

Views: 152

Answers (1)

SoftGuide
SoftGuide

Reputation: 336

do you mean something like this?

<?php
   $aAll = array();

   $aTogather = array(
       'standaard'  => array(),
       'natugro'        => array()
   );

   foreach($aPost as $sKey => $mRequest) {
       if(is_array($mRequest)) {
           foreach($mRequest as $sInnKey => $mValue)) {
               $aTogather[$sInnKey][] = $sKey . ' = ' . '"'.$mValue.'"';
           }
       }
   }

   foreach($aTogather as $sKey => $mValue) {
      echo 'UPDATE gewasregistratie SET '.implode(', ', $aTogather[$sKey]).' WHERE kenmerk = "'.$sKey.'" AND user_id = ...';
   }
?>

Upvotes: 1

Related Questions