Jump to content

PHP_SELF error


Recommended Posts

I am trying to use the script from the website http://www.designplace.org/scripts.php?page=1&c_id=25, and it works fine if I set the limit variable high so that only 1 page is needed.

 

However, if i need more than 1 page to display products, i get the following error:

 

Notice: Undefined variable: PHP_SELF in C:\wamp\www\search2.php on line 148

 

 

and it will only show me results from the first page. How can I fix this?

 

Thanks.

 

 

Link to comment
Share on other sites

Can you post the code?

 

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from the_table where 1st_field like \"%$trimmed%\"  
  order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["1st_field"];

  echo "$count.) $title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>

Link to comment
Share on other sites

$PHP_SELF was depreciated long ago in php4.2 in the year 2002. In most cases you can simply remove it (nothing in a URL refers to the current page) or replace it with $_SERVER['PHP_SELF']

 

Thanks.

 

I took it out and i don't get the notice error anymore, but I can't display the 2nd page of products. And when I put in the SERVER code instead I get:

 

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\search2.php on line 128

 

 

How can I make the code work so that I can display say 3 pages with 3 items on each?

Link to comment
Share on other sites

  echo " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><<"; 

Does your echo closes the "?

 

Sorry, I mean yes i have:

 

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  echo " <a href=\"$_SERVER['PHP_SELF']?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

Link to comment
Share on other sites

<html>

<head>

<title> www.sports-r-us.co.uk </title>

<link rel="stylesheet" type="text/css" href="registerpage.css"/>

</head>


<body>

<div id="site" align="center">

<div id="header">
<div class="text">
<a href="sitemap.php">SiteMap</a>
<a href="contactus.php">ContactUs</a>
<a href="help.php">Help</a>
</div>
</div>

<div id="navbar">

	<div class="text">
	<a href="user_Home.php">Home</a> 
	<a href="product-catalogue.php">ProductCatalogue</a> 
	<a href="search.php">Search</a> 
	<a href="browse.php">BrowseByCategory</a> 
	<a href="basket.php">Basket</a> 
	<a href="logout.php">Logout</a>  
	</div> 
</div>

<?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=3; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root",""); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("dh-ecommerce2") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from products where productName like \"%$trimmed%\"  
  order by productCode"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_numrows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q=" 
  . $trimmed . "\" target=\"_blank\" title=\"Look up 
  " . $trimmed . " on Google\">Click here</a> to try the 
  search on google</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "<font color=\"white\">Results:</font> </br></br>";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $productName = $row["productName"];
  $productLine = $row["productLine"];
  $productDescription = $row["productDescription"];
  $image = $row["image"];
  $sellPrice = $row["sellPrice"];

  echo "<font color=\"white\"><u>$count.) $productName</u></b><br><br>$productLine<br><br></font>";
  echo "<img alt=\"product image\" width=\"250\" height=\"265\" src=\"images/".$image." \"/>";
  echo "<font color=\"white\"></br><u>Product Description</u><br><br>$productDescription<br><br>£$sellPrice<hr color='gray' width='35%'></hr><br></font>";

  
  //echo "$count.) $productName</br>$productLine</br>" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  echo " <a href=\"$_SERVER['PHP_SELF']?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>

<div id="footer">
</div>
</div>	
</html>

Link to comment
Share on other sites

As was pointed out above:

  echo " <a href=\"{$_SERVER['PHP_SELF']}?s=$prevs&q=$var\"><<
  Prev 10</a>&nbsp ";

 

Notice the { and } around $_SERVER['PHP_SELF']

 

I've done that too, and just got the same message as i got in the first place:

 

Notice: Undefined variable: prevs in C:\wamp\www\search2.php on line 148

Link to comment
Share on other sites

     <?php

  // Get the search variable from URL

  $var = isset($_GET['q'])?$_GET['q']:''; // use isset instead of @ operator as @ just suppresses errors which should not be done
  $trimmed = trim($var); //trim whitespace from the stored variable
  $prevs = 0; // define $prevs since it is used later on to prevent a notice error message.

 

It is often good practice to define your variables that may or may not be initiated but used in the script. That should remove the notice error for that variable.

Link to comment
Share on other sites

     <?php

  // Get the search variable from URL

  $var = isset($_GET['q'])?$_GET['q']:''; // use isset instead of @ operator as @ just suppresses errors which should not be done
  $trimmed = trim($var); //trim whitespace from the stored variable
  $prevs = 0; // define $prevs since it is used later on to prevent a notice error message.

 

It is often good practice to define your variables that may or may not be initiated but used in the script. That should remove the notice error for that variable.

 

Yep thats got rid of the notice errors, thankyou.

 

My page still says, for example:

 

Next 3 >>

 

Showing results 1 to 3 of 8

 

And wont let me go to results 4-6. Any ideas?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.