views:

40

answers:

2

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');
      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;
    }  ?>
A: 

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";
Kris.Mitchell
You got me to look at the query and I found that it was slightly wrong, it should of been.. AND slug != '".PAGEPARENT."-".PAGECHILD."-".PAGEGRANDCHILD."' "); your revision would not of closed the query statement mysql_query(". I am using defines site wide and as there are 3 different styles for the theme and the defines help sort that and make it easier to change the theme instead of changing 1000's of lines of code :) sadly the sql query still doesnt work. I do appriciate the help though.
Daniel Wrigley
Anyone else able to help out?
Daniel Wrigley
+1  A: 

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.

Pekka
That worked a charm Pekka, thank you very much for the rapid reply and appologies on the double post, I didnt want to do that but saw no other option to re-ignite my original post, Im newish on here and could not see any option to republish my post so it would go back to the top of the recent questions list. Something the site should do; if it doesnt already :)
Daniel Wrigley
@Daniel yeah, no problem. For ways to get attention on old questions see http://meta.stackoverflow.com/questions/7046/how-to-get-attention-for-your-old-unanswered-questions
Pekka
@Pekka, sorry to hassle, but do you also know how I can use the LIKE clause in SQL within the SELECT clause? I need to find a way of pulling back the final part of a string that has hyphen delimeters i.e. foo-bar-rules (how would I grab `rules` from that string instead of the full string?)
Daniel Wrigley
@Daniel you mean as a result? No, you can't use LIKE for that. That part you'd have to build using mySQL string functions, or parse it out later in PHP (probably easier).
Pekka
@Pekka, I suppose I could parse it out as you say and explode it in php and call back the array index for that string, but if there is a neater way to do this within mySQL I would love to learn, I have looked into INSTR and LOCATE, I have learnt how to get the first substring of a string using INSTR but how could I change that to get the last part? I shall show how I am getting the first substring in another reply.
Daniel Wrigley
`SELECT DISTINCT SUBSTRING(slug, 1, INSTR(slug, '-') - 1) result`
Daniel Wrigley
@Daniel nice! That looks about right.
Pekka
@Pekka, :D thank you so so much for your help, really appriciate it.
Daniel Wrigley