简体   繁体   中英

How to properly insert rows from Oracle DB into HTML select box

I am fetching rows from an Oracle table to put them into a select box but the output is not as expected. To give some context, i am querying the database to give me 4 columns of the desired rows to put each row on a single line in the drop menu but instead, i get every column of each row individually on a line. I would like to get help on how i could proceed to have each individual row with their 4 columns on a single line.

This is my oracle table from which i am retrieving data:

create table TP2_ITEM (
    NO_ITEM number generated always as identity,
    NO_ENCAN number(9) not null,
    NO_ITEM_ENCAN_ITE number(9) not null,
    TITRE_ITE varchar2(50) not null,
    CHEMIN_PHOTO_ITE varchar2(200) default 'N/A',
    DESC_ITE varchar2(200) default 'N/A',
    DONATEUR_ITE varchar2(25) not null,
    MNT_VALEUR_ITE number(8,2) not null,
    MNT_PRIX_DEPART_ITE number(8,2) not null,
    MNT_INCREMENT_MINI_ITE number(8,2) not null,
    MNT_ACHAT_IMMEDIAT_ITE number(8,2) not null,
    EST_FERME_ITE number(1) default 0,
    EST_PAYE_ITE number(1) default 0,
    constraint PK_ITEM primary key(NO_ITEM),
    constraint FK_NO_ENCAN1 foreign key(NO_ENCAN)
    references TP2_ENCAN(NO_ENCAN) on delete cascade,
    constraint AK_ENCAN_ITEM unique(NO_ENCAN,NO_ITEM_ENCAN_ITE));

this is the code to fetch my rows and put them into a select box:

<?php 
    $stid = oci_parse($conn, "select I.NO_ITEM, NO_ITEM_ENCAN_ITE, TITRE_ITE, 
                              MNT_VALEUR_ITE from TP2_ITEM I,TP2_ITEM_FAVORI F
                              where NOM_UTILISATEUR = '" .$nom_utilisateur. "' 
                              and F.NO_ITEM = I.NO_ITEM and NO_ENCAN = '" 
                              .implode(',', $no_encan_uti). "'";
    oci_execute($stid);
    echo "<select>";
    while (($row = oci_fetch_array($stid, OCI_ASSOC)) != false {
        foreach ($row as $item) {
            echo "<option>" .$item. "</option>;
        }
    }
    echo "</select>";
    oci_free_statement($Stid);
?>
```[![Output from my program][1]][1]

PS I do not have any errors when i run the program but the result is not what i expect, like i described earlier.

** This is an image of the result i get with the drop menu.

Assuming that I.NO_ITEM is the identifier, and you want to use that field as the value of the option, and the other three as the text, you can do

while ($row = oci_fetch_array($stid, OCI_ASSOC) {

    $value = $row[0];
    $text = $row[1] . ' ' . $row[2] . ' ' . $row[3];

    echo "<option value='$value'>$text</option>"; 

}

You don't need to compare a value to false in a conditional, is enough to use that value as the condition, if it is false the condition fails.

If you check the code you posted, you will also see that you have two sets of parenthesis around the assignment ((while ($row = oci_fetch_array(...)) , but no parethesis after the !=false condition and before the { so your code probably is not running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM