gps similation class

PHOTO EMBED

Thu Nov 30 2023 09:37:04 GMT+0000 (Coordinated Universal Time)

Saved by @MAKEOUTHILL #html

<?php

	class GPSSimulation {
		private $conn;
		private $hst = null;
		private $db = null;
		private $usr = null;
		private $pwd = null;
		private $gpsdata = [];
			
		//////////////////////// PRIVATE
		
		private function fillDataTable() {
			$this->initializeDatabase();
			
			$add = $this->conn->prepare("INSERT INTO gpsmsg(dom, wagon, x, y) 
										       VALUES(?, ?, ?, ?)");
			
			
			// voorkom dubbele entries in de database.
			// als satelliet, datum, x en y al voorkomen in de tabel
			// kun je vaststellen dat de huifkar tijdelijk stilstaat
			// voor pauze, lunch of restaurantbezoek
			// en is een nieuwe entry niet nodig.
			$doesRecordExist = $this->conn->prepare(
				"SELECT COUNT(*) FROM gpsmsg 
			     WHERE dom = ? AND wagon = ? AND x = ? AND y = ?"
			);
			
			foreach($this->gpsdata as $ins) {
				
				list($dom, $wagon, $x, $y) = $ins;

				$doesRecordExist->execute([$dom, $wagon, $x, $y]);

				if($doesRecordExist->fetchColumn() == 0) {
					$add->execute([$dom, $wagon, $x, $y]);
				} 
			}
    	}
		
		private function initializeDatabase() {
			$this->conn->query("TRUNCATE TABLE gpsmsg");
			
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      100, 100];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      150, 150];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      230, 310];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",       80, 245];		    
			
			// test dubbelen, worden niet opgenomen in de database
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      100, 100];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      150, 150];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",      230, 310];
		    $this->gpsdata[] = ["2023-10-19", "Old Faithful",       80, 245];		    
					
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",       10,  54];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",       75, 194];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      175, 161];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      134, 280];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      300, 160];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      400, 290];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      544, 222];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      444, 122];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      321,  60];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",      200,  88];
		    $this->gpsdata[] = ["2023-10-15", "Jade Princess",       25,  25];

		    $this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",       50,  50];
		    $this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",      300, 188];
		    $this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",      225,  90];			    
			
			// test dubbelen, worden niet opgenomen in de database
			$this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",       50,  50];
		    $this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",      300, 188];
		    $this->gpsdata[] = ["2023-10-10", "Skyblue Wonder",      225,  90];		    
			
			$this->gpsdata[] = ["2023-10-05", "Red Lobster",          50,  50];
		    $this->gpsdata[] = ["2023-10-05", "Red Lobster",         190, 288];
		    $this->gpsdata[] = ["2023-10-05", "Red Lobster",         260, 122];
		    $this->gpsdata[] = ["2023-10-05", "Red Lobster",         340,  90];
		    $this->gpsdata[] = ["2023-10-05", "Red Lobster",         240,  45];
		}
		
		//////////////////////// PUBLIC

		public function __construct($phst, $pdb, $pusr, $ppwd, $refresh = false) {
			$this->hst = $phst;	// bewaar de verbindingsgegevens
			$this->db  = $pdb;
			$this->hst = $pusr;
			$this->pwd = $ppwd;
			
			$this->conn = new PDO("mysql:host=$phst;dbname=$pdb", $pusr, $ppwd);
			
			if($refresh) $this->fillDataTable();
		}
	
		public function getDataRaw($wagname = null) {
        
			$sql = "SELECT * FROM gpsmsg ";
		
			if($satnm != null) {
				$sql .= "WHERE wagon = :wag";
		
				$stmt = $this->conn->prepare($sql);
        		$stmt->execute([":wag" => $wagname]);
			} else {
				$stmt = $this->conn->query($sql);
			}

			$s = "<table border='1' cellspacing='5' cellpadding='5'>";
        	$s .= "\r<tr><td>Date</td><td>Wagon</td><td>X</td><td>Y</td><tr>";
        	while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
            	$s .= "\r<tr>"
					."<td>{$row->dom}</td>"
			  	 	."<td>{$row->wagon}</td>"
				 	."<td>{$row->x}</td>"
				 	."<td>{$row->y}</td>"
				 	."</tr>";
        	}
        	$s .= "\r</table><br>";

        	return $s;
    	}

		public function getTraject() {
	
			$stmt = $this->conn->query("SELECT * FROM gpsmsg ");

			$dta = [];
        	while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
				$dta[] = [ 
					"dom"   => $row->dom, 
					"wagon" => $row->wagon, 
					"x"     => $row->x, 
					"y"     => $row->y 
				];
        	}

			return $dta;
    	}

		public function createSelectbox() {
		
			$stmt = $this->conn->query("SELECT DISTINCT wagon FROM gpsmsg");
			$s = "<div id='pleaseChooseWagon'><strong>Wagon</strong>";
			$s .= "<select name='selWagon' id='selWagon' "
			   ."onchange='getWagonSelected(this)'>";
			$s .= "<option value='0'>-- choose wagon --</option>";

			while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
				$s .= "<option value='{$row->wagon}'>{$row->wagon}</option>";
			}
			$s .= "</select></div>";

			return $s;
		}

	}  // einde class GPSSimulation
content_copyCOPY