0

I'm trying to sort by certain alphanumeric range 1-3 & A-B for first 15 matches, but am getting nothing in output. Using MeekroDB. Not sure how to translate what I have here to their method.

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet
FROM gallery ORDER BY (CASE theme
  WHEN '1%' THEN 1
  WHEN '2%' THEN 2
  WHEN '3%' THEN 3
  WHEN 'A%' THEN 4
  WHEN 'B%' THEN 5
  ELSE -1
END) LIMIT 15"); 

$x = 0
foreach ($results as $row) {
  $x++;
  if ($x == 1) {  // first in query
    $t1 = $row['theme'];
    $d1 = $row['developer'];
    $th1 = $row['thumb'];
    $thlg1 = $row['thumb_lg'];
  }
  ...
}

Echo example in body:

<img src="<?php echo($th1); ?>" data-retina="<?php echo($thlg1); ?>" alt="<?php echo($t1); ?>" />
<span><p class="hname"><?php echo($t1); ?></p>
<p class="hdev"><?php echo($d1); ?></p></span>

Update2:

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg
FROM gallery ORDER BY (CASE
  WHEN theme LIKE '1%' THEN 1
  WHEN theme LIKE '2%' THEN 2
  WHEN theme LIKE '3%' THEN 3
  WHEN theme LIKE 'A%' THEN 4
  WHEN theme LIKE 'B%' THEN 5
  ELSE -1
END) LIMIT 15");

Update3:

$results = DB::query("SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb, thumb_lg FROM gallery
  ORDER BY (CASE Alphabet
    WHEN '1' THEN 1
    WHEN '2' THEN 2
    WHEN '3' THEN 3  
    WHEN 'A' THEN 4
    WHEN 'B' THEN 5
    ELSE 6
  END)
  LIMIT 15");
parti
  • 205
  • 3
  • 15
  • Are you trying to use `LIKE` to perform a pattern match? Your CASE expression is doing exact matches. – Barmar Jun 02 '14 at 17:16
  • Does the query itself produce results when run against the database? echo $x when you start it to make sure that there is a row to echo. – Jay Blanchard Jun 02 '14 at 17:17
  • When I echo $results, get word Array. – parti Jun 02 '14 at 17:20
  • That's what you get when you try to echo an array in PHP. Use `var_dump()` to see the contents. – Barmar Jun 02 '14 at 17:21
  • `if ($x == 1)` is limiting your result to one row. – Jay Blanchard Jun 02 '14 at 17:21
  • 1
    @JayBlanchard I suspect the `...` includes cases for other values of `$x`. – Barmar Jun 02 '14 at 17:21
  • 2
    Your query only returns one column, `Alphabet`. There's no `theme`, `developer`, or `thumb`. – Barmar Jun 02 '14 at 17:23
  • I suspect that you may be right @Barmar, but I'd sure remove it for testing. – Jay Blanchard Jun 02 '14 at 17:23
  • Good eye @Barmar! I have been looking at this with the OP and I totally brain-farted! *facepalm* – Jay Blanchard Jun 02 '14 at 17:25
  • Here's my test page: http://www.partiproductions.com/vault_test/1-a-b.php Ran var_dump($results) Yes, x=1 is just for 1st match, I have 14 more, but just showing one for simplicity ;) So what do I need to do? – parti Jun 02 '14 at 17:27
  • 1
    Add the columns to the query that @Barmar spotted as missing. `SELECT substr(theme, 1, 1) as Alphabet, theme, developer, thumb FROM...` – Jay Blanchard Jun 02 '14 at 17:30
  • You can see on the test page, var_dump is producing weird results. Is it thinking Alphabet is one of my fields, as it's not. I'm also pulling in a thumb_lg FYI: first row id= 1, theme=Bella, developer=2bitoperation & thumb=resources/Thumbs/bella-v.jpg, & thumb_lg=(blank at the moment) – parti Jun 02 '14 at 17:47
  • Wait, I just updated code to use foreach (was using a while loop) & now, at least stuff is coming through. BUT - it's not in the alphanumeric order it should be in. There's a theme that starts w/ '3' & others that start w/ 'a' that should be listed first. – parti Jun 02 '14 at 17:58
  • @Barmar Guys, still not getting correct Case sort order. – parti Jun 02 '14 at 19:44

1 Answers1

1

The ORDER BY clause should be:

ORDER BY CASE Alphabet
    WHEN '1' THEN 1
    WHEN '2' THEN 2
    WHEN '3' THEN 3
    WHEN 'A' THEN 4
    WHEN 'B' THEN 5
    ELSE 6
END, theme

Your code is doing exact matches on the whole theme, '1%' should have been matched using LIKE. My version just uses the first character, which you already extracted into Alphabet.

Another way to write this is:

ORDER BY IF(LOCATE(Alphabet, "123AB"), 0, 1), theme

This works because the order of your first characters matches the normal lexicographic order.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Updated code $results above, but still getting weird results. – parti Jun 02 '14 at 20:05
  • Please don't change the question to match the answer. How is someone supposed to know what the problem was that I fixed? – Barmar Jun 02 '14 at 20:07
  • How are the results weird? This should show all the themes that don't begin with `1-3` or `A-B` first, then the ones with these prefixes. – Barmar Jun 02 '14 at 20:09
  • The results should show themes alphanumerically that do begin w/ a number/letter of 1,2,3,A & B (In that order) Thus, Ex: 3-Dimensional should be shown first, then aRRaNGe, Artist2, etc... – parti Jun 02 '14 at 20:16
  • 1
    Then you should make the `ELSE` value higher than all the others, not lower. – Barmar Jun 02 '14 at 20:17
  • Ok, It's getting close, see update3 code above. Listings w/in each letter set aren't in alphabetical order, Ex, Currently: 3-Dimensional, AutoCue, aRRaNGe, AutoCue Arrows, etc... Can we get it to look at the entire theme name to determine the order? – parti Jun 02 '14 at 20:56