wordpress - Handling URLs correctly with PHP

62

i have the following case within my plugin for wordpress: I have a details page of a database entry which can be accessed like this:

http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42

The next would be to add sorting options to this page and i solved it with this code:

The url would be:

http://localhost:8888/studio/wp-admin/admin.php?page=myplugin-details&id=42&orderby=answer&order=asc



<?php
      if (!empty($_GET['orderby'])) {
        $pos = strpos($_SERVER["REQUEST_URI"], 'orderby');
        $url = substr($_SERVER["REQUEST_URI"], 0, $pos-1); 
        if ($_GET['order'] == 'desc') {
          echo '<th >';
          echo '<a href="'.$url.'&orderby=answer&amp;order=asc">';
        } else {
          echo '<th >';
          echo '<a href="'.$url.'&orderby=answer&amp;order=desc">';
        }
      } else {
        echo '<th >';
        echo '<a href="'.$_SERVER["REQUEST_URI"].'&orderby=answer&amp;order=asc">';
      }
      ?>

This works fine, but do i have to do that URL/REQUEST_URI stuff or is there a solution which is much simpler?

Thanks!

People are also looking for solutions to the problem: php - Level & Exp Point System

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.