+ Reply to Thread
Results 1 to 8 of 8

Thread: php rank ratings

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Question php rank ratings

    Hello,

    my situation is a bit complicated so please try to understand:

    I have a page (index.php) that lists all pages and some info about them.

    The info of the pages ($description, $title, $tags ...) is stored in files at profiles/.
    there are also a file with an array ($pages) with all titles in. - pages.php

    Code:
    <?php
    include 'pages.php'
    $num = count($pagetitles);
    
    for ($i=0;$i<$num;$i++){
    //get info about $i prank
    include ('profiles/'.$pagetitles[$i].'.php');
    
    echo "<div><h2>$title</h2><p>$info</p> ... ... </div>";
    
    }
    This code will write all pages in array $pagetitles' order.

    The profiles also have a $rating. What can I do to get the element with best rating first? Sort it?

    Thanks ......

  2. #2
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Re: php rank ratings

    well yes, you could do that. or you could simply loop through it like so:
    (don't use my syntax, it might be wrong. Use my idea)

    Code:
    $maxrating = $rating[0]; //set the first page as default
    $max_rating_page = $page[0];
    for ($i=0;$i<$num;$i++)
    {
      if ($rating[$i] > $maxrating)
      {
        $maxrating = $rating[$i]; //new biggest rating
        $max_rating_page = $page[$i];
      }
    }
    That was just off the top of my head. Hope it helps. Adjust it to suit your needs. If you need help, post back. If I'm not understanding correctly, please expand. Good luck.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: php rank ratings

    Do you want them all listed by rank?

    Or do you just want the highest rank one at top and don't care about the order of the rest?

    Ties?
    Nothing is always absolutely so.

  4. #4
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: php rank ratings

    Thanks denzilb22, but there is one problem

    I want all of them listed by rank, not only the first one. That's what I cant figure out.
    Last edited by gaptrast; 04-26-2011 at 02:20 PM.

  5. #5
    jkoritzinsky16 is offline x10Hosting Member jkoritzinsky16 is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    10

    Re: php rank ratings

    How about this:
    PHP Code:
    $num=count($page);
    $sortedByRating=array();
    for(
    $i=0;$i<$num;$i++)
    {
          if(isset(
    $sortedByRating[$rating[$i]]))
          {
                 if(!isset(
    $sortedByRating[$rating[$i]][0]))
                {
                     
    $sortedByRating[$rating[$i]][0]=$sortedByRating[$rating[$i]];
                }
                
    $sortedByRating[$rating[$i]][]=$page[$i];
          }
          else
          {
                
    $sortedByRating[$rating[$i]]=$page[$i];
           }
    }
    krsort($sortedByRating); 
    The ratings are the keys, and the pages are the values. If there are overlapping rating values, the value of $sortedByRating[sharedRating] is an unordered array with the pages sharing the rating as the values. The page with the highest rating is first, followed by the next highest rating and so on down.
    Last edited by jkoritzinsky16; 04-26-2011 at 02:49 PM. Reason: Missing a few explanations.

  6. #6
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: php rank ratings

    hmm can you explain it a bit more??

    Let me try:

    PHP Code:

    $num
    =count($page);
    $sortedByRating=array();
    for(
    $i=0;$i<$num;$i++)
    {

    //get info
    include ('profile/'.$page[i$].'.php');
    //also $rating (not array!!) example: 4

    //check if $sortedByRating[4] exists
          
    if(isset($sortedByRating[$rating]))
          {
    // ????????? multidimensional array???
                 
    if(!isset($sortedByRating[$rating][0]))
                {
                     
    $sortedByRating[$rating][0]=$sortedByRating[$rating[$i]];
                }
                
    $sortedByRating[$rating][]=$page[$i];
          }
          else
          {
                
    $sortedByRating[$rating]=$page[$i];
           }
    }
    // what's this?
    krsort($sortedByRating);

    //what if the rating is 4.6 or 3.65?? 
    Thanks for helping
    Last edited by gaptrast; 04-27-2011 at 11:04 AM.
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  7. #7
    jkoritzinsky16 is offline x10Hosting Member jkoritzinsky16 is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    10

    Re: php rank ratings

    Ok. In my snippet, $pages is an array holding the page names, and $rating is an array holding the ratings, with each key corresponding to an entry in the $pages array. So in your snippet, $rating is set within the included page, right?
    A multidimensional array is an array within an array. A better replacement for !isset($sortedByRating[$rating][0]) would be is_array($sortedByRating[$rating]) (my bad, sorry). krsort sorts the keys with the highest key (rating) first in the array supplied, so you can use a foreach loop to go through the array. Any rating can be used. The foreach loop would look like this:
    PHP Code:
    foreach($sortedByRating as $pagerating=>$page)
    {
         if(
    is_array($page))///Handling Multi-dimensional Array
         
    {
               foreach(
    $page as $nestedPage)
               {
                        
    ///$nestedPage here is one of multiple pages sharing the same rating of $pagerating
               
    }
         }
         else
    ///Only one page with this rating
         
    {
               
    ///$page here is the page name with the rating of $pagerating
         
    }

    The pages will go in order of highest rating to lowest rating through the loop.
    Last edited by jkoritzinsky16; 04-27-2011 at 11:52 AM.

  8. #8
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: php rank ratings

    I have understood a bit more, but not everything. Let me explain:

    writenow($p) is a function that writes something about the page (including ratings, description etc.). $p is number in $pages array.

    PHP Code:
    for ($p=0;$p<$num;$p++){
    ...
    ...
    $rating[$p] = $therating;

    This code makes the array $rating with the rating as value, and number as key.


    PHP Code:
    $sortedByRating=array();
    for(
    $i=0;$i<$num;$i++)
    {
          if(isset(
    $sortedByRating[$rating[$i]]))
          {
                 if(
    is_array($sortedByRating[$rating[$i]]))
                {
                     
    $sortedByRating[$rating[$i]][0]=$sortedByRating[$rating[$i]];
                }
                
    $sortedByRating[$rating[$i]][/*what to put in here??]=$page[$i];
          }
          else
          {
                $sortedByRating[$rating[$i]]=$page[$i];
           }
    }
    krsort($sortedByRating); 
    What will an example of sortedByRating be?

    $sortedByRating[3.56]="computers";
    or more than one page with 3.56:
    $sortedByRating[3.56][????]="computers";

    Thanks for any help.
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 11-19-2009, 03:38 AM
  2. Help - How To Get Google Rank.
    By indians in forum Advertising
    Replies: 15
    Last Post: 11-14-2009, 05:34 AM
  3. My new website. [Give me ratings]
    By DarkDragonLord in forum Graphics & Webdesign
    Replies: 26
    Last Post: 12-03-2007, 06:28 PM
  4. Personal-ish design! Ratings please!
    By Archkronos in forum Graphics & Webdesign
    Replies: 0
    Last Post: 07-26-2007, 04:36 PM
  5. Ratings Please
    By Woolie in forum Graphics & Webdesign
    Replies: 11
    Last Post: 01-04-2006, 08:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers