Search code examples
phploopsopencart

Custom shipping select option


I want to add a select option to my custom sipping method, I have a CSV where I have scraped all data from other URL, and want to dispay all the data in select options with while and foreach loop, but cannot get all the rows, only last one displays in select option, any assist with my code?

enter image description here

$_['text_title']       = 'LP express 24/7';

$csv = file_get_contents('https://www.lpexpress.lt/out/fck/fck_file/Terminal%C5%B3_ID_2019_01_02.csv');

file_put_contents('data.csv', str_replace(';', ' ',$csv));

$handle = fopen('data.csv', "r");

fgetcsv($handle);
fgetcsv($handle);

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    foreach($data as $value=>$title) {  
        //view the data 
        $select = '<option value = "' . $value. '">' . $title. '</option>';  
    } 
}

$_['text_description'] = '<select>' . $select . '</select>';

fclose($handle);

Solution

  • You don't use correctly fgetcsv() and the preceding processing is strange.

    Test and adapt this script :

    $csv = 'https://www.lpexpress.lt/out/fck/fck_file/Terminal%C5%B3_ID_2019_01_02.csv';
    $fp = fopen($csv, 'r');
    fgetcsv($fp); // Skip empty line
    fgetcsv($fp); // Skip header
    $html = "<select>\r\n";
    while ( ($data = fgetcsv($fp, 0, ';', '"')) !== FALSE ) {
        $html .= "<option value='$data[1]'>$data[2] -- $data[3] $data[4] $data[0]</option>\r\n";
    }
    fclose($fp);
    $html .= "</select>\r\n";
    echo $html;
    

    It gives :

    <select>
    <option value='0101'>Akropolis -- Ozo g. 25 07150 Vilnius</option>
    <option value='0102'>Maxima XXX -- Ukmergės g. 282 06115 Vilnius</option>
    <option value='0103'>Maxima XX -- Liepkalnio g. 112 02121 Vilnius</option>
    <option value='0104'>Banginis -- P. Lukšio g. 34 08235 Vilnius</option>
    <option value='0106'>Centrinis paštas -- Gedimino pr. 7 01103 Vilnius</option>
    <option value='0107'>Maxima XX -- J. Tiškevičiaus g. 22 02231 Vilnius</option>
    <option value='0108'>Panorama -- Saltoniškių g. 9 08105 Vilnius</option>
    <option value='0110'>Circle K -- Laisvės pr. 43C 05112 Vilnius</option>
    <option value='0111'>IKI Minskas -- Žirmūnų g. 2 09214 Vilnius</option>
    ...