Jump to content

Array results to select option


kproctor

Recommended Posts

HI,

 

The below code returns these results all the way to 44:

 

0
1
2
3ATB Financial
4Alterna Bank
5Alterna Savings
6Bank of Montreal
7Bank of Nova Scotia
8CIBC Mortgages
9Caisses 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>";
	
}
?>

Link to comment
Share on other sites

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>";
?>

 

 

Link to comment
Share on other sites

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

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.  

Link to comment
Share on other sites

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

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.

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.