galvin Posted April 7, 2011 Share Posted April 7, 2011 So if I have this code (while working on my local machine )... for ($i=1; $i<=15; $i++) { if ($timelimit == $i) { ${"time".$i}="selected='selected'"; } } <select name="timelimit"> <option value="1" <?php echo $time1;?>>1</option> <option value="2" <?php echo $time2;?>>2</option> <option value="3" <?php echo $time3;?>>3</option> <option value="4" <?php echo $time4;?>>4</option> <option value="5" <?php echo $time5;?>>5</option> <option value="6" <?php echo $time6;?>>6</option> <option value="7" <?php echo $time7;?>>7</option> <option value="8" <?php echo $time8;?>>8</option> <option value="9" <?php echo $time9;?>>9</option> <option value="10" <?php echo $time10;?>>10</option> <option value="11" <?php echo $time11;?>>11</option> <option value="12" <?php echo $time12;?>>12</option> <option value="13" <?php echo $time13;?>>13</option> <option value="14" <?php echo $time14;?>>14</option> <option value="15" <?php echo $time15;?>>15</option> </select> When I view the select menu in the browser, all but one of the options says "Notice: Undefined variable: time1 in C:\wamp\www\yyyyyy\create.php on line 112". It doesn't say this if I publish everything to the internet. So the question is, should I put code in to eliminate those Notices, or does it not matter. In other words, if I add this snippet below to declare all the other variables, it eliminates all the Notices. But is this necessary/good practice to do?.. for ($i=1; $i<=15; $i++) { ${"time".$i}=""; } Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/ Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 personally i wouldnt bother (I'm lazy), but i would say it better practice to declare, as it eliminates the possibility of what you see happening locally happening on the live site. Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198024 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2011 Share Posted April 7, 2011 The error is still occurring on your live server, but either the error_reporting or display_errors settings are set to hide them. However, your server is probably still logging them, thereby creating multi-gigabyte error log files. Code should not generate ANY type of errors during its normal execution. Only for abnormal things, such as a hacker attempting to break into your script or a legitimate visitor doing something perfectly valid but that your code did not take into account. You would want to log errors due to these type of things, but you wouldn't want to have to sift through millions of error messages to find them just because every time your code runs it generates dozens of error messages. Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198028 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2011 Share Posted April 7, 2011 For the specific code you posted, there's no reason to use a loop to optionally create 15 different variables, just to put them into a hard-code HTML <select> menu. Just simplify all that to make the <select> menu all at once - <?php $select = "<select name='timelimit'>\n"; for ($i=1; $i<=15; $i++) { $selected = ($timelimit == $i) ? "selected='selected'" : ''; // determine which choice is selected or a default value of an empty string $select .= "<option value='$i' $selected>$i</option>\n"; } $select .= "</select>"; echo $select; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198034 Share on other sites More sharing options...
galvin Posted April 7, 2011 Author Share Posted April 7, 2011 Wow, so simple and so smart. I have a ton of these situations so this will help condense my code a ton. Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198035 Share on other sites More sharing options...
galvin Posted April 7, 2011 Author Share Posted April 7, 2011 Just curious, is that type of condensed code doable with a select option that uses text values instead of numbers, as in below... Seems like it's not doable since you can't really loop through a set of strings. Just curious if I'm missing some way to condense this as well... //code to set category to whatever has been previously chosen for ($i=1; $i<=13; $i++) { ${"category".$i}=""; } if ($category == "Sports") { $category1 ="selected='selected'"; } elseif ($category == "Movies") { $category2 ="selected='selected'"; } elseif ($category == "TV") { $category3 ="selected='selected'"; } elseif ($category == "Music") { $category4 ="selected='selected'"; } elseif ($category == "Geography") { $category5 ="selected='selected'"; } elseif ($category == "Science") { $category6 ="selected='selected'"; } elseif ($category == "History") { $category7 ="selected='selected'"; } elseif ($category == "Pop Culture") { $category8 ="selected='selected'"; } elseif ($category == "Literature") { $category9 ="selected='selected'"; } elseif ($category == "Language") { $category10 ="selected='selected'"; } elseif ($category == "Games") { $category11 ="selected='selected'"; } elseif ($category == "Holiday") { $category12 ="selected='selected'"; } elseif ($category == "Miscellaneous") { $category13 ="selected='selected'"; } <span class="bold">Category:</span><select name="category"> <option value="Sports" <?php echo $category1;?>>Sports</option> <option value="Movies" <?php echo $category2;?>>Movies</option> <option value="TV" <?php echo $category3;?>>TV</option> <option value="Music" <?php echo $category4;?>>Music</option> <option value="Geography" <?php echo $category5;?>>Geography</option> <option value="Science" <?php echo $category6;?>>Science</option> <option value="History" <?php echo $category7;?>>History</option> <option value="Pop Culture" <?php echo $category8;?>>Pop Culture</option> <option value="Literature" <?php echo $category9;?>>Literature</option> <option value="Language" <?php echo $category10;?>>Language</option> <option value="Games" <?php echo $category11;?>>Games</option> <option value="Holiday" <?php echo $category12;?>>Holiday</option> <option value="Miscellaneous" <?php echo $category13;?>>Miscellaneous</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198036 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2011 Share Posted April 7, 2011 When you have a set of related values, you would generally use an array and use a foreach(){} loop to iterate over the array. Here is an example for producing (and validating) checkboxes that would equally apply to generating a select menu - http://www.phpfreaks.com/forums/index.php?topic=329432.msg1550587#msg1550587 Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198039 Share on other sites More sharing options...
galvin Posted April 7, 2011 Author Share Posted April 7, 2011 Dude, you're awesome. I used that post you referenced to write this below and it seems to be working perfectly. THANK YOU!!!!!! <?php $categories = array(0=>'Sports',1=>'Movies',2=>'TV',3=>'Music',4=>'Geography',5=>'Science',6=>'History',7=>'Pop Culture',8=>'Literature',9=>'Language',10=>'Games',11=>'Holiday',12=>'Miscellaneous'); $select = "<select name='category'>\n"; foreach($categories as $key => $value){ $selected = ($category == $value) ? "selected='selected'" : ''; // determine which choice is selected or a default value of an empty string $select .= "<option value='$value' $selected>$value</option>\n"; } $select .= "</select>"; echo $select; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198041 Share on other sites More sharing options...
galvin Posted April 7, 2011 Author Share Posted April 7, 2011 Anyone happen to know where I can read more about this bit of code. Basically, I want a breakdown in plain english what each part does. I have seen it a lot and never knew exactly how it worked and after this post, i know that I should be using it a lot more. Just want to fully understand it. If it has a name or something, just let me know that and I'll google it. Just not sure what to google in order to learn more about it $selected = ($category == $value) ? "selected='selected'" : ''; Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198043 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2011 Share Posted April 7, 2011 It's called the Ternary Operator. Scroll down to Example #2 at this link - http://us3.php.net/Ternary Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198048 Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 Written as an if/else statement, the example you posted would be the same as: if($category == $value) { $selected = "selected='selected'"; } else { $selected = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/232931-dealing-with-notices/#findComment-1198050 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.