Just a lil question or 2 on mysql/js

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
weirdone2
Posts: 3
Joined: Thu May 14, 2015 2:53 pm

Just a lil question or 2 on mysql/js

Post by weirdone2 »

So I've started piecing together a game, and decided to use the mmotutorial 2.0 as a sorta skeleton that I'm slowly chopping up. :P But I'm curious on exactly how a certain piece of code works that has to do with mysql. Let me explain the situation a lil first though, because I have a 2nd question as well. :P So what I plan to do is take out the current map system, and instead make it into basically a screen loader for the current location your at, basically builds activities based on your location from mysql. But I'm not sure about this part of the code.

Code: Select all

function getMap()
{
         $.ajax({
        url : 'scripts/php/getMap.php',
        type : 'POST',
        data : {mapR:mapR,mapC:mapC,zone:zone
        },
        dataType:'json',
        success : function(data) {              
           buildMap(data);
        }
    });
}
function buildMap(data)
{
    var map = document.getElementById("map"); 
    ///.innerHTML will end all div tags so must do a string builder
    var mapDiv = "";
    var counter = 0;
    for(var i = 0; i < 25; i++)
    {
        if(counter == 5)
        {
            mapDiv += "<div id='grid" + i + "' class='breakGrid grid'>";
            innerCounter = 0;
            for(var m = 0; m < 9; m++)
            {
                var backgroundPic = parseMapBackground(data[i],m);
                var foregroundPic = parseMapForeground(data[i],m);
                if(innerCounter == 3)
The part of that snippet that I don't quite understand, would be the data in the parse area. My take on it so far is that data is basically an array returned from getmap() $rows that added the whole mysql entry to the array, and so each increment of i, just goes down the mysql entry list basically. If that's the case, it would sorta be like an array inside an array I guess, or am I a little off on that one?

Also second question, is that I plan to use many if statments based off what my mysql has in it for activities to build my screen, is there a better way than using many if statments?
Example of what I plan:

Code: Select all

if (data.zone==1){if (data.region==1){if (data.activities == 7) {build activity 7}if(data.activities == 3) {build activities 3}}} etc
So with my current plan it would be many, many if statments once I have a few zones, and acitivities. More than one activity can be in an area. Anyone know a cleaner, more efficient method? I'm also thinking about taking the multiple activities, and assisgning them just one number based on their location, this would save some space and processing, but I would still have all those if statements for zone/region but could at least use else if's for the activities. o.0 Just thought up a pretty decent way to streamline it actually, but still curious of others takes on how to deal with this? :)


For those not familiar with mmotutorial 2.0, here is the getmap()

Code: Select all

<?php
include 'DbClass.php';
$db = new Db();
$zone = $_POST['zone'];
$mapR = $_POST['mapR'];
$mapC = $_POST['mapC'];
$mapRMin = $mapR - 2;
$mapCMin = $mapC - 2;
$mapRMax = $mapR + 2;
$mapCMax = $mapC + 2;
$rows = $db -> select("select * from maps where zone='" . $zone . "' and (mapR >= " . $mapRMin . " and mapR <= " . $mapRMax . ") and (mapC >= " . $mapCMin . " and mapC <= " . $mapCMax . ")");
if($db->error() != '')
{
    $error = $db->error();
    $db -> close();
    echo json_encode($error);
}
else
{
     $db -> close();
    echo json_encode($rows);
}

?>
Post Reply

Return to “Beginner Help and Support”