Jump to content

PHP array into a select list


loxfear
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
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);
?>
Link to comment
Share on other sites

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
Link to comment
Share on other sites

what i need is an array with arrays inside, i want to be able to select the arrays inside the array, and when that is selected i want the data in that to be shown,

but for now, i need the arrays to be shown in the select list menu ;) 

Link to comment
Share on other sites

  • Solution

You would need to tell it which item of the array you want to echo, so either of these I guess

echo '<option value="'.$key.'">'.$value['title'].'</option>';
echo '<option value="'.$key.'">'.$value['pris'].'</option>';
Link to comment
Share on other sites

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);
?>
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.