-1

I have made this for loop to get a variable amount of columns selected from the database. As I'm running this, I get an error saying :

Notice: Undefined variable: kolom_1
Notice: Undefined variable: kolom_2
Notice: Undefined variable: kolom_3
Notice: Undefined variable: kolom_4
Notice: Undefined variable: kolom_5
Notice: Undefined variable: kolom_6

But I have it all placed in a for loop, why does he not recognize them? I am not getting what I'm doing wrong.

function lijst_ophalen($data, $from){

        $totaal = count($data);

        for ($i=1; $i<$totaal; $i++){
            $kolom_[$i] = $this->mysqli->real_escape_string($data['kolom_' . $i . '']);
            if($kolom_[$i]!="") $kolom_[$i] = "{$kolom_[$i]},"; else $kolom_[$i]="";

            if($kolom_[$i]==$totaal)  $kolom_[$i] = "{$kolom_[$i]}";

        }       

        $from_table = "";
        $categorie = "";

        if($from == "bv"){
            $from_table = "klanten_algemene_gegevens_bv";
            $categorie = "";
        }

        if(($from == "1manszaak") || ($from == "vof")){
            $from_table = "klanten_algemene_gegevens_vof_1manszaak";
            if($from == "1manszaak"){
                $categorie = "1manszaak";
            }
            if($from == "vof"){
                $categorie = "vof";
            }

            $categorie = "WHERE soort_onderneming = '{$categorie}'";
        }

        if($from == "ib"){
            $from_table = "klanten_ib";
            $categorie = "";
        }

        $result = $this->mysqli->query(
<<<EOT
            SELECT 
            {$kolom_1}
            {$kolom_2}
            {$kolom_3}
            {$kolom_4}
            {$kolom_5}
            {$kolom_6}
            FROM {$from_table}
            {$categorie}
EOT
        );
        if($result){
                $waardes = array();
                while ($row = $result->fetch_assoc()) {
                $waardes[]=$row;
            }
            return $waardes;
        }
    }
shA.t
  • 16,580
  • 5
  • 54
  • 111
Joop Schoolse
  • 125
  • 1
  • 1
  • 9
  • 7
    That's because $kolom_1 and $kolom_[1] is two different things, you are creating an' array called $kolom_ and you are trying to call a variable called $kolom_1 – Epodax Apr 22 '15 at 07:32
  • 2
    you are placing them in an array not in a variable. there is a difference between `$kolom_[1]` and `$kolom_1`. – Kuldeep Dangi Apr 22 '15 at 07:33
  • 1
    @Epodax why don't you post this as an answer? – Voitcus Apr 22 '15 at 07:47

1 Answers1

2

When you are initializing / filling data into the $kolom_ you are creating an' array instead of your (properly) intended series of different variables.

$kolom_[$i] <--- ARRAY

And lower down you are calling a series of variables $kolom_1, which doesn't exist because you created an' array and not a series of variables.

To avoid the error you can simply change your

$kolom_1
$kolom_2 (and so on)

calls into

$kolom_[1]
$kolom_[2] (and so on)

and you should be set to go.

Epodax
  • 1,828
  • 4
  • 27
  • 32