Pablo
Pablo

Reputation: 113

Split results into variables after mysqli query

I have following:

if($broj_podstanica != "" && $broj_podstanica != 0) {
                        $uzmi_podstanice = "SELECT  *  FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT $broj_podstanica";
                        $pronasao_sve_podstanice = $db->query($uzmi_podstanice);
         while($sifrePodstanica = $pronasao_sve_podstanice->fetch_assoc()) {
                                    $sifreIzbrojane = $sifrePodstanica['sifra'] . ",";
                                    $izbaci_zarez = explode(",", $sifreIzbrojane);

                                if (!isset($izbaci_zarez[0])) {
                                    $izbaci_zarez[0] = "";
                                    $pods0 = "";
                                } else {

                                    $pods0 = $izbaci_zarez[0];
                                }
                                if (!isset($izbaci_zarez[1])) {
                                    $izbaci_zarez[1] = "";
                                    $pods1 = "";
                                } else {
                                    $pods1 = $izbaci_zarez[1];
                                }
                                if (!isset($izbaci_zarez[2])) {
                                    $izbaci_zarez[2] = "";
                                    $pods2 = "";
                                } else {
                                    $pods2 = $izbaci_zarez[2];
                                }
}
                    echo "1:" . $pods0;
                    echo "2:" . $pods1;
                    echo "3:" . $pods2;
                    echo "4:" . $pods3;
                    echo "5:" . $pods4;
}

Query gives me results: 30313233. After while loop I tried to control variables $pods0, $pods1, $pods2, $pods3 and $pods4 but It gives me result for first variable only; $pods0 is 30.. Is it possible to get other values from variables?

Upvotes: 1

Views: 77

Answers (1)

Almog
Almog

Reputation: 220

You have some problems with your code, first of all, you do not need use isset() function, it always returns true because of the variable is exists. Secondly, if $izbaci_zarez[1] is empty you do not need to set it again with an empty value. The last thing, store the data in an array instead of a variable and it will not limit your variables count (Because of it will hard to follow) and set the values to their correct index.

if($broj_podstanica != "" && $broj_podstanica != 0) {
    $uzmi_podstanice = "SELECT  *  FROM objekt WHERE vrsta_objekta = '2' ORDER BY sifra ASC LIMIT $broj_podstanica";
    $pronasao_sve_podstanice = $db->query($uzmi_podstanice);
    $i = 1;
    $pods = array();
    while($sifrePodstanica = $pronasao_sve_podstanice->fetch_assoc()) {
        $sifreIzbrojane = $sifrePodstanica['sifra'] . ",";
        $izbaci_zarez = explode(",", $sifreIzbrojane);
        $pods[$i] = $izbaci_zarez[0];
        $i++;
    }
    echo "1:" . $pods[1];
    echo "2:" . $pods[2];
    echo "3:" . $pods[3];
    echo "4:" . $pods[4];
    echo "5:" . $pods[5];
    // and etc...
}

Upvotes: 1

Related Questions