0

I have problem with my php sorting...
I use _SESSION because I want to store sorting for another page, I think it's right. This is my code:

  <?php     
                $sql = "SELECT * FROM movies";
                $_SESSION['sort'] = isset($_GET['sort']);

                        if (isset ($_SESSION['sort']) == 'Year')
                        {
                            $sql .= " ORDER BY year";
                        }
                        elseif (isset($_SESSION['sort']) == 'IMDB')
                        {
                            $sql .= " ORDER BY IMDBrating";
                        }
                        elseif (isset($_SESSION['sort']) == 'user')
                        {
                            $sql .= " ORDER BY userrating";
                        }               

            ?>

                <select name="sort" id="sort" tabindex="1">
                    <option value="Year">Year</option>
                    <option value="IMDB">IMDB rating</option>
                    <option value="user">user rating</option>
                </select>

<?php
            $pagesize = 5;

            $recordstart = (int)(isset($_GET['recordstart'])) ? $_GET['recordstart'] : 0;

            $sql01 = "SELECT * FROM movies LIMIT $recordstart, $pagesize";
            $records=mysql_query($sql01);

            $result = mysql_query("SELECT count(id) FROM movies");
            $totalrows = mysql_fetch_row($result);

            while ($movies=mysql_fetch_array($records)){
                echo '<div class="movie_box"><p><div class="news_img"><div class="cover"><img src="'.$movies['cover'].'" width = "183px" height = "271px"/></div><br><button class="trailer_button" type="button">Trailer</button></div><strong><p class="h3"><div class="content">'.$movies['name'].'</p></strong>'.$movies['plot'].'<br><br><strong>Žanr</strong>:'.$movies['genre'].'<br><strong>IMDB ocjena</strong>:'.$movies['IMDBrating'].'<br><strong>Director</strong>:'.$movies['director'].'<br><strong>Glumci</strong>:'.$movies['Starring'].'<br><strong>Ocjena korisnika</strong>:</div><br><div class="trailer">'.$movies['trailer'].'</div><div class="dark"></div></p></div>';
            }

            if ($recordstart > 0){
                $prev = $recordstart - $pagesize;
                $url = $_SERVER['PHP_SELF'].'?recordstart='.$prev;
                printf('<a id="prev" href="%s"><</a>',$url);
            }

            if ($totalrows > ($recordstart + $pagesize)){
                $next = $recordstart + $pagesize;
                $url = $_SERVER['PHP_SELF'].'?recordstart='.$next;
                printf('<a id="next" href="%s">></a>',$url);
            }

        ?> 

There is no error but still no sorting. Now I edit post and you can see another part with another page script.

6
  • You never actually execute your SQL statement? Where specifically does this fail? When you debug this, what is the actual SQL statement being executed and what are the results? Commented May 3, 2015 at 20:25
  • Echo out the $sql and post it back here. Also, that code doesn't show any queries being made. Commented May 3, 2015 at 20:25
  • @N.B. When i echo #sql it says SELECT * FROM movies ORDER BY year Commented May 3, 2015 at 20:27
  • Great, and where do you query MySQL with that? The code shows only that you created the $sql string, but no queries are being made. Is that the entire code? Commented May 3, 2015 at 20:29
  • 1
    Your use of isset() is confusing and awkward. And I suspect it will always result in the first if block always being true. Commented May 3, 2015 at 20:29

2 Answers 2

2

isset() returns a boolean, either true or false. So this:

$_SESSION['sort'] = isset($_GET['sort']);

will set the session variable to either true or false. Then any time you do this:

isset($_SESSION['sort'])

it will return true since you set that session variable. This, however:

if (isset($_SESSION['sort']) == 'Year')

will always result in true because any non-null string value is "true-y". So your code will always sort by year, regardless of what sort options have been specified by the user.


If this contains your sort specifier:

$_GET['sort']

Then you don't need to use session or isset like this. Just compare that value:

if ($_GET['sort'] == 'Year')
{
    $sql .= " ORDER BY year";
}
// etc.
Sign up to request clarification or add additional context in comments.

2 Comments

but if i don't use session, then how i can store that sort if i go to another page?
@IvanGorički: There's nothing in your code which indicates that there even is another page. But yes, if you use session then you can refer to that value on another page. Or you could use a $_GET query string value like you're already using.
1
isset($_GET['sort']) == true;

if sort is set in _GET param, thats what isset function returns, true or false

you should do:

if (isset($_GET['sort'])) {
  $_SESSION['sort'] = $_GET['sort'];
} else {
 $_SESSION['sort'] = 'somekind of default sort';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.