Columbus Columbus - 1 month ago 8
HTML Question

How to delete selected values in an sql-database-array

By using an html-form, I want to read out data from an sql-database and show them in an html-table. This works fine! Now, I want to reduce the number of columns in the table AFTER the sql-select-query.

$field = mysqli_fetch_fields($result)
returns the column-names and
var_dunp($field)
returns the folowing result:


array(11) { [0]=> object(stdClass)#3 (13) { ["name"]=> string(2) "id" ["orgname"]=> string(2) "id" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(1) ["length"]=> int(11) ["charsetnr"]=> int(63) ["flags"]=> int(49667) ["type"]=> int(3) ["decimals"]=> int(0) } [1]=> object(stdClass)#4 (13) { ["name"]=> string(7) "Vorname" ["orgname"]=> string(7) "Vorname" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(9) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [2]=> object(stdClass)#5 (13) { ["name"]=> string(4) "Name" ["orgname"]=> string(4) "Name" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(14) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [3]=> object(stdClass)#6 (13) { ["name"]=> string(3) "Str" ["orgname"]=> string(3) "Str" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(22) ["length"]=> int(60) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [4]=> object(stdClass)#7 (13) { ["name"]=> string(3) "PLZ" ["orgname"]=> string(3) "PLZ" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(4) ["length"]=> int(15) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(254) ["decimals"]=> int(0) } [5]=> object(stdClass)#8 (13) { ["name"]=> string(3) "Ort" ["orgname"]=> string(3) "Ort" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(10) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [6]=> object(stdClass)#9 (13) { ["name"]=> string(3) "Tel" ["orgname"]=> string(3) "Tel" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(11) ["length"]=> int(75) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [7]=> object(stdClass)#10 (13) { ["name"]=> string(5) "EMail" ["orgname"]=> string(5) "EMail" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(20) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [8]=> object(stdClass)#11 (13) { ["name"]=> string(3) "WWW" ["orgname"]=> string(3) "WWW" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(17) ["length"]=> int(90) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } [9]=> object(stdClass)#12 (13) { ["name"]=> string(7) "Notizen" ["orgname"]=> string(7) "Notizen" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(13) ["length"]=> int(196605) ["charsetnr"]=> int(33) ["flags"]=> int(16) ["type"]=> int(252) ["decimals"]=> int(0) } [10]=> object(stdClass)#13 (13) { ["name"]=> string(10) "Geburtstag" ["orgname"]=> string(10) "Geburtstag" ["table"]=> string(8) "adressen" ["orgtable"]=> string(8) "adressen" ["def"]=> string(0) "" ["db"]=> string(4) "team" ["catalog"]=> string(3) "def" ["max_length"]=> int(10) ["length"]=> int(30) ["charsetnr"]=> int(33) ["flags"]=> int(0) ["type"]=> int(253) ["decimals"]=> int(0) } }


Since this is an array, I now want to remove selected elements from this array and tried to do this with:

// Initilize what to delete
$delete_val = array('PLZ');
// Search for the array key and unset
foreach($delete_val as $key){
$keyToDelete = array_search($key, $field);
unset($field[$keyToDelete]);
}


or with

$toremove = "PLZ";
$field = array_flip($field);
unset($field[$toremove]);
$field = array_flip($field);


However, both ways did not work. Obviously, the element I want to remove is not found within the array with this methods.

In a second step, I have to modify the while-loop, so that the correct values are shown in the respective columns:

$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) {
echo "<td>" . $row[$k] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // ende of while-loop


I would be grateful if somebody could help me with this Problem!

Answer

That's not normal array, that's an array of objects, which is what's expected from mysqli_fetch_fields() function, so your delete logic won't work in that fashion. Your code should be like this:

(Suppose PLZ is the column you want to delete here)

// Initilize what to delete
$delete_val = array('PLZ');
// Search for the array key and unset
foreach($delete_val as $dv){
    foreach($field as $key => $object){
        $array = (array)$object;
        if(in_array($dv, $array, true)){
            unset($field[$key]);
        }
    }   
}
$field = array_values($field);

// display $field array
var_dump($field);