Question

I have a custom PHP file that uses javascript, php, and SQL queries, but am brand new to drupal and having a difficult time implementing it. I've attempted to follow the module-building tutorial a few times, but feel overwhelmed/stressed with the amount of detail, and has not resulted in my favor as of yet. Is it possible to easily make my file that works wonderfully in my XAMPP environment into a drupal module without altering the code too much? And if so, what alterations do I need to make in my PHP file?

Below is the code in my PHP file:

<?php
$server = 'localhost';$user = 'root';$database = 'transportation';$password = null;
$db = mysqli_connect($server, $user, $password, $database);
?>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

var map = null;
var infowindow = new google.maps.InfoWindow();
var markers = [

  <?php
  if (isset($_GET['routeselected'])) 
  {
  $result = mysqli_query($db, "SELECT * FROM STOP INNER JOIN RouteStop ON Stop.Stop_ID = RouteStop.Stop_ID WHERE RouteStop.Route_ID = " . (int)$_GET['routeselected'] . " AND RouteStop.Company_ID = " . (int)$_GET['companyselected']);
  while ($row = mysqli_fetch_array($result))
      echo "{\"title\": '".$row['Stop_ID']."', \"lat\": '".$row['Latitude']."', \"lng\": '".$row['Longitude']."', \"description\": '".$row['StopName']."'},";
  }
  ?>

];

window.onload = function () {

var mapOptions = {
    center: new google.maps.LatLng(
        parseFloat(markers[0].lat),
        parseFloat(markers[0].lng)),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var path = new google.maps.MVCArray();
var service = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var poly = new google.maps.Polyline({map: map, strokeColor: '#F3443C'});

var lat_lng = new Array();
           for (i = 0; i < markers.length; i++) {
           var data = markers[i]
           var myLatlng = new google.maps.LatLng(data.lat, data.lng);
           lat_lng.push(myLatlng);
           var marker = new google.maps.Marker({
               position: myLatlng,
               map: map,
               title: data.title
           });
           (function (marker, data) {
               google.maps.event.addListener(marker, "click", function (e) {
                   infoWindow.setContent(data.description);
                   infoWindow.open(map, marker);
               });
           })(marker, data);
       }
for (var i = 0; i < markers.length; i++) {
    if ((i + 1) < markers.length) {
        var src = new google.maps.LatLng(parseFloat(markers[i].lat), 
                                         parseFloat(markers[i].lng));
        var des = new google.maps.LatLng(parseFloat(markers[i+1].lat), 
                                         parseFloat(markers[i+1].lng));
        service.route({
            origin: src,
            destination: des,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        }, function (result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++){
                    path.push(result.routes[0].overview_path[i]);
                }
                poly.setPath(path);
            }
        });
    }
}
} 

</script>

<?php
if (isset($_GET['companyselected'])) 
  {
  if ($result = mysqli_query($db, "SELECT DISTINCT RouteNum, RouteName FROM Route WHERE Company_ID = " . (int)$_GET['companyselected']))
  {
    $fields = mysqli_fetch_fields($result);
      echo "<table style='width:900px'><tr>";
    foreach ($fields as $column) 
      echo "<th>" . $column->name . "</th>"; 
      echo "<th>Select Bus Company</th></tr>";
    while ($row = mysqli_fetch_assoc($result)) {
      echo "<tr>";
    foreach ($row as $field) echo "<td>" . $field . "</td>";
      echo "<td><a href=\"" . $_SERVER['PHP_SELF'] . "?routeselected=" 
       . $row['RouteNum'] . "&companyselected=" . $_GET['companyselected'] . "\" style=\"color: green;\">Display Route</a></td></tr>";
  }
    echo "</table>";
  } 
}

if (!(isset($_GET['companyselected']))) {
  $result = mysqli_query($db, "SELECT DISTINCT Company_ID, CompanyName " . "FROM Company");
  $fields = mysqli_fetch_fields($result);
    echo "<table style='width:900px'><tr>";
  foreach ($fields as $column) 
    echo "<th>" . $column->name . "</th>"; 
    echo "<th>Select Bus Company</th></tr>";
  while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
  foreach ($row as $field) echo "<td>" . $field . "</td>";
    echo "<td><a href=\"" . $_SERVER['PHP_SELF'] . "?companyselected=" 
       . $row['Company_ID'] . "\" style=\"color: green;\">Show Routes</a></td></tr>";
}
echo "</table>";
}

?>
Was it helpful?

Solution

dirty way1: You can create a node with php content in body (PHP filter module) and use all the above code in your node which will hopefully work

dirty way2: you can use the above logic with a block instead of a node if u want to include it in more than one pages

module way: you have to provide more information on how exactly you would like your module to work (eg override the user's profile page to include the map or create a block you can include in the template taking variables from the administration back-end or.. etc), but your code will have to change lots and u should also make use of the drupal api (eg for querying the drupal database, accessing variables etc). Drupal modules take (or at least should take) advantage of the drupal API so for example instead of normal php connecting/querying the database you can simply (and securely) instead use the [db_query function]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top