dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Creating Array from PHP variables

  1. #1
    Join Date
    Oct 2017
    Posts
    5

    resolved [RESOLVED] Creating Array from PHP variables

    I have data coming from MYSQL to get the following:
    Photo 10 got 4 votes averaging 3.5 out of 5 // $pid=10 $pidtally=3.5 $votecount=4
    Photo 11 got 5 votes averaging 4.2 out of 5 // $pid=11 $pidtally=4.2 $votecount=5
    Photo 12 got 3 votes averaging 4.3 out of 5 // $pid=12 $pidtally=4.3 $votecount=3
    Photo 9 got 4 votes averaging 3.5 out of 5 // $pid=9 $pidtally=3.5 $votecount=4
    Photo 6 got 8 votes averaging 4.7 out of 5 // $pid=6 $pidtally=4.7 $votecount=8
    Photo 8 got 6 votes averaging 2.6 out of 5 // $pid=8 $pidtally=2.6 $votecount=6
    Photo 13 got 12 votes averaging 1.9 out of 5 // $pid=13 $pidtally=1.9 $votecount=12
    Photo 18 got 2 votes averaging 3 out of 5 // $pid=18 $pidtally=3 $votecount=2
    Photo 16 got 1 vote averaging 3 out of 5 // $pid=16 $pidtally=3 $votecount=1
    Photo 4 got 4 votes averaging 4.2 out of 5 // $pid=4 $pidtally=4.2 $votecount=4
    Photo 7 got 9 votes averaging 3.9 out of 5 // $pid=7 $pidtally=3.9 $votecount=9
    Photo 1 got 21 votes averaging 4.5 out of 5 // $pid=1 $pidtally=4.5 $votecount=21

    I can create the array to include all the data but I am hoping for only the top 10 based on $pidtally in descending order like this:

    Code:
    Array
    (
        [0] => Array
            (
                [pid] => 6
                [pidtally] => 4.7
                [votecount] => 8
            )
    
        [1] => Array
            (
                [pid] => 1
                [pidtally] => 4.5
                [votecount] => 21
            )
    
        [2] => Array
            (
                [pid] => 12
                [pidtally] => 4.3
                [votecount] => 3
            )
    
        [3] => Array
            (
                [pid] => 11
                [pidtally] => 4.2
                [votecount] => 5
            )
    
        [4] => Array
            (
                [pid] => 4
                [pidtally] => 4.2
                [votecount] => 4
            )
    
        [5] => Array
            (
                [pid] => 7
                [pidtally] => 3.9
                [votecount] => 9
            )
    
        [6] => Array
            (
                [pid] => 10
                [pidtally] => 3.5
                [votecount] => 4
            )
    
        [7] => Array
            (
                [pid] => 9
                [pidtally] => 3.5
                [votecount] => 4
            )
    
        [8] => Array
            (
                [pid] => 18
                [pidtally] => 3
                [votecount] => 2
            )
    
        [9] => Array
            (
                [pid] => 16
                [pidtally] => 3
                [votecount] => 1
            )
    )
    I'm trying to pass this array ($picNvote) to another PHP function for display but I cannot get the array sorted by the value of pidtally

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,214
    My first thought would be to customize the DB query to get it ordered the way you want. If that's not feasible for some odd reason, then you could use the usort() function to sort the array as desired.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Oct 2017
    Posts
    5
    Yeah, restructured the DB to get the desired result. Thanks

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,433
    How is $pidtally calculated?

    I don't quite see how it tallys with the total votes in the example you gave.

    In the example, the total number of votes is 61 which means an image that is viewed 21 times equals 34.42% of the likes yet its pidtally rates it as 4.5 when you have another entry that is a pidtally of 4.7 but the image only has 8 votes which is 13.11% of the vote, it is quite reasonable to expect that the pidtally value is something that relates to the views but it clearly has nothing to do with that aspect of the vote system.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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