Jump to content

PHP array into a select list


Go to solution Solved by fastsol,

Recommended Posts

nothing gets put into the select list
<?php
include 'sqlconnect.php';

$data = array();
while ($row = mysqli_fetch_assoc($result))
{
    $data[$row['id']] = array(
        'title' => $row['title'],
        'pris'  => $row['pris'],
    );
} ?>

<select name="country">
<option value="">-----------------</option>
<?php
foreach($data as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
</select>


how do i get this to work-?

 
Link to comment
https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/
Share on other sites

Does your real code have a query? As the code stands, $result is undefined.

 

Have you tried turning on all PHP errors? To do that, you could add the following to your PHP script:

 

<?php
//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

Assuming $result is a valid MySQL result set (with records) you would get crap for output.

 

In this loop you are storing the records into a multi-dimensional array with the id as the index and then TWO values in the sub-array.

$data = array();
while ($row = mysqli_fetch_assoc($result))
{
    $data[$row['id']] = array(
        'title' => $row['title'],
        'pris' => $row['pris'],
);
}

You then loop through that array as follows:

foreach($data as $key => $value):
    echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;

$value with be the subarray - but you are trying to echo it out. Do you want to echo out 'title' or 'pris' or some concatenation of the two?

Edited by Psycho

Just in case it was missed, did you see my reply?

 

 

 

Does your real code have a query? As the code stands, $result is undefined.

 

Have you tried turning on all PHP errors? To do that, you could add the following to your PHP script:

<?php
//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

i did see ur reply :)

<?php
include 'sqlconnect.php';

$sql = mysqli_query($con,"SELECT * FROM aktiviteter");
$data = array();
while ($row = mysqli_fetch_assoc($sql))
{
    $data[$row['id']] = array(
        'title' => $row['title'],
        'pris'  => $row['pris'],
    );
} ?>

<select name="country">
<option value="">-----------------</option>
<?php
foreach($data as $key => $value):
echo '<option value="'.$key.'">'.$value.'</option>'; //close your tags!!
endforeach;
?>
</select>


atm it sees the array, and therefor there will be, array in the list, but what i need is the title of every row in there, its prob not that easy

echo '<option value="'.$key.'">'.$value['title'].'</option>';

only shows one title ;/

Edited by loxfear
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.