0

I am trying to build a dropdown list from my sql database. Managed to get it work somewhat, but the list only shows the last item in the table, and i briefly get this printed on the page as the page reloads:

[
 {"id":"1","zavod_ime":"PEIT"},
 {"id":"2","zavod_ime":"PTJM"},
 {"id":"3","zavod_ime":"PM"},
 {"id":"4","zavod_ime":"PN"},
 {"id":"5","zavod_ime":"BS"}
]

this is my separate php file which handles connecting and putting things in an array:

    $sqltran = mysqli_query($con, "SELECT id, zavod_ime FROM zavod")or die(mysqli_error($con));
    $arrVal = array();

    while ($rowList = mysqli_fetch_array($sqltran)) {

                    $namez = array(
                        'id'=> $rowList['id'],
                        'zavod_ime'=> $rowList['zavod_ime']
                        );      


                        array_push($arrVal, $namez);    
    }
         echo  json_encode($arrVal);        


    mysqli_close($con);

and the list part in the html:

                    <select>
                        <?php foreach($namez as $zavod): ?>
                        <option value="<?= $namez['id']; ?>"><?= $namez['zavod_ime']; ?></option>
                        <?php endforeach; ?>
                    </select>  

ideas?

3
  • 1
    You're seeing the JSON object as you have echoed a json encoded array. Commented Jul 18, 2017 at 10:37
  • change it to this if you want array : echo json_encode($arrVal, true); Commented Jul 18, 2017 at 10:38
  • you should mark off questions with solutions given; you get good rep back also Commented Jul 18, 2017 at 11:10

3 Answers 3

5

You need arrVal to loop instead of namez

<select>
    <?php foreach($arrVal as $namez): ?>
    <option value="<?= $namez['id']; ?>"><?= $namez['zavod_ime']; ?></option>
    <?php endforeach; ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

1

First you are using echo in echo json_encode($arrVal); that is why your data is displaying.
If you are using ajax to retrieve data you should use echo followed by die()

Also you are returning json_encode so before you use that value decode the value.

The following is assuming that you are using include at the top of where you want your select

<?php $arrVal = json_decode($arrVal)?>
<select>
    <?php foreach($arrVal as $namez): ?>
    <option value="<?= $namez['id']; ?>"><?= $namez['zavod_ime']; ?></option>
    <?php endforeach; ?>
</select>

4 Comments

now i am seeing this in the list: Warning: json_decode() expects parameter 1 to be string, array given in /storage/ssd1/720/2097720/public_html/home.php on line 144
and also, how would the code without json_encode go?
insted of echo json_encode($arrVal); do this $arrVal = json_encode($arrVal), this will encode the array, but i think now you are getting the data as array so you are getting the error
did that and now i get:<b>Notice</b>: Undefined variable: arrval in <b>/storage/ssd1/720/2097720/public_html/home.php</b> on line <b>146</b><br /> <br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>/storage/ssd1/720/2097720/public_html/home.php</b> on line <b>146</b><br />
0

managed to make things work:

    $sqltran = mysqli_query($con, "SELECT id, zavod_ime FROM zavod")or die(mysqli_error($con));
    $arrVal = array();

    while ($rowList = mysqli_fetch_array($sqltran)) {

                    $namez = array(
                        'id'=> $rowList['id'],
                        'zavod_ime'=> $rowList['zavod_ime']
                        );      


                        array_push($arrVal, $namez);    
    }
         echo  json_encode($arrVal);        


    mysqli_close($con);

and html:

                        <?php foreach($arrVal as $namez): ?>
                        <option value="<?= $namez['id']; ?>"><?= $namez['zavod_ime']; ?></option>
                        <?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.