Jump to content

Create array in class and call function


charles36

Recommended Posts

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

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.
  • Like 1
Link to comment
Share on other sites

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

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 by benanamen
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.