Jump to content

charles36

New Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by charles36

  1. 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 )

  2. 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();
    
×
×
  • 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.