Jump to content

Barand

Moderators
  • Posts

    24,605
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. Test if it's a student nnumber or "N/A"? if (ctype_digit($sdata[0]) || $sdata[0]=='N/A') { $res[$kc]['students'][] = $sdata; } If you want those with "AAVL", "CBSE" etc as well, then if (!empty($sdata[0]) && $sdata[0] != ' ') { $res[$kc]['students'][] = $sdata; }
  2. Try this $html = file_get_contents('rohan.html'); // the table html $ranges= [[ 'start' => [ 'r'=>5, 'c'=>0 ], // DATES 'end' => [ 'r'=>5, 'c'=>21 ] ], [ 'start' => [ 'r'=>6, 'c'=>0 ], // specify block of rows/cols 'end' => [ 'r'=>11, 'c'=>21 ] // ( top left is r=0 c=0 ) ], [ 'start' => [ 'r'=>13, 'c'=>0 ], 'end' => [ 'r'=>18, 'c'=>21 ] ], [ 'start' => [ 'r'=>19, 'c'=>0 ], 'end' => [ 'r'=>24, 'c'=>21 ] ], [ 'start' => [ 'r'=>25, 'c'=>0 ], 'end' => [ 'r'=>30, 'c'=>21 ] ]]; foreach ($ranges as $range) { $results[] = getColumns($html, $range); } $results_by_date = getResultsByDate($results); function getResultsByDate($results) { $res = []; foreach ($results[0] as $kc => $date) { $res[$kc] = [ 'date' => $date[0], 'students' => [] ]; } for ($i=1; $i<=4; $i++) { foreach ( $results[$i] as $kc => $sdata) { if (ctype_digit($sdata[0])) { // does it lok like a student number? $res[$kc]['students'][] = $sdata; } } } // remove dates with no students $res = array_filter($res, function($v) { return !empty($v['students']); }); return $res; } function getColumns(&$html, $range) { $rows = []; $kr = 0; $p1 = 0; // find first row in out range for ($r=0; $r<=$range['start']['r']; $r++) { $p1 = strpos($html, '<tr', $p1); ++$p1; } $p1--; for ($kr=$range['start']['r']; $kr<=$range['end']['r']; $kr++) { $rows[$kr] = getCells($html, $range, $p1); $p1 = strpos($html, '<tr', $p1+1); } $cols = []; for ($kc=$range['start']['c']; $kc<=$range['end']['c']; $kc++) { $cols[] = array_column($rows, $kc); } return $cols; } function getCells(&$html, $range, $p1) { $cells = []; for ($kc=$range['start']['c']; $kc<=$range['end']['c']; $kc++) { $p1 = strpos($html, '<td', $p1+1); $p1 = strpos($html, '>', $p1+1); $p2 = strpos($html, '<td', $p1); $cells[$kc] = trim(strip_tags(substr($html, $p1+1, $p2-$p1-1))); } return $cells; } echo '<pre>' . print_r($results_by_date, 1) . '</pre>'; gives (21 students) Array ( [2] => Array ( [date] => May31Thu [students] => Array ( [0] => Array ( [0] => 8751 [1] => 03:55 [2] => 04:55 [3] => WFH [4] => COMP AVL [5] => 08:00 ) [1] => Array ( [0] => 8752 [1] => 08:35 [2] => COMP AVL [3] => WFH [4] => 11:55 [5] => 12:25 ) ) ) [3] => Array ( [date] => Jun01Fri [students] => Array ( [0] => Array ( [0] => 8462 [1] => 04:30 [2] => 05:30 [3] => WFH [4] => COMP NOT AVL [5] => 07:10 ) [1] => Array ( [0] => 8465 [1] => 07:45 [2] => COMP NOT AVL [3] => WFH [4] => 09:20 [5] => 09:50 ) ) ) [10] => Array ( [date] => Jun08Fri [students] => Array ( [0] => Array ( [0] => 8113 [1] => 05:05 [2] => 06:05 [3] => WFH [4] => ZRH [5] => 07:50 ) [1] => Array ( [0] => 8114 [1] => 08:25 [2] => ZRH [3] => WFH [4] => 10:10 [5] => ) [2] => Array ( [0] => 8277 [1] => 11:05 [2] => WFH [3] => MAD [4] => 13:40 [5] => 14:10 ) ) ) [11] => Array ( [date] => Jun09Sat [students] => Array ( [0] => Array ( [0] => 8274 [1] => 04:00 [2] => 05:00 [3] => MAD [4] => WFH [5] => 07:25 ) [1] => Array ( [0] => 8221 [1] => 08:10 [2] => WFH [3] => VLC [4] => 10:30 [5] => ) [2] => Array ( [0] => 8222 [1] => 11:05 [2] => VLC [3] => WFH [4] => 14:00 [5] => 14:30 ) ) ) [15] => Array ( [date] => Jun13Wed [students] => Array ( [0] => Array ( [0] => 8973 [1] => 04:05 [2] => 05:05 [3] => WFH [4] => SOF [5] => 08:05 ) [1] => Array ( [0] => 8974 [1] => 08:50 [2] => SOF [3] => WFH [4] => 12:10 [5] => 12:40 ) ) ) [17] => Array ( [date] => Jun15Fri [students] => Array ( [0] => Array ( [0] => 8233 [1] => 12:25 [2] => 13:25 [3] => WFH [4] => SSP [5] => 15:40 ) [1] => Array ( [0] => 8237 [1] => 16:10 [2] => SSP [3] => WFH [4] => 18:25 [5] => 18:55 ) ) ) [20] => Array ( [date] => Jun18Mon [students] => Array ( [0] => Array ( [0] => 807 [1] => 11:35 [2] => 12:35 [3] => WFH [4] => OMV [5] => 14:10 ) [1] => Array ( [0] => 808 [1] => 14:35 [2] => OMV [3] => WFH [4] => 16:15 [5] => ) [2] => Array ( [0] => 837 [1] => 16:50 [2] => WFH [3] => BFS [4] => 18:25 [5] => ) [3] => Array ( [0] => 840 [1] => 18:55 [2] => BFS [3] => WFH [4] => 20:25 [5] => 20:55 ) ) ) [21] => Array ( [date] => Jun19Tue [students] => Array ( [0] => Array ( [0] => 8551 [1] => 10:50 [2] => 11:50 [3] => WFH [4] => MJV [5] => 14:30 ) [1] => Array ( [0] => 8552 [1] => 15:00 [2] => [3] => WFH [4] => 17:40 [5] => ) [2] => Array ( [0] => 8187 [1] => 18:55 [2] => WFH [3] => LIN [4] => 20:50 [5] => 21:20 ) ) ) )
  3. You will find it easier to process the results and remove the unwanted (empty) sub-arrays. Also, only the first range contains the dates, so you you might want to grab the dates separately from the studunts' data then they can be applied to all ranges. These ranges grab the dates in $results[0] and the student data in $results[1] - [4] $ranges= [[ 'start' => [ 'r'=>5, 'c'=>0 ], // DATES 'end' => [ 'r'=>5, 'c'=>21 ] ], [ 'start' => [ 'r'=>6, 'c'=>0 ], // specify block of rows/cols 'end' => [ 'r'=>11, 'c'=>21 ] // ( top left is r=0 c=0 ) ], [ 'start' => [ 'r'=>13, 'c'=>0 ], 'end' => [ 'r'=>18, 'c'=>21 ] ], [ 'start' => [ 'r'=>19, 'c'=>0 ], 'end' => [ 'r'=>24, 'c'=>21 ] ], [ 'start' => [ 'r'=>25, 'c'=>0 ], 'end' => [ 'r'=>30, 'c'=>21 ] ]];
  4. You could extend the range of rows. But there is no way with that data to now know for sure where a new student starts in each column. If you know a way, build it into the process. The alternative is to repeat the process for each of the blocks... $ranges= [[ 'start' => [ 'r'=>5, 'c'=>0 ], // specify block of rows/cols 'end' => [ 'r'=>11, 'c'=>21 ] // ( top left is r=0 c=0 ) ], [ 'start' => [ 'r'=>13, 'c'=>0 ], 'end' => [ 'r'=>18, 'c'=>21 ] ], [ 'start' => [ 'r'=>19, 'c'=>0 ], 'end' => [ 'r'=>24, 'c'=>21 ] ], [ 'start' => [ 'r'=>25, 'c'=>0 ], 'end' => [ 'r'=>30, 'c'=>21 ] ]]; foreach ($ranges as $range) { $results[] = getColumns($html, $range); } echo '<pre>' . print_r($results, 1) . '</pre>';
  5. It deserves recognition
  6. OK. Forget the "simple" classes (or any other) exist and go back to basics. This allows you specify a block of cells (EG row 5, col 0 to row 12, col 21) and produces an array of the contents of each column... Array ( [0] => Array ( [0] => May29Tue [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => May30Wed [1] => AAVL [2] => 06:00 [3] => 14:00 [4] => [5] => [6] => ) [2] => Array ( [0] => May31Thu [1] => 8751 [2] => 03:55 [3] => 04:55 [4] => WFH [5] => COMP AVL [6] => 08:00 ) [3] => Array ( [0] => Jun01Fri [1] => 8462 [2] => 04:30 [3] => 05:30 [4] => WFH [5] => COMP NOT AVL [6] => 07:10 ) [4] => Array ( [0] => Jun02Sat [1] => CBSE [2] => 02:00 [3] => 10:00 [4] => [5] => [6] => ) [5] => Array ( [0] => Jun03Sun [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Jun04Mon [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Jun05Tue [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Jun06Wed [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Jun07Thu [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Jun08Fri [1] => 8113 [2] => 05:05 [3] => 06:05 [4] => WFH [5] => ZRH [6] => 07:50 ) [11] => Array ( [0] => Jun09Sat [1] => 8274 [2] => 04:00 [3] => 05:00 [4] => MAD [5] => WFH [6] => 07:25 ) [12] => Array ( [0] => Jun10Sun [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [13] => Array ( [0] => Jun11Mon [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [14] => Array ( [0] => Jun12Tue [1] => AAVL [2] => 05:15 [3] => 13:15 [4] => [5] => [6] => ) [15] => Array ( [0] => Jun13Wed [1] => 8973 [2] => 04:05 [3] => 05:05 [4] => WFH [5] => SOF [6] => 08:05 ) [16] => Array ( [0] => Jun14Thu [1] => ADTY [2] => 09:30 [3] => 16:30 [4] => [5] => [6] => ) [17] => Array ( [0] => Jun15Fri [1] => 8233 [2] => 12:25 [3] => 13:25 [4] => WFH [5] => SSP [6] => 15:40 ) [18] => Array ( [0] => Jun16Sat [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [19] => Array ( [0] => Jun17Sun [1] => N/A [2] => [3] => [4] => [5] => [6] => ) [20] => Array ( [0] => Jun18Mon [1] => 807 [2] => 11:35 [3] => 12:35 [4] => WFH [5] => OMV [6] => 14:10 ) [21] => Array ( [0] => Jun19Tue [1] => 8551 [2] => 10:50 [3] => 11:50 [4] => WFH [5] => MJV [6] => 14:30 ) ) Code $html = file_get_contents('rohan.html'); // the table html $range = [ 'start' => [ 'r'=>5, 'c'=>0 ], // specify block of rows/cols 'end' => [ 'r'=>11, 'c'=>21 ] // ( top left is r=0 c=0 ) ]; $results = getColumns($html, $range); function getColumns(&$html, $range) { $rows = []; $kr = 0; $p1 = 0; // find first row in our range for ($r=0; $r<=$range['start']['r']; $r++) { $p1 = strpos($html, '<tr', $p1); ++$p1; } $p1--; for ($kr=$range['start']['r']; $kr<=$range['end']['r']; $kr++) { $rows[$kr] = getCells($html, $range, $p1); $p1 = strpos($html, '<tr', $p1+1); } $cols = []; for ($kc=$range['start']['c']; $kc<=$range['end']['c']; $kc++) { $cols[] = array_column($rows, $kc); } return $cols; } function getCells(&$html, $range, $p1) { $cells = []; for ($kc=$range['start']['c']; $kc<=$range['end']['c']; $kc++) { $p1 = strpos($html, '<td', $p1+1); $p1 = strpos($html, '>', $p1+1); $p2 = strpos($html, '<td', $p1); $cells[$kc] = trim(strip_tags(substr($html, $p1+1, $p2-$p1-1))); } return $cells; }
  7. I've tried simple_html_dom and simplexml and, unsurprisingly, that table defeats all three of us.
  8. Aren't there 3 students in that column?
  9. More likely the coder. mysql_errno() does not exist. It should be mysqli_errno($conn) Try turning error reporting on occasionally.
  10. That is very true. No labelling of the rows, so it's like trying to navigate around a strange city where all the street names have been removed. You don't know if you're looking at a leaving time or the time of the next race at Cheltenham. There doesn't appear to be any consistent pattern below each row of student numbers so without row labels we're f****d. Good luck!
  11. Congratulations. Was someone on commission for every non-breaking space they used in the table's data? But back to the probem - what are you trying to extract from the table?
  12. If your form's action is "this script", just leave out the action attribute altogether. <form method='POST' > . . . </form>
  13. That's a start. But it will leave you trying to insert nothing into your table. Good spot. I didn't even notice he doesn't have a form to submit.
  14. Your only input that has a name is "item", so the others won't appear in the POST array. You also need to check that data has been posted before attempting to access them if ($_SERVER['REQUEST_METHOD'= == 'POST) { // process posted data }
  15. Neither did I until you said you needed them grouped together.
  16. Then change the code. I've given you a start. [EDIT] What the hell! $xml = simplexml_load_string($html); $trs = $xml->xpath("//tr[@class='hscore' or @class='vscore']"); $i = 1; foreach ($trs as $tr) { $tr->td[0] = "#$i ".$tr->td[0]; ++$i; } echo '<pre>' . htmlentities($xml->asXML()) . '<?pre>';
  17. Use xpath. $xml = simplexml_load_string($html); $trs = $xml->xpath("//tr[@class='hscore']"); $i = 1; foreach ($trs as $tr) { $tr->td[0] = "#$i ".$tr->td[0]; ++$i; } $trs = $xml->xpath("//tr[@class='vscore']"); $i = 1; foreach ($trs as $tr) { $tr->td[0] = "#$i ".$tr->td[0]; ++$i; } echo '<pre>' . htmlentities($xml->asXML()) . '<?pre>'; gives <?xml version="1.0"?> <table border="1" style="width:100%;" cellspacing="0" cellpadding="3"> <tr class="stats-section"> <td colspan="99">Scoring</td> </tr> <tr class="stats-section"> <td colspan="99">2nd Period</td> </tr> <tr class="hscore"> <td>#1 UMD</td> <td>4&#xD7;4</td> <td> Kobe Roth (1)</td> <td> Noah Cates, Casey Gilling </td> <td align="right">12:35</td> </tr> <tr class="vscore"> <td>#1 BSU</td> <td>4&#xD7;4</td> <td> Alex Ierullo (1)</td> <td> Kyle Looft </td> <td align="right">13:06</td> </tr> <tr class="stats-section"> <td colspan="99">3rd Period</td> </tr> <tr class="hscore"> <td>#2 UMD</td> <td/> <td> Blake Biondi (1)</td> <td> Quinn Olson </td> <td align="right">10:10</td> </tr> </table>
  18. You almost had it fruits = ["apple", "orange", "cherry"]; for (x = 0; x< fruits.length; x++) { document.getElementById("demo2").innerHTML += fruits[x] + "<br>"; } or fruits = ["apple", "orange", "cherry"]; $.each(fruits, function(k,v) { $("#demo").append(v + "<br>") })
  19. Your input form has a mass of (unnecessary) hidden inputs for just about eveything but the user_id.
  20. What is HTML markup for that page? Then we can get an idea of what you are trying to do. Do you realize you can only use an id once - they have to be unique? The first time throught the loop will put apple into demo. The second time will overwrite apple with orange. Finallly you will finish with just cherry in the demo element.
  21. Good - that's what I told you to use 16 hours and many posts ago
  22. Here's what your code is attempting to do. Does it look anything like what you intended? fruits = ["apple", "orange", "cherry"]; // define an array of fruits x = document.getElementById("myList").innerHTML; // get text content from mylist and put in x // (I have no idea how this relates to rest of the code) for (x = 0; x< fruits.length; x++) { // now use x as a counter to loop throught fruits array // (whatever you put in x above is now replaced by the counter values) document.getElementById("demo").innerHTML = x // each time through the loop put the value of x (0,1,2) into demo // so, at the end of the loop, demo should have value of 2. }
  23. try class DB { protected static $con; public static function getConnection(){ try{ self::$con = new PDO( 'mysql: host= localhost; dbname=testdb', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); } catch (PDOException $e) { echo "Could not connect to database."; exit; } //Returns Writeable db connection return self::$con; } } $conn = DB::getConnection();
×
×
  • 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.