user3081276
user3081276

Reputation: 1

$_GET variable is Undefined within the Pagination Code

I'm not familiar with PHP and I got a 2 weeks problem on how to make the $_GET or $_SESSION variable within my pagination code. The situation is:

I have a value from the index.php page that when I click to it (example Photos/Images) I get the value using GET then when I echo the value to my Category.php page, the value is projected OK within my BODY page using 'echo' but what I wanted is when the page loaded, the GET variable will be the searchkey in my pagination code. But when I echoed the GET variable within my pagination code, I get this result 'UNDEFINED' which means the variable is not defined within my pagination code. Here is my pagination. Oh yes, I made changes to my code i while back im sorry but this is the previous code where i used $mycat and GET method. I am sorry.

<?php
session_start();
$mycat =$_GET['category'];
//PHP code for specific search
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';

if($action == 'ajax'){

    /* Connect To Database*/
    $dbname = 'ufile';
    $link = mysql_connect("localhost","root","") or die("Couldn't make connection.");
    $db = mysql_select_db($dbname, $link) or die("Couldn't select database");

    $mycat =  htmlspecialchars ($_GET['category']) ? trim($_GET['category']) : ''; //SQL injection protection

    include 'pagination.php'; //include pagination file

    //pagination variables
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 10; //how many records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;

    echo 'Result: ' .$mycat; //////////THIS IS THE GET VALUE....WHICH TURNS OUT AS UNDEFINED IN VALUE IT SHOULD BE 'PHOTOS/IMAGES, AUDIOS/VIDEOS or COMPRESSED FILES'....PLEASE HELP ME.

    //Count the total number of row in your table*/
    $count_query   = mysql_query("SELECT COUNT(category) AS numrows FROM udocs WHERE category LIKE '%" . $mycat . "%'"); //Count the number of search result here (tagal kitang hinanap dito ka lng pala)
    $row     = mysql_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = 'cat_index.php';

    //main query to fetch the data
    $result = mysql_query("SELECT * FROM udocs WHERE category LIKE '%" . $mycat . "%' ORDER BY date_upload LIMIT $offset,$per_page");


    //loop through fetched data
    while($test = mysql_fetch_array($result)){

        $id = $test['file_name'];   
            $fileid=$test['file_path'];

            echo "<div class='content'>";
                echo'<div class="img_content"  ><img src='.$test['file_icon'].' style=height:50px;width:50px;float:left;margin-right:10px;></div>';
                echo"<font  color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Name:   </font><font color='yellowgreen'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['file_name']."</font><br>";
                    echo"<font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Category:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['category']."</font></font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Size:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['file_size']."</font></font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Credits to:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['author']."</font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Date Uploaded:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['date_upload']."</font><br>";
                    echo"<font color='black'; font size='2px !important' ;>Description:". $test['description']. "</font><br>";
                    echo"<a href ='download.php?file_path=$fileid' ><font color='orange' title='$mycat'>Download File</p></a>";
            echo        "</div>";

}
    //BOTTOM
    if ($numrows==0)
    {

    }
    else
    {
        echo paginate($reload, $page, $total_pages, $adjacents);
    }

    //header('Location:index.php');
    //die();

} // end of ajax first condition
else
{

?>
<body>

<?php
 echo 'You are searching for: '.$mycat;
?>

</body>

<?php } ?>

Upvotes: 0

Views: 476

Answers (2)

Muhammet Arslan
Muhammet Arslan

Reputation: 985

As i understood on ajax u could get variable , but when refresh u couldnt right? If im true , u should store it on session , and then first look on get/request and then session.

Take it easy

Upvotes: -1

Zarathuztra
Zarathuztra

Reputation: 3251

If $_REQUEST will allow you to access the parameters, but $_GET will not, try $_POST, meaning that the HTTP method used was POST and not GET. Check the form/javascript that is submitting to this script as well. $_REQUEST is a catch all superglobal that contains both get and post parameters (and cookies too!).

$_REQUEST

I'd also recommend doing a little reading on the HTTP protocol in depth, it can help with these sorts of issues :)

In addition to what Joren just commented, do include the URL you're using to access this page.

Upvotes: 2

Related Questions