loxfear Posted May 20, 2014 Share Posted May 20, 2014 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 More sharing options...
loxfear Posted May 20, 2014 Author Share Posted May 20, 2014 im getting the data from my sql server, putting it into an array and then i want the selection list to show the array Link to comment https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480213 Share on other sites More sharing options...
cyberRobot Posted May 20, 2014 Share Posted May 20, 2014 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 https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480217 Share on other sites More sharing options...
Psycho Posted May 20, 2014 Share Posted May 20, 2014 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? Link to comment https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480218 Share on other sites More sharing options...
loxfear Posted May 20, 2014 Author Share Posted May 20, 2014 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 https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480220 Share on other sites More sharing options...
fastsol Posted May 20, 2014 Share Posted May 20, 2014 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 https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480227 Share on other sites More sharing options...
cyberRobot Posted May 20, 2014 Share Posted May 20, 2014 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 https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480229 Share on other sites More sharing options...
loxfear Posted May 20, 2014 Author Share Posted May 20, 2014 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 ;/ Link to comment https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480231 Share on other sites More sharing options...
loxfear Posted May 20, 2014 Author Share Posted May 20, 2014 Nvm it suddently worked Link to comment https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480232 Share on other sites More sharing options...
loxfear Posted May 20, 2014 Author Share Posted May 20, 2014 now how do i make it echo the pris row beside the select ? Link to comment https://forums.phpfreaks.com/topic/288636-php-array-into-a-select-list/#findComment-1480233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.