Sorting

Discussion in 'PHP' started by hotfaps, Feb 16, 2010.

  1. #1
    Ok not sure if this was ment for SQL or here but it goes in a php tag so im putting it here...

    Ok there is a database field that holds a number like

    111-252-51

    The first number sorts it by Type , then Brand - Then model

    How would I make it so it will only pull by specfic parts of this and sort it like that?
     
    hotfaps, Feb 16, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Assuming all numbers will be in the same format as your example (except different digits).

    <?php
    
    $number = "111-252-51";
    
    preg_match("/([0-9]{3})-([0-9]{3})-([0-9]{2})/", $number, $parts);
    
    $type = $parts[1];
    
    $brand = $parts[2];
    
    $model = $parts[3];
    
    ?>
    PHP:
    Now you have each part in a variable.
     
    danx10, Feb 16, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi, it's better to redesing your database, so Type , Brand and Model will be separated integer fields.
    Regards
     
    koko5, Feb 16, 2010 IP
  4. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah. The way suggested by Danx10 is a fix for your problem, although the easier way is to create seperate Integer fields in the database, and when you need to combine the numbers, simply

     $combined = $type . '-' . $brand . '-' . $model; 
    PHP:
     
    Marshton, Feb 16, 2010 IP
  5. hotfaps

    hotfaps Active Member

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #5
    That all works good, now I need one more thing


    I need to have it pull from database

    XXX-THIS-XXXXXX

    I need it to pull only the center line and all of them that have the same number. How do I make it sort though like that and ignore the rest.
     
    hotfaps, Mar 15, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    <?php
    
    $str = "XXX-THIS-XXXXXX";
    
    preg_match("/(.+)\-(.+)\-(.+)/", $str, $parts);
    
    $center = $parts[2];
    
    echo $center;
    ?>
    PHP:
     
    danx10, Mar 15, 2010 IP
  7. hotfaps

    hotfaps Active Member

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    I don't see how this will do it,

    Let me show a better example of what I need to do

    11-142-13452 - Product Name - Product Description - Price
    11-142-13453 - Product Name - Product Description - Price
    11-142-13451 - Product Name - Product Description - Price
    11-143-31452 - Product Name - Product Description - Price

    Lets say I wanted to pull all rows of 142 and use every field

    I need to be able to use the other this when I pull a array of products.
     
    hotfaps, Mar 15, 2010 IP
  8. hotfaps

    hotfaps Active Member

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #8
    Anyone know how to do this?
     
    hotfaps, Mar 23, 2010 IP