开发者

Slight problem getting mySQL SUBSTRING to work

I know I am pretty close with this, as I have recieved some help on this topic earlier, but when it cam to making the same code work for another part of the site it seems I cannot get it working again.

What I am doing is trying to get the first part of a string (which is broken by a - delimiter in the database) so the db entry slug would be foo-bar-rules, I need to grab foo from that string where slug is not equal to foo-bar-rules but equal to phoo-bar-rules or feuw-bar-rules. Am sorry if my explanation was'nt very helpful, I had trouble explaning it to myself :D.

Thank you in advance anyone that can help me out here..

<?php define ('PAGEPARENT', 'foo');
     开发者_JAVA技巧 define ('PAGECHILD', 'bar');
      define ('PAGEGRANDCHILD', 'rules');

    switch (PAGEGRANDCHILD) {
        case PAGEGRANDCHILD:
            $pageGrandChild = PAGEGRANDCHILD;
            $rangeRelationResult = mysql_query("SELECT DISTINCT SUBSTRING(slug, 1, INSTR(slug, '-') - 1) result
                                                FROM web_navbar_links
                                                WHERE SUBSTRING(slug FROM INSTR(slug, '-".PAGECHILD."-') + 1) = '$pageGrandChild'
                                                AND grandchild = 1
                                                AND slug != '".PAGEPARENT."-".PAGECHILD."-".PAGEGRANDCHILD."';
                                                ");
            while ($rangeRelationRow = mysql_fetch_object($rangeRelationResult)) { ?>
                <a href="?page=<?php echo $rangeRelationRow->result."-".PAGECHILD."-".PAGEGRANDCHILD; ?>&pageLevel=<?php echo $_GET['pageLevel']; ?>" title="<?php echo PAGEGRANDCHILD."&nbsp;for&nbsp;".$rangeRelationRow->result; ?>"><div id="<?php echo $rangeRelationRow->result; ?>Channel"><?php echo "&raquo;&nbsp;".ucwords(PAGECHILD)." for ".$rangeRelationRow->result; ?></div></a> <?php
            }
        break;
    }  ?>


I'm not entirely sure whether I get your question right, but is LIKE an option?

SELECT .... WHERE slug LIKE "%-rules" AND slug != "foo-bar-rules" 

would select all records whose slug ends in -rules.


Does the query just fail because of syntax, or does the logic not work?

Anyways, I think your quotes are a little off...

At the end of the your query you had

AND slug != '".PAGEPARENT."-".PAGECHILD."-".PAGEGRANDCHILD."';
                                            ");

and I think you needed

AND slug != '".PAGEPARENT."-".PAGECHILD."-".PAGEGRANDCHILD."')";

Also, why are you going to the length of using define?

Can you just do this? (I might be missing something if you are using classes...)

$PAGEPARENT = "foo";  $PAGECHILD = "bar";  $PAGEGRANDCHILD = "rules";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜