kproctor Posted March 15, 2017 Share Posted March 15, 2017 HI, The below code returns these results all the way to 44: 0123ATB Financial4Alterna Bank5Alterna Savings6Bank of Montreal7Bank of Nova Scotia8CIBC Mortgages9Caisses Desjardins ... 44 There are two things I need to do 1. Remove the blank keys - 0,1,2,44 2. I want to assign the remaining 40 items to a form select option. Thank you for any help. <?php $lender_list = array_filter($lender_list); foreach($lender_list as $key => $value) { //$mykey = $key; echo $key; echo $value['name']; echo"<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 15, 2017 Share Posted March 15, 2017 Where is the data coming from? A database? Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 15, 2017 Author Share Posted March 15, 2017 from a website. I have made some progress. Here is what I have now. I like the dropdown list idea. The only problem I have to solve now is to remove the blank keys. <?php echo "<select>"; $lender_list = ($lender_list); foreach($lender_list as $key => $value) { echo '<option value="'. $key. '">' .$value['name'] . '</option>'; } echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 15, 2017 Share Posted March 15, 2017 What is this supposed to be doing? $lender_list = ($lender_list); Pro tip: create your logic (i.e. the core PHP code at the top of the script. Then output within the HTML at the bottom of the script. <?php //PHP Code at the TOP of the script //Create variable to hold the lender options $lenderOptions = ''; //Iterate through the array of lenders foreach($lender_list as $value => $label) { //Trim the value $label = trim($label); //If label is empty skip this record if(empty($label)) { continue; } //Create new option for the current lender $lenderOptions .= "<option value='{$value}'>{$label}</option>\n"; } ?> <html> . . . . <select name="some_field_name"> <?php echo $lenderOptions; ?> </select> </html> Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 15, 2017 Author Share Posted March 15, 2017 Thank you for the reply. I am getting this error message. Warning: trim() expects parameter 1 to be string, array given in /Library/WebServer/Documents/lessons101/SwitchCalculator.php on line 33 Question relating to best practice. The form I am creating is big with lots of functions. What is the recommended lengh of code before it should be set in a separate file and then use the include function. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted March 16, 2017 Share Posted March 16, 2017 (edited) A side note, most people get annoyed entering (or choosing) more than 8-10 fields on one page, so if the form is huge with a lot of input fields it is best to split the form and have it on multiple web pages. So that might give you an idea if you should separate the code in a separate file? Edited March 16, 2017 by Strider64 Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2017 Share Posted March 16, 2017 Warning: trim() expects parameter 1 to be string, array given in /Library/WebServer/Documents/lessons101/SwitchCalculator.php on line 33 Use this $label = trim($label['name']); Question relating to best practice. The form I am creating is big with lots of functions. What is the recommended lengh of code before it should be set in a separate file and then use the include function. Code should be separated based on its logical function/purpose. I would first start by separating the logic (i.e. PHP code) and the presentation (i.e. HTML content) into separate files. The presentation should just have code to output PHP variables that were generated within the logic. For new/casual programmers it is easiest to first start with what I proposed above - putting all the logic at the top of the script. In my opinion, people new to programming find it difficult to conceptualize the entire process when dealing with multiple, independent files and it is easier to work with a single field that creates the complete page. As you gain experience you should start moving any logic that is used on multiple pages into separate file(s). For example, if you make database calls, you should have a single file that creates the database connection. If you create multiple select lists dynamically, I would create a function that accepts tow parameters (an array with the value/label and (optionally) the currently selected value) and that function can build and return the HTML content for those select options. Once you have a better understanding of using multiple files, classes, etc in development then you can start using an MVC framework or something similar. Quote Link to comment 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.