开发者

Most efficient way of checking which array a variable belongs to

I have a bunch of information pages. Currently I have a conversation for each different page/topic but I'd like to reduce them to fit under say 5 topics. Currently I have this script(and a similar on the actual links), which checks the hash tag and accordingly brings the right information page + executes the function which brings the conversation for the page:

$(document).ready(function() {
var hash = window.location.hash.substr(7);
var locs = ["koti", "espoonk", "luonto", "tyo", "kulttuuri", "kalajarvi", "dalby", "oittaa", "mgolf", "smeds", "marketanpuisto", "hognas", "pirttimaki", "ekumeenink", "kaisankoti", "solvalla", "velskola", "korpilampi", "nuuksio", "majalampi", "juvanmalmi", "rinnekoti", "hognasa", "pellaksenmaki", "royla", "ketunkorpi", "kulloonmaki", "gobbacka", "kalliomaki", "nepperi", "viiskorpi", "antinmaki", "niipperinn", "metsamaa", "odilampi", "lakisto", "lahnus", "koskelonsolmu", "isannansolmu", "auroranportti", "juvansolmu"];
if(navigator.appName == "Microsoft Internet Explorer") {
    if ($.inArray(hash, locs) != -1) {
            window.location = "indexie.html#kohde-" + hash;
    }
    else {
            window.location = "indexie.html"
    }
}
else if ($.inArray(hash, locs) != -1) {
    var toLoad = 'info/'+hash+'.html';
    $('#info').load(to开发者_JAVA百科Load);
    changeTopic(hash);
    $('#topicid').val(hash);
}
else {
    $('#info').load('info/help.html');
}
});

As you can see I currently have all the different topics under locs, but I dont want this many different conversations going on. changeTopic(hash) is used to bring the conversation by inserting the hash in a query string xmlhttp.open("POST", "gettopic.php?t="+topic,true);

While gettopic.php has these lines $t=$_GET["t"]; $inf = "SELECT * FROM comments WHERE page = '".$t."' ORDER BY time ASC";

Which way would be most efficient in sorting out different pages under common topics? I'm guessing I could do 5 different arrays and check if the hash belongs to any of them with 5 separate else if statements but this seems clumsy. Is there perhaps a way to bunch these different pages inside gettopic.php?

// EDIT

So I could have

var hash = window.location.hash.substr(7);
var topic1 = ["loc1", "loc2", "loc3"];
var topic2 = ["loc4", "loc5", "loc6"];
var topic3 = ["loc7", "loc8", "loc9"];

but this would lead me to having check each one individually (having 5 instead of 3 as well)

if ($.inArray(hash, topic1) != -1) {
    var toLoad = 'info/'+hash+'.html';
    $('#info').load(toLoad);
    changeTopic(topic1);
    $('#topicid').val(topic1);
}
else if ($.inArray(hash, topic2) != -1) {
    var toLoad = 'info/'+hash+'.html';
    $('#info').load(toLoad);
    changeTopic(topic2);
    $('#topicid').val(topic2);
}
else if ($.inArray(hash, topic3) != -1) {
    var toLoad = 'info/'+hash+'.html';
    $('#info').load(toLoad);
    changeTopic(topic3);
    $('#topicid').val(topic3);
}

What I'd like is a smoother way of checking which array the hash belongs to and then just running one single script which would run changeTopic(theArrayHashIsIn)


Ok, one absolutely striking thing that I see.. and don't take this as an insult, but make sure you're escaping $t before it is being used in that query string! We don't need another viagra website running around the internet on accident.

Anyway, try this... I think it may be more what you're looking for. Keep in mind that the 'true' value can be anything.. I just threw 'true' in for kicks.

var tree = {
    topic1: {
        conversation1: true,
        conversation2: true
    },
    topic2: {
        conversation3: true
    }
}

var flatMap = {
    conversation1: 'topic1',
    conversation2: 'topic2',
    conversation3: 'topic3'
}

Now, where it may apply to you.. if the hash is 'conversation1', you can do a check via the flatMap..

var hash = window.location.hash.substr(7); //lets say this is "conversation1"
if(flatMap[hash]) {
    //This is one of your conversations, and it'll give you "topic1" as the branch its under.
    //as well as giving you the validation that this is indeed a conversation
}

Or if you want to get a reference to the branch to open up..

var branch = tree[flatMap[hash]]

That'll get you an object of, for example,

{
    conversation1: true,
    conversation2: true
}

Or if you want to get a reference to the data you've stored in that spot..

var data = tree[flatMap[hash]][hash]; //This will just be 'true', because that's all we've stored.

Make sense?


Unless I misunderstand, you're looking for a way of mapping the location hash (which should equal a value in locs) to a topic. In JS, you can achieve this using any object, the array index operator and a string index. Similarly, arrays in PHP can have string indices, so you can use them to map strings to anything.

PHP:

$loc_topics = array(
    'koti' => 'topic1',
    'espoonk' => 'topic2',
    ///...
);
if (isset($loc_topics[$hash])) {
    $topic = $loc_topics[$hash];
    ...
} else {
    ...
}

JS:

var loc_topics = {
    koti: 'topic1',
    espoonk: 'topic2',
    ...
};

var topic = loc_topics[hash];
if (topic) {
    var toLoad = 'info/'+hash+'.html';
    $('#info').load(toLoad);
    changeTopic(topic);
    $('#topicid').val(topic);
} else {
    /* unknown loc/hash */
    $('#info').load('info/help.html');
}

If you've defined the array in PHP, you can generate the corresponding JS data structure using json_encode, so you don't duplicate the information and suffer the problems it can cause. In this case, you create a PHP script that generates the JS script:

var loc_topics = <?php echo json_encode($loc_topics) ?>;
...

You can also store the same information in your database in a table that relates locs and topics, which can make things easier to manage, should you change the topics, locs or the relationships between them.

If you have locs and topics tables:

CREATE TABLE locs_topics (
    loc INT UNSIGNED NOT NULL,
    topic INT UNSIGNED NOT NULL,
    INDEX (loc, topic),
    INDEX (topic, loc),
    FOREIGN KEY loc REFERENCES locs (id),
    FOREIGN KEY topic REFERENCES topics (id)
) Engine=InnoDB;

To query this table:

SELECT t.name 
  FROM locs AS l
    LEFT JOIN locs_topics AS lt ON lt.loc = l.id
    LEFT JOIN topics AS t ON t.id = lt.topic
  WHERE l.name = ?

If there is no topic for a given loc (but the loc is defined), this will return NULL. If there is no loc, this will return 0 rows.

If there are no locs or topics tables (though this can lead to inconsistencies, such as if there's a typo in the value of a field in one row):

CREATE TABLE locs_topics (
    loc VARCHAR(32) NOT NULL,
    topic VARCHAR(32) NOT NULL,
    INDEX (loc, topic),
    INDEX (topic, loc)
) Engine=InnoDB;

Querying is simpler:

SELECT topic
  FROM locs_topics
  WHERE loc = ?

If there is no topic for a loc, or if the loc isn't defined, this will return 0 rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜