Jump to content

Barand

Moderators
  • Posts

    22,403
  • Joined

  • Last visited

  • Days Won

    618

Barand last won the day on November 30

Barand had the most liked content!

About Barand

Profile Information

Recent Profile Visitors

68,873 profile views

Barand's Achievements

Prolific Member

Prolific Member (5/5)

1.8k

Reputation

254

Community Answers

  1. Wow! So many "Deadly Sins" in a single block of code. Thou shouldst not use global Thou shouldst not run queries inside loops Thou shouldst not split data of the same entity type across multiple tables Thou shouldst not use "SELECT * " Why don't you use fetch _assoc instead of fetch row, then the loop becomes simply while ($rowtime = $result_time->fetch_assoc()) { $temparray[] = $rowtime; }
  2. I guess it's incomplete documentation. DateTime is full of surprises. One I found to be useful is setting a dateInterval to "next weekday" to get a list of N working days $dt = new DateTime('next monday'); $di = DateInterval::createFromDateString('next weekday'); $dp = new DatePeriod($dt, $di, 9); foreach ($dp as $d) { echo $d->format('D M jS').'<br>'; } gives Mon Dec 6th Tue Dec 7th Wed Dec 8th Thu Dec 9th Fri Dec 10th Mon Dec 13th Tue Dec 14th Wed Dec 15th Thu Dec 16th Fri Dec 17th
  3. There is a compromise solution using file() and that is to use str_getcsv() instead of explode(). $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $array = imp_open('Medikamente.csv'); foreach ($array as $line) { $rec = str_getcsv($line); $data[] = array_combine($heads, $rec); } foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); return $content; }
  4. Yes, you could, but your explode above isn't giving the array that you need. You would still have to loop through your array lines and remove the newline from the end of each line explode each individual line trim off the quotes $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $array = imp_open('Medikamente.csv'); foreach ($array as $line) { $rec = explode(',', $line); $rec = array_map(function($v) { return trim($v, ' "'); }, $rec); $data[] = array_combine($heads, $rec); } foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); return $content; } When php provides a function specifically for handling csv data and does all that for you, why not use it?
  5. Then your csv data is not as you posted, with 4 items of data in each row. I used the data you gave Medikamente.csv... 9999900001,"zebra","Noropr","159" 9999900002,"coco1","Noropr","999998" 9999900003,"coco12","Noropr","78" 99999000099999,"coco1123","Noropr","33" 9999900005,"coco198","Noropr","79" 9999900006,"coco111","Noropr","66" 9999900007,"coco1456","NoroprNoropr","2999996" 9999900008,"coco1sss","Salbe","55" 9999900009,"coco90","Salbe","90" 9999900010,"coco111111","Tabletten","102" 9999900011,"coco178989","Noropr","999998" 9999900012,"coco18283838383","Noropr","59" 9999900013,"coco17874738774","Tabletten","899999" 99999000199999,"Tannosynt","Salbe","71" 9999900015,"Vomex A","Noropr","699999" 9999900016,"Vomex A","Noropr","35" ... and my results were etc.
  6. try <?php $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $csv = fopen('Medikamente.csv', 'r'); while ($line = fgetcsv($csv)) { $data[] = array_combine($heads, $line); } fclose($csv); foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <style type='text/css'> label { display: inline-block; width: 120px; background-color: #E0E0E0; color: black; padding: 8px; border: 1px solid white; } </style> </head> <body> <?= $data_list ?> </body> </html>
  7. You could try something like this example <?php if (isset($_GET['ajax'])) { if (rand(0,1)) { exit("Bar Code Incorrect"); } else { exit("Everything OK"); } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#test").click( function() { $.get( "", {"ajax":1}, function(resp) { if (resp=="Bar Code Incorrect") { let err = $("<div>", {"id":"error", "class":"alert alert-warning", "html":resp}) $("#output").html(err) $("#error").fadeOut(4000) } else { $("#output").html(resp) } } ) }) }) </script> <style type='text/css'> .alert { background-color: red; color: white; padding: 16px; } </style> </head> <body> <button id='test'>Test</button> <div id='output'></div> </body> </html>
  8. Before you go any further you should nomalize those skills and not have a comma-separated list. EG +--------------+ +--------------+ | person | | skill | +--------------+ +--------------+ | person_id PK |--+ +--| skill_id PK | | name | | | | skill_name | | etc | | | +--------------+ +--------------+ | | | +-----------------+ | | | person_skill | | | +-----------------+ | +----<| person_id PK | | | skill_id PK |>--+ | date_achieved | +-----------------+ In the SQL tutorial in my signature there is a similar example application which uses pupils/subects rather than person/skills
  9. Did you you mean something like this? $data = [ 'Hot' => [ 'A' => 5, 'B' => 20, 'C' => 15, 'D' => 10, 'E' => 8 ], 'Dead' => [ 'A' => 15, 'B' => 30, 'F' => 55, 'C' => 40, 'G' => 60, ] ]; echo '<pre> BEFORE ' . print_r($data, 1) . '</pre>'; // original array // // GET ALL KEYS // $keys = []; foreach ($data as $k1 => $v1) { $keys = array_merge($keys, array_keys($v1)); } $keys = array_unique($keys); $blank_values = array_fill_keys($keys, null); // // INSERT MISSING KEYS INTO THE SUBARRAYS // foreach ($data as $k => &$subarray) { $subarray = array_merge($blank_values, $subarray); } echo '<pre> AFTER ' . print_r($data, 1) . '</pre>'; // array after processing which gives... BEFORE Array ( [Hot] => Array ( [A] => 5 [B] => 20 [C] => 15 [D] => 10 [E] => 8 ) [Dead] => Array ( [A] => 15 [B] => 30 [F] => 55 [C] => 40 [G] => 60 ) ) AFTER Array ( [Hot] => Array ( [A] => 5 [B] => 20 [C] => 15 [D] => 10 [E] => 8 [F] => [G] => ) [Dead] => Array ( [A] => 15 [B] => 30 [C] => 40 [D] => [E] => [F] => 55 [G] => 60 ) )
  10. https://www.php.net/manual/en/features.file-upload.post-method.php
  11. Then move all the processing to the same place - one or the other
  12. Erm! @gizmola Note that in the English speaking parts of the world we have "speciality" and not "specialty" 😀
×
×
  • 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.