creating a drop down from csv file


#1

Hi all, I am trying to create a drop down box in a form from a csv file, which will give me the first entry of each line as the options. The nearest I have got so far creates as many boxes as options!! I can see why this has happened but cannot find out how to rectify it… Any help would be very much appreciated.
here is the code
[php]<?php
$users = file(“clientlist.txt”);
foreach ($users as $user) {
list($name, $addr1, $addr2, $addr3, $addr4, $addr5) = explode(",", $user);
echo “$name”;
}
?>
[/php]

And in case it helps this is the csv file contents

Client No 1, Client 1 addr1, Client 1 addr2, Client 1 addr3, Client 1 addr4, Client 1 addr5
Client No 2, Client 2 addr1, Client 2 addr2, Client 2 addr3, , Client 2 addr5
Client No 3, Client 3 addr1, Client 3 addr2, , , Client 3 addr5
Client No 4, Client 4 addr1, Client 4 addr2, Client 4 addr3, Client 4 addr4, Client 4 addr5
Client No 5, Client 5 addr1, Client 5 addr2, Client 5 addr3, Client 5 addr4, Client 5 addr5
Client No 6, Client 6 addr1, Client 6 addr2, , , Client 6 addr5


#2
Hello dai_trying, your looking for creating drop down for each line define in your csv. Replace below code [php] //Replace it echo "$name"; [/php] use below modified code [php] #Use it echo ''; echo ''.$name.''; echo ''.$addr1.''; echo ''.$addr2.''; echo ''.$addr3.''; echo ''.$addr4.''; echo ''.$addr5.''; echo ''; [/php] I hope this will helpful for you. Reply your feedback ~~SR~~

#3

Thank you for your help,
although it isn’t quite what I was after it does help me a little in other area’s. What I am trying to achieve in this part is to get a list of "$name"s in a drop down box, the other information in the file isn’t needed yet, although i will want to post it at a later stage in the form. My problem is I get multiple drop down menu’s, I only want one. I think I have probably gone about it wrong and am guessing that I might have to start the “select” process before the “foreach”. I’ll keep trying other things as i find them (google working overtime) and again thank you for the reply.
Regards
Dave


#4

Dai, drop downs are quite easy. Just remember to do it something like this:

Loop your for or while here loading each line you want in the drop-down. text you want the user to see continue your looping to fill all the option info...

Note, the VALUE inside the option is for YOUR use only, the user does not see it. I load it with a lot of various info and then use it later in the program. For instance, you can put two values in it such as an item number and value like “12-Ernie”, “13-Alex” and then use this data in your program using the explode options to separate the data. I pull data from .CSV’s and databases and store several values into this OPTION-VALUE area and then in my PHP code, I pull it apart when needed. So, in your project, you can store either the same value in it as the text for the drop down that the user sees, or you could save some other data. Whatever works for you. Hope this general info helps you… Good luck!


#5

That’s great! Thanks you guys that given me the missing link I needed, I’m (obviously!) pretty new to this and your help is really appreciated.
Regards
Dave


#6
hello dai_trying, that's the good thing that your problem is finally resolve. Thanks ErnieAlex your input. ~~SR~~

#7

I’m not sure if I should start another thread for this but as it is kinda related I’ll put it here, Moderator please move if appropriate.

I have a problem with the error checking of form data.
When I enter no data in the form It should tell me that each field is required, but the last one (field8) doesn’t show as an error, and If I fill the other fields and leave field 8 blank it processes as if no error has been received!
I have checked my code but cannot see any errors.
Any help would be appreciated.

Here is the code

(testformselect.php)
[php]

Field1<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[0]"; } echo ""; fclose($f); ?>
Field2<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[1]"; } echo ""; fclose($f); ?>
Field3<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[2]"; } echo ""; fclose($f); ?>
Field4<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[3]"; } echo ""; fclose($f); ?>
Field5<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[4]"; } echo ""; fclose($f); ?>
Field6<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[5]"; } echo ""; fclose($f); ?>
Field7<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[6]"; } echo ""; fclose($f); ?>
Field8<?php $f = fopen("testcsvfile.csv", "r"); echo ""; while (!feof($f)) { $arrM = explode(",",fgets($f)); echo "$arrM[7]"; } echo ""; fclose($f); ?>
[/php]

(selecthandler.php)

[php]

<?php if($_POST['testformselect'] == "Submit") { $errorMessage = ""; if(empty($_POST['tfs1'])) { $errorMessage .= "
  • You MUST use field1!
  • ";} if(empty($_POST['tfs2'])) { $errorMessage .= "
  • You MUST use field2!
  • ";} if(empty($_POST['tfs3'])) { $errorMessage .= "
  • You MUST use field3!
  • ";} if(empty($_POST['tfs4'])) { $errorMessage .= "
  • You MUST use field4!
  • ";} if(empty($_POST['tfs5'])) { $errorMessage .= "
  • You MUST use field5!
  • ";} if(empty($_POST['tfs6'])) { $errorMessage .= "
  • You MUST use field6!
  • ";} if(empty($_POST['tfs7'])) { $errorMessage .= "
  • You MUST use field7!
  • ";} if(empty($_POST['tfs8'])) { $errorMessage .= "
  • You MUST use field8!
  • ";} $str1 = $_POST["tfs1"]; $str2 = $_POST["tfs2"]; $str3 = $_POST["tfs3"]; $str4 = $_POST["tfs4"]; $str5 = $_POST["tfs5"]; $str6 = $_POST["tfs6"]; $str7 = $_POST["tfs7"]; $str8 = $_POST["tfs8"]; if(empty($errorMessage)) { $fs = fopen("orders","a"); fwrite($fs, $str1 . "," . $str2 . "," . $str3 . "," . $str4 . "," . $str5 . "," . $str6 . "," . $str7 . "," . $str8 . "\n"); fclose($fs); echo("

    There were no errors detected

    \n"); echo $_POST["tfs1"]; echo "
    "; echo $_POST["tfs2"]; echo "
    "; echo $_POST["tfs3"]; echo "
    "; echo $_POST["tfs4"]; echo "
    "; echo $_POST["tfs5"]; echo "
    "; echo $_POST["tfs6"]; echo "
    "; echo $_POST["tfs7"]; echo "
    "; echo $_POST["tfs8"]; echo "
    "; echo("

    This record has been added.

    \n"); } } if(!empty($errorMessage)) { echo("

    There was an error with your form:

    \n"); echo("
      " . $errorMessage . "
    \n"); echo("

    This operation failed

    \n"); } ?>

    To return click HERE

    [/php]

    #8

    Your script is working as you programmed it.
    BUT… You ended your line: if($_POST[‘testformselect’] == “Submit”)
    BEFORE testing your if error is empty…

    Normally you would do this in this format (just general code):

    if (form is posted) {
    if (empty($errorMessage)) {
    do some code like post data, whatever…
    }else{
    send error message whatever…
    } // close if-empty
    } // close form-is-posted

    I think is is just the order you are doing your if’s… BOTH the errormessage is empty and not empty should be inside the if form is posted section. (They are both from inside the form code.)
    Not sure, but, I could not see any other issues with this code. Let us know if this helps…


    #9

    Thank you ErnieAlex, I will look at setting it out better/properly and see if that solves the problem.

    Regards
    Dave