charles36 Posted December 19, 2016 Share Posted December 19, 2016 Hi, On my site I have dropdown selection boxes which use months, years and others. So to save myself repeating I wrote a quick function which selects all rows in a mysql database so I can call into any dropdown box Class Fetch { function getMonths($db){ try { $sql = 'SELECT monthName FROM months'; $stmt = $db->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch(PDOException $e) { echo '<p class=bg-danger center">'.$e->getMessage().'</p>'; } } } Then I just use this on any page I need to populate dropdowns (using a foreach) $months = $fetch->getMonths($db); This works but I thought it might be better if I created an array which would be easier to edit should it be needed, and so I can get rid of the various tables in my database. I have tried several combinations thinking I just have the syntax wrong but now am wondering if it's even possible or sensible. Is there a simple way to do this or shall I just stick with what's working at the moment?! This is my latest failure, it bring back empty dropdowns var $montharray = array('January', 'February', 'March'); function monthsArray(){ print($montharray); } $months = $fetch->monthsArray(); Quote Link to comment Share on other sites More sharing options...
requinix Posted December 19, 2016 Share Posted December 19, 2016 Why do you need a database? Why do you need anything more complicated than something like function get_months() { return ["January", "February", "March", etc]; }Internationalization/support for different languages is an acceptable answer, but doesn't seem to be the case here. 1 Quote Link to comment Share on other sites More sharing options...
DeX Posted December 19, 2016 Share Posted December 19, 2016 You appear to be printing the array when what you want to do is return it. Your function isn't actually returning anything so your foreach loop isn't receiving anything. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted December 19, 2016 Share Posted December 19, 2016 You appear to be printing the array when what you want to do is return it. Your function isn't actually returning anything so your foreach loop isn't receiving anything. That...and $montharray is a property of a class. So you would need to use $this->montharray to get the value. Quote Link to comment Share on other sites More sharing options...
charles36 Posted December 20, 2016 Author Share Posted December 20, 2016 (edited) Thanks but when I try this the dropdown list with a 'Warning: illegal string offset' error. function listMonths() { return ['january'=>'January', 'february'=>'February', 'march'=>'March', 'april'=>'April', 'may'=>'May', 'june'=>'June', 'july'=>'July', 'august'=>'August', 'september'=>'September', 'october'=>'October', 'november'=>'November', 'december'=>'December']; } $months = listMonths(); <select name="startMonth1" id="startMonth1" class="form-control"> <?php foreach ($months as $rows) { ?> <option value="<?php echo $rows['rows']; ?>" <?php if($startmonth1 == $rows['rows']) echo 'selected'; ?>><?php echo $rows['rows']; ?></option> <?php } ?> </select> when I print_r($months) I get: Array ( [january] => January [february] => February [march] => March [april] => April [may] => May [june] => June [july] => July [august] => August [september] => September [october] => October [november] => November [december] => December ) Edited December 20, 2016 by charles36 Quote Link to comment Share on other sites More sharing options...
charles36 Posted December 20, 2016 Author Share Posted December 20, 2016 <select name="startMonth1" id="startMonth1" class="form-control"> <?php foreach($months as $rows){ ?> <option value="<?php echo $rows ?>"><?php echo $rows ?></option> <?php } ?> </select> Nevermind! I see my error now, thanks for the nudge in the right direction Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 20, 2016 Share Posted December 20, 2016 (edited) Using a DB is overkill for this. You should also be using the month number for the key, not the month name. <?php $months = [ "1" => "January", "2" => "February", "3" => "March", "4" => "April", "5" => "May", "6" => "June", "7" => "July", "8" => "August", "9" => "September", "10" => "October", "11" => "November", "12" => "December" ]; ?> <select name="month"> <option>Select Month</option> <?php foreach ($months as $month_number => $month_name):?> <option value='<?= $month_number ?>'><?= $month_name ?></option> <?php endforeach;?> </select> Edited December 20, 2016 by benanamen Quote Link to comment Share on other sites More sharing options...
charles36 Posted December 20, 2016 Author Share Posted December 20, 2016 Is the purpose of using the foreach (...): and endforeach; to make the code easier to read without having to find the end curly bracket? or is that the correct syntax? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted December 20, 2016 Share Posted December 20, 2016 Is the purpose of using the foreach (...): and endforeach; to make the code easier to read without having to find the end curly bracket? or is that the correct syntax? endforeach is the alternate syntax. More information can be found here: http://php.net/manual/en/control-structures.alternative-syntax.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.