php rank ratings

gaptrast

Member
Messages
123
Reaction score
0
Points
16
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 ......
 

denzil

New Member
Messages
134
Reaction score
3
Points
0
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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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?
 

gaptrast

Member
Messages
123
Reaction score
0
Points
16
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:

jkoritzinsky16

New Member
Messages
10
Reaction score
0
Points
0
How about this:
PHP:
$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:

gaptrast

Member
Messages
123
Reaction score
0
Points
16
hmm can you explain it a bit more??

Let me try:

PHP:
$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:

jkoritzinsky16

New Member
Messages
10
Reaction score
0
Points
0
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:
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:

gaptrast

Member
Messages
123
Reaction score
0
Points
16
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:
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:
$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.
 
Top