0

This is my view page:

<?php

include 'connect/con.php';

$result = mysqli_query($con,"SELECT newsvid.id, newsvid.vidTitle, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id ORDER BY newsvid.id DESC");

while($row = mysqli_fetch_array($result)) {
  echo '<div class="id">#<a href="details.php?id='.$row['id'].'">'.$row['id'].'</a></div>';
  echo "<div class=\"vidTitle\">" . $row['vidTitle'] . "</div>";
  echo "<div class=\"imgCover\"><img class=\"imageCover\"src=\"" . $row['url'] . "\"></div>";
  echo "<div class=\"vidSD\">" . $row['vidSD'] . "</div>";
  echo "<div class=\"vidDetails\"> 
<table class=\"tableSD\" >
<tr><td class=\"tdBR\"><strong> Years: </strong></td><td class=\"tdB\">" . $row['vidYear'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> City: </strong></td><td class=\"tdB\">". $row['vidCity'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Zanr: </strong></td><td class=\"tdB\">". $row['vidZanr'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Quality: </strong></td><td class=\"tdB\">". $row['vidQuality'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Translated: </strong></td><td class=\"tdB\">". $row['vidTranslated'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Video time: </strong></td><td class=\"tdB\">". $row['vidTime'] .  "</td></tr>
</table> 
 </div>";


  echo "<div class=\"published\"><strong>Published: </strong>" . $row['published'] . "</div>"; 
}
mysqli_close($con);
?>

I need a LINK in index.php page this bit "ORDER BY newsvid.id DESC" will be changes according to the clicked link.

EG:

<div class="mainLeftCover">
<a href="#>ASC</a> |
<a href="#>DESC</a> |
</div>

If I press ASC the view.php page will show result ORDERED BY newsvid.id ASC and if I will press DESC same view.php page will show the result ORDERED BY newsvid.id DESC and so on...

Denis
  • 103
  • 3
  • 10

2 Answers2

1
  • Make two links featuring ASC and DESC as an URL query parameter (say, $_GET['order'])
  • On the server side be sure to check whether the value is either ASC or DESC to prevent malicious intent (i.e., in_array(strtolower($_GET['order']), ['asc', 'desc'])
  • Append the parameter to the query so the DESC/ASC part is variable
Elias
  • 1,532
  • 8
  • 19
1

First you require to add a parameter to the link url

<div class="mainLeftCover">
<a href="view.php?order=ASC">ASC</a> 
<a href="view.php?order=DESC">DESC</a> 
</div>

Then set a variable with the stub of the query minus ORDER part.

$query ="SELECT newsvid.id, newsvid.vidTitle, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id";

Use isset() with the ternary operator to either GET the parameter or if it is not set pass a default.

$order = isset($_GET['order']) ? $_GET['order'] : 'DESC';//Change to ASC if you want this as default

Use in_array()to sanitise

$goodParam = array("ASC", "DESC");
if (in_array($order, $goodParam)) {

Then use .= Concatenation assignment to build $query.

        if($order == 'DESC'){
            $query .= " ORDER BY newsvid.id DESC";
        }else{
            $query .= " ORDER BY newsvid.id ASC";
      }
}
    $result = mysqli_query($con,$query);
Community
  • 1
  • 1
david strachan
  • 7,174
  • 2
  • 23
  • 33