TampaMorton Posted December 15, 2009 Share Posted December 15, 2009 Hi, I know my code is a little sloppy but I'm keeping it ultra basic because I am still learning. Here is a snipped of my code. What the function should do is pick an if statement, and then use whatever data (if any) supplied in the second argument to choose a default box. This would be put between some <select> tags. It doesn't seem to be returning the string, or doing anything for that matter. My second problem is when I call this function it keeps saying that this function is not defined. I really am at my wits end because it works in this sample so I don't know why it says undefined in my bigger project <? $month = "March"; echo dateOpt(2, $month); function dateOpt($toggle,$set) { $string = ""; //1 if ($toggle == 1){ //2 for ($i = 1; $i <=31; $i++){ //3 if ($i == $set){ //4 $string .= "<option selected=selected>" . $i . "</option>"; } else { $string .= "<option>" . $i . "</option>"; } //3 } //2 } //1 if ($toggle == 2){ //2 $month = array("January","February","March","April","May","June","July","August","September","October","November","December"); foreach ($month as $value){ //3 if ($value == $set){ //4 $string .= "<option selected=selected>" . $value . "</option>"; } else { $string .= "<option>" . $value . "</option>"; } //3 } //2 } //1 if ($toggle == 3){ //2 for ($i = date("Y"); $i >= (date("Y")-5); $i--){ //3 if ($i == $set){ //4 $string .= "<option selected=selected>" . $i . "</option>"; // prints last five years. } else { $string .= "<option>" . $i . "</option>"; // prints last five years. } //3 } //2 } //1 return $string; } //0 ?> Thank you for your help, sorry for being such a noob I am really trying hard to learn this at home Quote Link to comment https://forums.phpfreaks.com/topic/185270-function-not-returning-a-string/ Share on other sites More sharing options...
Psycho Posted December 15, 2009 Share Posted December 15, 2009 I don't see anything obviously wrong as to why you are not getting output. Your second problem is probably due to how you are including the function in your other code. I will offer some suggestions though. 1) I would advise against using 1, 2, 3 to determine the type of list being created. It makes it more difficult to understand your code. Use values that you will understand - i.e. day, month, year. 2) Each of the different "lists" are doing the exact same thing, the only difference is the set of values. Your code is more complex than it needs to be. You should use a switch statement to define the list then perform the exact same operation of creating the optino values. Here's an example: <?php function dateOpt($listType, $selectedValue) { //Select the values for the list type switch($listType) { case 'day': $optionsAry = range(1, 31); break; case 'month': $optionsAry = array ( 'January','February','March','April','May','June', 'July','August','September','October','November','December' ); break; case 'year': default: $optionsAry = range(date("Y"), date("Y")-5, -1); break; } //Create the select options HTML $optionsHTML = ''; foreach($optionsAry as $optionValue) { $selected = ($optionValue==$selectedValue) ? ' selected="selected"' : ''; $optionsHTML .= "<option value=\"{$optionValue}\"{$selected}>{$optionValue}</option>\n"; } return $optionsHTML; } $month = "March"; echo dateOpt('month', $month); ?> NOTE: You could also put logic in to pass the currently selected month and year as optional values when getting the list for days. That way you can limit the number of days appropriately according the the month/year. Quote Link to comment https://forums.phpfreaks.com/topic/185270-function-not-returning-a-string/#findComment-978020 Share on other sites More sharing options...
TampaMorton Posted December 15, 2009 Author Share Posted December 15, 2009 I do agree that the code is extremely complex and sloppy, my main issue is that I am going so crazy trying to debug it it de-volves into the mess you see! However, as you suggested, cleaning it up looks really good and makes a lot more sense. The range() function I was unfamiliar with, and I will look into using these more. And hopefully I will be able to add some logic after I get these basic guys worked out ^.^ I have a lot of work to do later in the form where scheduling longer tasks means fewer avaliable options. Might I ask one more question, is it required to place the function before it is called to achieve results? If it is, is there any other way I can call to it before it is placed aside from include() in a separate file? I am still working on the separate issue of "Call to undefined function dateOpt()" in the main project file. I would post the code here but I feel it's kind of cheating and I'd never learn that way unless I absolutely cannot find a solution on my own On a good (bad?) note, I found that even this code is not returning any values! So it must be something with my hosts' php installation. At least I know I'm not completely crazy that even your code isn't working for me (although I'm sure it's valid!) Thank you again so much for helping with my coding Quote Link to comment https://forums.phpfreaks.com/topic/185270-function-not-returning-a-string/#findComment-978028 Share on other sites More sharing options...
TampaMorton Posted December 15, 2009 Author Share Posted December 15, 2009 Wouldn't let me edit my post, just to confirm yes your code works perfectly. It is definitely something wrong with my original host's PHP. Quote Link to comment https://forums.phpfreaks.com/topic/185270-function-not-returning-a-string/#findComment-978040 Share on other sites More sharing options...
Psycho Posted December 15, 2009 Share Posted December 15, 2009 To answer some of your questions, no you do not "have" to put the functions before you call them. It is just good programming etiquette (sp?). You *should* break your code up into logical sections. The logic (PHP), the presentation (CSS) and the output (HTML). For example, in the above I would probably use the function like this: $monthOptions = dateOpt('month', $month); That would be in the head of my document (or even a separate file). Then at the bottom (or output file) I woul have just HTML code with the dynamic output inserted appropriately <select name="month"> <?php echo $monthOptions; ?> </select> This keeps your code very clean and much easier to maintain. Try not to intermingle PHP logic in the HTML. Quote Link to comment https://forums.phpfreaks.com/topic/185270-function-not-returning-a-string/#findComment-978056 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.