-2

Hi I have a txt file like this:

lenovo,pc,mouse
mac,pc,mouse
dell,pc,mouse

and I want to make a drop down list of first part of txt file string (pc name). Here is my code but it doesnt work how I want. It doesnt explode it. Any help?

echo "Which pc are you using?? <br>";   
$pc = file('pc.txt');
$name = ' ';
$name.="<option>Choose please</option>";
foreach ($pc as $type) { 
    $name .= '<option value="'.$type.'">'.$type.'</option>';
explode(',',$name);}
$select = '<select name="pc">'.$name.'</select>';
echo $select;
copy
  • 19
  • 5
  • 1
    What do you mean by 'explode' it? What are you expecting as your output and what are you getting instead? – jbx Apr 03 '19 at 15:05
  • this code gives me in my option list - lenovo,pc,mouse I need only lenovo. I know what i need to explode it but i dont know how to do it – copy Apr 03 '19 at 15:08
  • Your `$name` variable contains all of the options for the `select` you are trying to build. What are you trying to accomplish by trying to explode that string? – Dave Apr 03 '19 at 15:11

2 Answers2

0

you must explode $type

echo "Which pc are you using?? <br>";   
$pc = file('pc.txt');
$name = ' ';
$name.="<option>Choose please</option>";
foreach ($pc as $type) { 
    $pc_name = explode(',',$type);
    $name .= '<option value="'.$pc_name[0].'">'.$pc_name[0].'</option>';
    // explode(',',$name);
}
$select = '<select name="pc">'.$name.'</select>';
echo $select;
Goe Roe Ku
  • 14
  • 1
  • 3
0

Break up the lines from the file inside of the loop where you are creating the options.

echo "Which pc are you using?? <br>";   
$pc = file('pc.txt');
$name = ' ';
$name.="<option>Choose please</option>";
foreach ($pc as $type) { 
    $type = explode(',',$name);
    $name .= '<option value="'.$type[0] . '">' . $type[0] . '</option>';
}
$select = '<select name="pc">'.$name.'</select>';
echo $select;
Dave
  • 5,108
  • 16
  • 30
  • 40