Reputation: 154
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.
Upvotes: 0
Views: 31
Reputation: 519
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';
}
Upvotes: 1
Reputation: 219117
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.
Upvotes: 2