0% found this document useful (0 votes)
7 views

Listing Program

The document discusses PHP programs stored on a server for CRUD processes on two applications. It includes programs for database connection and Floyd–Warshall algorithm to find shortest paths in a graph, as well as functions for getting paths and distances.

Uploaded by

riskifitri0303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Listing Program

The document discusses PHP programs stored on a server for CRUD processes on two applications. It includes programs for database connection and Floyd–Warshall algorithm to find shortest paths in a graph, as well as functions for getting paths and distances.

Uploaded by

riskifitri0303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Karena terdapat 2 aplikasi jadi berikut ini akan dijelaskan mengenai program php yang disimpan

dalam directory server untuk proses CRUD pada masing – masing aplikasi.

Aplikasi Petugas

A. Pada menu Membuat Rute


Program – program dibawah ini terletak dalam satu file php yaitu fw.php
 Program untuk koneksi
<?php
$konek = null;
$connection = null;
try{
//config
$host = "localhost";
$username = "innerdia";
$password = "fisika123";
$dbname = "fitria";

//connect
$database = "mysql:dbname=$dbname;host=$host";
$connection = new PDO($database, $username, $password);
$connection ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$konek = mysqli_connect($host,$username,$password,$dbname) or die("Database
MySQL tidak terhubung");

} catch(PDOException $e){
echo "Error ! " . $e->getMessage();
die;
}

?>

 Program algoritma
<?php
error_reporting(E_STRICT);
define('INFINITE', pow(2, (20 * 8 - 2)-1));
class FloydWarshall {

/**
* Distances array
* @var array
*/
private $dist = array(array());
/**
* Predecessor array
* @var array
*/
private $pred = array(array());
/**
* Weights array
* @var array
*/
private $weights;
/**
* Number of nodes
* @var integer
*/
private $nodes;
/**
* Node names array
* @var array
*/
private $nodenames;
/**
* Temporary table for various stuff.
* @var array
*/
private $tmp = array();

/**
* Constructor
* @param array $graph Graph matrice.
* @param array $nodenames Node names as an array.
*/
public function __construct($graph, $nodenames='') {

$this->weights = $graph;
$this->nodes = count($this->weights);
if ( ! empty($nodenames) && $this->nodes == count($nodenames) ) {
$this->nodenames = $nodenames;
}
$this->__floydwarshall();
}
/**
* The actual PHP implementation of Floyd-Warshall algorithm.
* @return void
*/
private function __floydwarshall () {

// Initialization
for ( $i = 0; $i < $this->nodes; $i++ ) {
for ( $j = 0; $j < $this->nodes; $j++ ) {
if ( $i == $j ) {
$this->dist[$i][$j] = 0;
} else if ( (float)$this->weights[$i][$j] > 0 ) {
$this->dist[$i][$j] = (float) $this->weights[$i][$j];
} else {
$this->dist[$i][$j] = INFINITE;
}
$this->pred[$i][$j] = $i;
}
}

// Algorithm
for ( $k = 0; $k < $this->nodes; $k++ ) {
$a=$k+1;
//echo '<strong>K = '.$a.'</strong><br />';
//echo '<table border="1" cellpadding="4" cellspacing="0">';
if (!empty($this->nodenames) ) {
//echo '<tr>';
//echo '<td>&nbsp;</td>';
for ($n = 0; $n < $this->nodes; $n++) {
if($n == $k) {
//echo '<td width="15" align="center" style="background-
color:#6789AA"><strong>' .
//$this->nodenames[$n] .
//'</strong></td>';
} else {
//echo '<td width="15" align="center" ><strong>' .
//$this->nodenames[$n] .
//'</strong></td>';
}
}
}
//echo '</tr>';
for ( $i = 0; $i < $this->nodes; $i++ ) {
if($i == $k) {
//echo '<tr height="50" bgcolor="#6789AAA">';
} else {
//echo '<tr>';
}
//echo '<td width="15" align="center"><strong>' .
//$this->nodenames[$i] .
//'</strong></td>';

for ( $j = 0; $j < $this->nodes; $j++ ) {


if ($this->dist[$i][$j] > ((float)$this->dist[$i][$k] + (float)$this-
>dist[$k][$j])) {
$this->dist[$i][$j] = (float)$this->dist[$i][$k] + (float)$this-
>dist[$k][$j];
$this->pred[$i][$j] = $this->pred[$k][$j];
if($j == $k) {
//echo '<td width="50" align="center" style="background-
color:#6789AA">' .
//$this->dist[$i][$j] .'</td>';
} else {
//echo '<td width="15" align="center"><strong>' .
//$this->dist[$i][$j] .'</strong></td>';
}
} else {
if($j == $k) {
//echo '<td width="50" align="center" style="background-
color:#6789AA">' .
//$this->weights[$i][$j] .'</td>';
} else {
//echo '<td width="15" align="center">' .
//$this->weights[$i][$j] .'</td>';
}
}

}
//echo '</tr>';
}
//echo '</table><br />';
// JALUR
/**
* Private method to get the path.
*
* Get graph path from predecessor matrice.
* @param integer $i
* @param integer $j
* @return void
*/
private function __get_path($i, $j) {

if ( $i != $j ) {
$this->__get_path($i, $this->pred[$i][$j]);
}
array_push($this->tmp, $j);
}

/**
* Public function to access get path information.
*
* @param ingeger $i Starting node.
* @param integer $j End node.
* @return array Return array of nodes.
*/
public function get_path($i, $j) {
$this->tmp = array();
$this->__get_path($i, $j);
return $this->tmp;
}

/**
* Print nodes from a and b.
* @param ingeger $i Starting node.
* @param integer $j End node.
* @return void
*/
public function print_path($i, $j) {

if ( $i != $j ) {
$this->print_path($i, $this->pred[$i][$j]);
}
if (! empty($this->nodenames) ) {
print($this->nodenames[$j]) . ' ';
} else {
print($j) . ' ';
}
}
/**
* Get total cost (distance) between point a to b.
*
* @param integer $i
* @param ingeger $j
* @return array Returns an array of costs.
*/
public function get_distance($i, $j) {
return $this->dist[$i][$j];
}

/************************************************************
*** DEBUG FUNCTIONS ***
*** - print_graph ***
*** - print_dist ***
*** - print_pred ***
*************************************************************/

/**
* Print out the original Graph matrice.
* @return void
*/
public function print_graph () {

if ( empty($_SERVER['argv']) ) {
echo '<strong>Graph (K = 0)</strong><br />';
echo '<table border="1" cellpadding="4" cellspacing="0">';
if (! empty($this->nodenames) ) {
echo '<tr>';
echo '<td>&nbsp;</td>';
for ($n = 0; $n < $this->nodes; $n++) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$n] .
'</strong></td>';
}
}
echo '</tr>';
for ($i = 0; $i < $this->nodes; $i++) {
echo '<tr>';
if (! empty($this->nodenames) ) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$i] .
'</strong></td>';
}
for ($j = 0; $j < $this->nodes; $j++) {
echo '<td width="15" align="center">' .
$this->weights[$i][$j] . '</td>';
}
echo '</tr>';
}
echo '</table><br />';

} else {

}
}
/**
* Print out distances matrice.
* @return void
*/
public function print_dist () {

if ( empty($_SERVER['argv']) ) {
echo '<strong>Jarak ( Hasil dari iterasi ke '.$this->nodes.' )</strong><br />';
echo '<table border="1" cellpadding="4" cellspacing="0">';
if (! empty($this->nodenames) ) {
echo '<tr>';
echo '<td>&nbsp;</td>';
for ($n = 0; $n < $this->nodes; $n++) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$n] .
'</strong></td>';
}
}
echo '</tr>';
for ($i = 0; $i < $this->nodes; $i++) {
echo '<tr>';
if (! empty($this->nodenames) ) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$i] .
'</strong></td>';
}
for ($j = 0; $j < $this->nodes; $j++) {
echo '<td width="15" align="center">' .
$this->dist[$i][$j] . '</td>';
}
echo '</tr>';
}
echo '</table><br />';
} else {
echo "cmd line not yet completed!\n";
}

}
/**
* Print out predecessors matrice.
* @return void
*/
public function print_pred () {

if ( empty($_SERVER['argv']) ) {
echo '<strong>Jalur yang dilewati</strong><br />';
echo '<table border="1" cellpadding="4">';
if (! empty($this->nodenames) ) {
echo '<tr>';
echo '<td>&nbsp;</td>';
for ($n = 0; $n < $this->nodes; $n++) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$n] .
'</strong></td>';
}
}
echo '</tr>';
for ($i = 0; $i < $this->nodes; $i++) {
echo '<tr>';
if (! empty($this->nodenames) ) {
echo '<td width="15" align="center"><strong>' .
$this->nodenames[$i] .
'</strong></td>';
}
for ($j = 0; $j < $this->nodes; $j++) {
echo '<td width="15" align="center">' .
$this->pred[$i][$j] . '</td>';
}
echo '</tr>';
}
echo '</table><br />';
} else {
echo "cmd line not yet completed!\n";
}

} // End of class
?>

 D
 D

B. Sss
C. S
D. S
E. S

You might also like