Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2d array_multisort problem

    I am attempting to create a 2d table where I can enter the data as I get it row by row, then use the array_multisort() function to get the output in alphabetical order by "title".

    But I get

    Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag

    So how do I correct this mess to get the proper order I want?




    PHP Code:

    $all60
    [1]=array("Title"=>"El Paso","Flip"=>"Running Gun","Artist"=>"Marty Robbins","Label"=>"Columbia 41511");
    $all60[2]=array("Title"=>"Why","Flip"=>"Swingin' On A Rainbow","Artist"=>"Frankie Avalon","Label"=>"Chancellor 1045");
    $all60[3]=array("Title"=>"The Big Hurt","Flip"=>"Memphis Bell","Artist"=>"Toni Fisher","Label"=>"Signet 275");
    $all60[4]=array("Title"=>"Running Bear","Flip"=>"My Heart Knows","Artist"=>"Johnny Preston","Label"=>"Mercury 71474");
    $all60[5]=array("Title"=>"Way Down Yonder In New Orleans","Flip"=>"Fractured","Artist"=>"Freddy Cannon","Label"=>"Swan 4043");
    $all60[6]=array("Title"=>"","Flip"=>"","Artist"=>"","Label"=>"");
    $all60[7]=array("Title"=>"","Flip"=>"","Artist"=>"","Label"=>"");
    $all60[8]=array("Title"=>"","Flip"=>"","Artist"=>"","Label"=>"");
    $all60[9]=array("Title"=>"","Flip"=>"","Artist"=>"","Label"=>"");
    $all60[10]=array("Title"=>"","Flip"=>"","Artist"=>"","Label"=>"");

    foreach (
    $all60 as  $row) {
         
    $row['Title'];
         
    $row['Flip'];
    }

    array_multisort($TitleSORT_DESC$FlipSORT_ASC$all60);

    print_r($all60);

    echo 
    "<table border='1' width='500'>";
    echo 
    "<tr>";
    echo 
    "<th> Title/Flip </th> <th> Artist/Label </th>";
    echo 
    "</tr>";
    echo 
    "</table>"

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    Not a MySQL question. Hopefully a moderator will move this to the PHP forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This isn't a mysql question, moving to PHP forum.
    I hate multisorts. What are the values of $Title and $Flip?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I think I have the sort problem worked out.

    Now how do I pull out each value so that the value falls under the proper column?
    Column 1 has title and flip.
    Column 2 has artist and label.


    PHP Code:
    foreach ($all60 as $Key=>$row) {
         
    $Title[$Key]=$row['Title'];
         
    }

    array_multisort($TitleSORT_ASC$all60);

    print_r($all60); 

  • #5
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dang it.
    Forgot which group I was in.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What are you wanting to sort by? The title and then the flip?
    Personally since I hate multisorting that much, I'd write a function for it and use an input sort:
    PHP Code:
    function sortByTitleThenFlip(array $a, array $b)
    {
        
    $iResult 0;
        if (isset(
    $a['Title'], $b['Title'], $a['Flip'], $b['Flip']))
        {
            
    $iResult strcmp($a['Title'], $b['Title']);
            if (
    $iResult == 0)
            {
                
    $iResult += strcmp($a['Flip'], $b['Flip']));
            }
        }
        return 
    $iResult;
    }

    usort($all60'sortByTitleThenFlip'); 
    I literally hate multisort that much that I'd rather write a comparator than use it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I think I have the sort issue solved.
    I need the output formatted as the table I show here.
    http://mroldies.net/top40-1960.html

    So how do I loop through the values to get that look?


    PHP Code:

    <?php



    $all60
    []['Title']="El Paso";
    $all60[]['Flip']="Running Gun";
    $all60[]['Artist']="Marty Robbins";
    $all60[]['Label']="Columbia 41511";


    $all60[]=array("Title"=>"Why","Flip"=>"Swingin' On A Rainbow","Artist"=>"Frankie Avalon","Label"=>"Chancellor 1045");
    $all60[]=array("Title"=>"The Big Hurt","Flip"=>"Memphis Bell","Artist"=>"Toni Fisher","Label"=>"Signet 275");
    $all60[]=array("Title"=>"Running Bear","Flip"=>"My Heart Knows","Artist"=>"Johnny Preston","Label"=>"Mercury 71474");
    $all60[]=array("Title"=>"Way Down Yonder In New Orleans","Flip"=>"Fractured","Artist"=>"Freddy Cannon","Label"=>"Swan 4043");

    foreach (
    $all60 as $Key=>$row) {
       
    $Title[$Key]=$row['Title'];
       
    $Flip[$Key]=$row['Flip'];
       
    $Artist[$Key]=$row['Artist']; 
       
    $Label[$Key]=$row['Label'];
       }
       
    array_multisort($TitleSORT_ASC$all60);

    foreach (
    $all60 as $item){ foreach ($item as $item2) {echo "$item2\n" "<br>";}}



    echo 
    "<table border='1' width='500'>";
    echo 
    "<tr>";
    echo 
    "<th> Title/Flip </th> <th> Artist/Label </th>";
    echo 
    "</tr>";



    echo 
    "</table>";

        
    ?>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is no good for your dataset:
    Code:
    $all60[]['Title']="El Paso";
    $all60[]['Flip']="Running Gun";
    $all60[]['Artist']="Marty Robbins";
    $all60[]['Label']="Columbia 41511";
    But the rest are fine.

    Once you've sorted it, you simply iterate and put it into a table.
    PHP Code:
    if (($iCnt count($all60)) > 0)
    {
        print 
    '<table>';
        
    $i 0;
        foreach (
    $all60 AS $item)
        {
            
    printf('<tr>
                    <td>%d</td>
                    <td>%s<br/>%s</td>
                    <td>%s<br/>%s</td>
                </tr>'
    , ++$i$item['Title'], $item['Flip'], $item['Artist'], $item['Label']);
        }
        print 
    '</table>';

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help.
    I was looking at using while.
    Your method I like better.


    PHP Code:
    while ($i<=5):
    $atitle=$all60[$i]['Title'];
    $btitle=$all60[$i]['Flip'];

    echo 
    "<tr><td> $atitle <br> $btitle </td></tr>";
    $i++;
    endwhile; 

  • #10
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks all.
    The almost done version

    http://mroldies.net/test1.php


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •