Jump to content

Barand

Moderators
  • Posts

    24,345
  • Joined

  • Last visited

  • Days Won

    795

Posts posted by Barand

  1. That gives

    mysql> SELECT price
        ->  , CAST(REPLACE(price,' ','') AS signed) as number
        -> FROM test
        -> ORDER BY CAST(REPLACE(price,' ','') AS signed);
    +--------+--------+
    | price  | number |
    +--------+--------+
    | 8 082  |   8082 |
    | 8 791  |   8791 |
    | 8 791  |   8791 |
    | 9 374  |   9374 |
    | 9 823  |   9823 |
    | 10 186 |  10186 |
    | 12 257 |  12257 |
    | 12 698 |  12698 |
    | 13 959 |  13959 |
    | 14 463 |  14463 |
    | 14 920 |  14920 |
    | 15 132 |  15132 |
    | 16 023 |  16023 |
    | 16 117 |  16117 |
    | 16 606 |  16606 |
    +--------+--------+
    

    So, except for "table" being a reserved word and not to be used as a table name, I see no problem.

     

    edit: But why are you storing prices as text? Why not just use DECIMAL.

  2. That code that I linked you to was in a thread of yours from a few weeks ago, yet here you are again with the same problems ::)

     

    Have another look at the linked code - particularly at how parameters are used in prepared queries.

  3. Yes - it has been serialized twice.

    Serialize the array into a string

    Serialize the resulting string

     

    To unravel it, unserialize twice

    print_r( unserialize(unserialize('s:287:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"38000";s:8:"original";s:0:"";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:0:"";}s:11:"highway_mpg";a:1:{s:5:"value";s:0:"";}s:12:"custom_badge";s:0:"";s:5:"video";s:0:"";s:10:"short_desc";s:0:"";}"')));
    

    Gives

    Array
    (
        [price] => Array
            (
                [value] => 38000
                [original] => 
            )
    
        [custom_tax_inside] => 
        [custom_tax_page] => 
        [city_mpg] => Array
            (
                [value] => 
            )
    
        [highway_mpg] => Array
            (
                [value] => 
            )
    
        [custom_badge] => 
        [video] => 
        [short_desc] => 
    )
    
    • Like 1
  4. Having created a mysqli connection object and stored it in $con the next thing you do is destroy that object by overwriting it with a string value (your sql code).

     

    Secondly, just creating a string of sql code does not execute it. You need to use mysqli::query()

     

    eg

    $sql = "SELECT whatever ..."
    $con->query($sql);
  5. Either

    • loop through the rows, opening a new table when the occupation changes, or
    • store records in a 2 dimensional array by occupation then process that array
      foreach (data as occupation => records)
          open table for occupation
          foreach (records as row)
              write row
          endforeach
          close table
      endforeach
  6. The second query is finding the category whose id matches that in the product. So that is the candidate for the join.

     

    I.E.

    FROM products
    INNER JOIN categories ON products.category_id = categories.category_id

    or

    FROM products
    INNER JOIN categories USING (category_id)

    Do not use "SELECT * ". Specify the columns you need (and use aliases)

    SELECT 
        p.name
      , p.slug as prodslug
      , c.slug as catslug
    FROM FROM products p
        INNER JOIN categories c 
            ON p.category_id = c.category_id 
    WHERE p.status='1' AND p.featured='1'
    ORDER BY p.product_id 
    LIMIT 12

    See my signature re mysql_ library

  7. I couldn't even get your query from reply #7 to run - you need two variables but you only provide one. It would need to be

    $parent= 13425;
    $sql = "SELECT 
                id
              , dogname
              , mother_id as dam
              , father_id as sire
              , gender
            FROM dog
            WHERE mother_id= :parent1
            OR father_id = :parent2
            ORDER BY id";
    $dogs = [];
    $dogs[0] = ['name' => 'N/A', 'gender' => 'N/A', 'pups' => [] ];
    
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':parent1', $parent, PDO::PARAM_INT);
    $stmt->bindParam(':parent2', $parent, PDO::PARAM_INT);
    $stmt->execute();
    

    However, that is academic as you wouldn't want that query anyway.

    • Like 1
  8. I am not sure what you are saying. Using

    $dog_id = 13425;
    descendants($dog_id, $dogs);  // 3rd parameter used internally to track generation depth (indent)
    

    I get

    Alka Volání Karpat (female)
     ----- Connor from Bandit's World (male)
     -----  ----- A.- Magnus the Wolf I. Lord of Lasvegas (male)
     -----  ----- A.- Marny the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Melody the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Mercedes the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Merlin the Wolf I.Lord of Lasvegas (male)
     -----  ----- A.- Milena the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Miles the Wolf I. Lord of Lasvegas (male)
     -----  ----- A.-Minerva the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Mirabell the Wolf I. Lady of Lasvegas (female)
     -----  ----- A.- Morgan the Wolf I. Lord of Lasvegas (male)
     -----  ----- Basilea the Wolf I. Lady of Lasvegas (female)
     -----  ----- Beatrix the Wolf I. Lady of Lasvegas (female)
     -----  ----- Ben the Wolf I. Lord of Lasvegas (male)
     -----  ----- Bogdan the Wolf I. Lord of Lasvegas (male)
     -----  ----- Bonita the Wolf I. Lady of Lasvegas (female)
     -----  ----- Bozena the Wolf I. Lady of Lasvegas (female)
     -----  ----- Brix the Wolf I. Lord of Lasvegas (male)
     -----  ----- Brixius Jack the Wolf I. Lord of Lasvegas (male)
     -----  ----- Camilo the Wolf I. Lord of Lasvegas (male)
     -----  ----- Cedric the Wolf I. Lord of Lasvegas (male)
     -----  ----- Chrysanthus the Wolf I. Lord of Lasvegas (male)
     -----  ----- Chandra the Wolf I. Lady of Lasvegas (female)
     -----  ----- Can the Wolf I. Lord of Lasvegas (male)
     -----  ----- Conan the Wolf I. Lord of Lasvegas (male)
     -----  ----- El Cid Grey Diamond (male)
     -----  ----- Eddi the Wolf I. Lord of Lasvegas (male)
     -----  ----- Elvis the Wolf I. Lord of Lasvegas (male)
     -----  ----- Ezra the Wolf I. Lord of Lasvegas (male)
     -----  ----- Emmi the Wolf I. Lady of Lasvegas (female)
     -----  ----- Esma the Wolf I. Lady of Lasvegas (female)
     ----- Chunami from Bandit's World (female)
     -----  ----- Djumana from Bandit's World (female)
     -----  ----- Delphi from Bandit's World (female)
     -----  -----  ----- Gangster from Bandit's World (male)
     -----  -----  ----- Gaucho from Bandit's World (male)
     -----  -----  ----- Gauner from Bandit's World (male)
     -----  -----  ----- Ganove from Bandit's World (male)
     -----  -----  ----- Greedy from Bandit's World (male)
     -----  -----  ----- Gwendy from Bandit's World (female)
     -----  -----  ----- Gil from Bandit's World (female)
     -----  -----  ----- Genesis from Bandit's World (female)
     -----  ----- Dynamite from Bandit's World (female)
     -----  ----- Dunbar from Bandit's World (male)
     -----  ----- Excalibur from Bandit's World (male)
     -----  ----- Efia-Eliska from Bandit's World (female)
     -----  ----- Feivel from Bandit's World (male)
     -----  ----- Fabulous from Bandit's World (male)
     -----  ----- Floyd from Bandit's World (male)
     -----  ----- Fraser from Bandit's World (male)
     -----  ----- Fiasco from Bandit's World (male)
     -----  ----- For Me from Bandit's World (female)
     -----  -----  ----- Heartbreaker from Bandit's World (male)
     -----  -----  ----- Hero from Bandit's World (male)
     -----  -----  ----- Highlander from Bandit's World (male)
     -----  -----  ----- Hashtag Houston from Bandit's World (female)
     -----  ----- Fanny from Bandit's World (female)
     ----- Chica from Bandit's World (female)
     ----- Cherina from Bandit's World (female)
     ----- Chandra from Bandit's World (female)
     ----- Cazan from Bandit's World (male)
     ----- Aiyana from Bandit's World  (female)
     ----- Akela from Bandit's World  (female)
     ----- Amy from Bandit's World  (female)
     ----- Bakira from Bandit's World  (female)
     ----- Balko from Bandit's World  (male)
     ----- Bayana from Bandit's World  (female)
     ----- Blaidd from Bandit's World  (female)
     ----- Bruce from Bandit's World  (male)
    
  9. For the record

     

    My method

    Get and store all dogs data  : 0.467 seconds
    Output                       : 0.001 seconds
    
    

    Jaques' method

    mysql> SELECT
        ->     d1.id
        ->   , d1.dogname as name1
        ->   , d1.gender
        ->   , d2.dogname as name3
        ->   , d3.dogname as name3
        ->   , d4.dogname as name4
        ->   , d5.dogname as name5
        -> FROM dog d1
        -> LEFT JOIN dog d2 ON d1.id IN (d2.mother_id, d2.father_id)
        -> LEFT JOIN dog d3 ON d2.id IN (d3.mother_id, d3.father_id)
        -> LEFT JOIN dog d4 ON d3.id IN (d4.mother_id, d4.father_id)
        -> LEFT JOIN dog d5 ON d4.id IN (d5.mother_id, d5.father_id)
        -> WHERE d1.id = 8032;
    
    411 rows in set (1 min 46.40 sec)
    
  10. Use one query to put the dogs into an array, each dog having an array of pups.

     

    Then use a recursive function on the array. More efficient than recursive queries

    $sql = "SELECT 
                id
              , dogname
              , mother_id as dam
              , father_id as sire
              , gender
            FROM dog
            ORDER BY id";
    $dogs = [];
    $dogs[0] = ['name' => 'N/A', 'gender' => 'N/A', 'pups' => [] ];
    $res = $db->query($sql);
    $results = $res->fetchAll();
    //
    // get the dogs
    //
    foreach ($results as $d) {
        if (!isset($dogs[$d['id']])) {
            $dogs[$d['id']] = [ 'name' => $d['dogname'], 'gender' => $d['gender'], 'pups' => [] ];
        }
    }
    
    //
    // assign their pups
    //
    foreach ($results as $d) {
        $dogs[$d['dam']]['pups'][] = $d['id'];
        $dogs[$d['sire']]['pups'][] = $d['id'];
    }
    
    
    
    function descendants($id, &$dogs, $level=0)
    {
        if (!isset($dogs[$id])) return;
        $indent = str_repeat(' ----- ', $level); 
        echo "$indent{$dogs[$id]['name']} ({$dogs[$id]['gender']})<br>";
        if (!empty($dogs[$id]['pups'])) {
            foreach ($dogs[$id]['pups'] as $pupid) {
                descendants($pupid, $dogs, $level+1);
            }
        }
    }
    
    //
    // CALL THE RECURSIVE FUNCTION
    //
            $dog_id = 8032;
            descendants($dog_id, $dogs, 0);
    
    
    
    • Like 2
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.