bulk_update.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. include 'connect.php';
  3. if(! (isset($_FILES['routesfile']) && isset($_FILES['paddlesfile'])) )
  4. {
  5. $okay=false;
  6. }
  7. else
  8. {
  9. $okay=true;
  10. }
  11. if(!$okay)
  12. {
  13. echo 'Route spreadsheet should have the following format:';
  14. echo '<table border="1"><tr><th>A</th><th>B</th><th>C</th><th>...</th></tr>
  15. <tr><td>Route#</td><td>Stop 1 Name</td><td>Stop 2 Name</td><td>...</td></tr>
  16. <tr><td colspan="4"><center>...</center></td></tr></table><br>';
  17. echo 'Paddle spreadsheet should have the following format:';
  18. echo '<table border="1"><tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th><th>G</th><th>...</th></tr>
  19. <tr><td>Paddle#</td><td>Order#</td><td>Block#</td><td>Route #</td><td>Trip #</td><td>Stop 1 Time</td><td>Stop 2 Time</td><td>...</td></tr>
  20. <tr><td colspan="8"><center>...</center></td></tr></table><br>';
  21. echo 'It should be noted that Order# and Block# are ignored, it is assumed that the spreadsheet is sorted.<br>';
  22. echo '
  23. <form enctype="multipart/form-data" action="bulk_update.php" method="POST">
  24. <input type="hidden" name="MAX_FILE_SIZE" value="400000" />
  25. Upload routes file (.csv): <input name="routesfile" type="file" accept="text/csv,application/csv" /><br />
  26. Upload paddles file (.csv): <input name="paddlesfile" type="file" accept="text/csv,application/csv" /><br />
  27. <input type="submit" value="Upload Files" />
  28. </form>';
  29. exit;
  30. }
  31. $routes=array();
  32. $stops=array();
  33. $res=mysqli_query($sql, "SELECT name,id FROM stops");
  34. while($row=mysqli_fetch_assoc($res))
  35. {
  36. $stops[$row['name']]=$row['id'];
  37. }
  38. //print_r($_FILES);
  39. $f=fopen($_FILES['routesfile']['tmp_name'],"rb");
  40. while($row=fgetcsv($f,1024))
  41. {
  42. $num=intval($row[0]);
  43. if($num > 0)
  44. {
  45. $idx=0;
  46. $tmp=array();
  47. while(strlen($row[$idx + 1]) > 0)
  48. {
  49. $tmp[$idx]=trim($row[$idx + 1]);
  50. $idx++;
  51. }
  52. $routes[$num]=$tmp;
  53. }
  54. }
  55. fclose($f);
  56. // echo "<pre>";
  57. // print_r($routes);
  58. // echo "</pre>";
  59. $paddles=array();
  60. $f=fopen($_FILES['paddlesfile']['tmp_name'],"rb");
  61. while($row=fgetcsv($f,1024))
  62. {
  63. $pnum=intval($row[0]);
  64. if($pnum > 0)
  65. {
  66. $rt=intval($row[3]);
  67. $tp=intval($row[4]);
  68. if(isset($routes[$rt]))
  69. {
  70. $n=count($routes[$rt]);
  71. for($i=0;$i < $n; $i++)
  72. {
  73. $time=$row[$i + 5];
  74. if(isset($stops[$routes[$rt][$i]]))
  75. {
  76. $stpid=$stops[$routes[$rt][$i]];
  77. }
  78. else
  79. {
  80. $stpid=0;
  81. $err=$routes[$rt][$i];
  82. $x=$i+1;
  83. echo "Ignoring Unknown stop \"$err\" (Work Run $pnum Route $rt Trip $tp Stop $x)<br>";
  84. continue;
  85. }
  86. if(strlen(trim($time)) > 0)
  87. {
  88. $paddles[$pnum][]=array( 'id' => $pnum, 'arrival' => $time, 'route' => $rt, 'trip' => $tp, 'stop' => ($i + 1), 'stage' => ($i + 1), 'stopid' => $stpid);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. fclose($f);
  95. //echo '<pre>';
  96. //print_r($paddles);
  97. //echo '</pre>';
  98. foreach($paddles as $pnum => $paddle)
  99. {
  100. mysqli_query($sql,"DELETE FROM paddles WHERE id='$pnum'");
  101. foreach($paddle as $slot => $record)
  102. {
  103. $arr=$record['arrival'];
  104. $rt=$record['route'];
  105. $tp=$record['trip'];
  106. $stp=$record['stop'];
  107. $stg=$record['stage'];
  108. $stpid=$record['stopid'];
  109. mysqli_query($sql,"INSERT INTO paddles SET id='$pnum', slot='$slot', arrival='$arr', route='$rt', trip='$tp', stage='$stg', stop='$stp', stopid='$stpid'");
  110. }
  111. }
  112. echo "Done!<br>";
  113. echo '<a href="index.php?goto=mainmenu">Return to menu</a>';
  114. ?>