Jump to content

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
https://forums.phpfreaks.com/topic/302777-create-array-in-class-and-call-function/
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

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.

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
<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 :)

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

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

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.