D. 777KLM
D. 777KLM

Reputation: 470

Laravel - Create Separate Row for Data from txt file for mySQL database

I am having an problem with this. Say, for example I these lines of data:

QZS-2 (QZSS/PRN 184)    
1 42738U 17028A   17258.40853294 -.00000184  00000-0  00000-0 0  9994
2 42738  44.6467 288.3531 0740253 270.7360  80.8515  1.00267707  1090

These lines give some details about an orbit of an satellite. Line 0 is the name while lines 1 and 2 are orbital parameters.

So far I have managed to save these lines to the mySQL database using this:

public function displayer() {
    $url = 'http://celestrak.com/NORAD/elements/sbas.txt';
    $lines = file($url, FILE_IGNORE_NEW_LINES);
    $arrays = array_map(function($array) {
        $columns = ['object_name', 'tle_line1', 'tle_line2'];
        return array_combine($columns, array_map('trim', $array));
    }, array_chunk($lines, 3));
    DB::table('tester')->insert($arrays);
    dd($arrays);
}

This works great and manages to create the 3 lines I need. Now for the problem I am trying to solve:

I am trying to create a separate row for the five digit number (also called the norad_cat_id). So for example:

2 42738 44.6467 288.3531 0740253 270.7360 80.8515 1.00267707 1090 creates a row with the number 42738. So each satellite should have 4 columns of data each: the three lines already present and now the new norad_cat_id five digit number. The data follows a standard: the line where the norad_cat_id is present always has a 2 at the start of it and a then a space.

Sometimes the norad_cat_id can have a zero at the start of number: for example: 08513. Would it be possible just to save 8513 to the database without the zero?

TL;DR

EDIT: Maybe something can be done with this (I found this on an another answer):

$splitName = explode(' ', $name, 2); 
$first_name = $splitName[0];
$last_name = !empty($splitName[1]) ? $splitName[1] : '';

This splits first and last names. Maybe something can be derived from this?

Upvotes: 1

Views: 1072

Answers (1)

The Alpha
The Alpha

Reputation: 146219

This is your modified code which will create each row using four columns:

public function displayer()
{
    $url = 'http://celestrak.com/NORAD/elements/sbas.txt';
    $lines = file($url, FILE_IGNORE_NEW_LINES);
    $arrays = array_map(function($array) {
        $columns = ['object_name', 'tle_line1', 'tle_line2'];
        $row = array_combine($columns, array_map('trim', $array));

        // New line added
        return array_merge($row, [
            'norad_cat_id' => (int) explode(' ', end($row), 3)[1]
        ]);

    }, array_chunk($lines, 3));

    DB::table('tester')->insert($arrays);
}

Upvotes: 1

Related Questions