Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/07/2023 in all areas

  1. You don't, because such a thing doesn't make sense to do. How about you describe the problem you think this will solve? Sounds like maybe what you want instead is either a sequence or an identity column.
    1 point
  2. Very similar to the last one - I'm sure you should have been able to work it out. But, as an example for others... ################################################################################ # CSV COLUMNS # ################################################################################ $main = [ 1 => 'branch_id', 3 => 'dept_id', 5 => 'grp_id', 7 => 'category_id', 9 => 'sub_id' ]; $descrips = [ 10 => 'Wear Part', // if you have ids for these items, use those instead of text descriptions 11 => 'Consumables', 12 => 'Accessories', 13 => 'Electrical', 14 => 'Other Parts', 15 => 'Pipeline', 16 => 'Mixer Drum' ]; $qtrs = [ 17 => 'Q1', 18 => 'Q2', 19 => 'Q3', 20 => 'Q4' ]; ################################################################################ # READ CSV AND CREATE DATA ARRAY # ################################################################################ $fp = fopen('test2.csv', 'r'); $hdr = fgetcsv($fp); // ignore header row $data = []; while ($line = fgetcsv($fp)) { $reca = array_intersect_key($line, $main ); $recc = array_intersect_key($line, $qtrs ); foreach (array_intersect_key($line, $descrips ) as $k => $d) { // need a record for each description with a value if ($d) { $recb = [ $descrips[$k], (empty($d) ? 0 : $d ) ]; $data[] = vsprintf("(%d,%d,%d,%d,%d,'%s',%d,%d,%d,%d,%d)", array_merge($reca, $recb, $recc)); } } } fclose($fp); ################################################################################ # WRITE RECORDS FROM ARRAY TO target TABLE # ################################################################################ $chunks = array_chunk($data, 2000); foreach ($chunks as $ch) { $pdo->exec("INSERT INTO target (branch_id, dept_id, grp_id, category_id, sub_id, description, value, Q1, Q2, Q3, Q4) VALUES ". join(',', $ch) ); }
    1 point
  3. You'll also note that my function returns a string which is then displayed by echo(). It's a subtle distinction but means that you can send that string result anywhere you want. You should retrieve the data up front and pass it to the templating "system", not the other way around. Having the templating "system" reaching out to get its own data whenever it needs it will cripple the application. You could wind up running dozens (or hundreds!) of queries where one would do just as well. The principle I'm trying to demonstrate here is that data retrieval (from the database) and creation of content (based on a "template") need to be separate functions and you use PHP code to get the data you want from one into the other. The "front-end" must be parameterised to take the data you pass it and apply those values to the template HTML. The "back-end" must retrieve the required data and put it in a form that you can pass to the "front-end". In my example, I used individual parameters, mainly for clarity. It sounds like you'd be better off passing an array, with key-value pairs containing the data. This allows the templating "system" to take whichever values it wants and use them and "ignore" any that it doesn't need. (This is the classic "XML" principle; a great idea, as long as you don't have to worry about security!). Regards, Phill W.
    1 point
This leaderboard is set to New York/GMT-05:00
×
×
  • 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.