Solved

problem with PHP limit page and  header("Location:

Posted on 2007-11-13
25
4,430 Views
Last Modified: 2010-05-18
Hi all,

I am having a problem with a php page im working on.
The page is a results page from a keyword search page.

On this page i limit the records to 10 and have page numbers so the user can click on the page they want records for.

I also have a  script at the top of the page that directs the user back to the search form and echos a message if no fields are filled in and the search is submitted.

The problem i have is if i click one of the page numbers  to view records on that recordset page, the page doesnt load, instead the header code at the top of my page is executed. and i get redirected to the search form as if i just entered a blank search?
I assume this is due to the way i have my code laid out but i am unsure how to fix the issue while retaining my header script at the top of the page.

Here is my entire page code so you can see what i am reffering to.

<?php require_once('Connections/EE_con.php'); ?>
<?php

$keyword_street = "";
$keyword_village = "";
$case = 0;
if(!isset($_REQUEST["keyword_street"]) || $_REQUEST["keyword_street"]=='')
{
      $keyword_street = "<font color=\"red\">Please enter this field!</font>";
}

if(!isset($_REQUEST["keyword_village"]) || $_REQUEST["keyword_village"]=='')
{
      $keyword_village = "<font color=\"red\">Please enter this field!</font>";
}
     
if($keyword_village!="" && $keyword_street!="")
{
      header("Location: keyword2.php?street=$keyword_street&village=$keyword_village");
      // Keyword2.php is the name of the page where my search form is.
     exit();
}
?>

<?

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;


mysql_select_db($database_EE_con, $EE_con);

$keywordstreet = $_REQUEST['keyword_street'];
$keywordvillage = $_REQUEST['keyword_village'];
$K_street = $_POST['keyword_street'];
$K_village = $_POST['keyword_village'];



if (strlen($_POST['keyword_street']) > 0) {
$test = "one";
}
if (strlen($_POST['keyword_village']) > 0) {
$test2 = "two";
}

if ($test = "one" && $test2 = "two") {
$where_clause = "WHERE property_address1 LIKE '%$keywordstreet%' AND property_towncity LIKE '%$keywordvillage%'";
   }

$query_Recordset1 = "SELECT * FROM properties $where_clause ORDER BY price ASC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $EE_con) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;



if ($totalRows_Recordset1 == 0){




echo "There are no results matching your search criteria <BR> Keyword 1:$K_street <BR> Keyword 2:$K_village <BR><BR> Click Here to search again!";
}

echo "whereclause:$where_clause<br>";
echo "keyword Street: $keywordstreet<br>";
echo "keyword Village: $keywordvillage<br>";
echo "test: $test<br>";
echo "test2: $test2<br>";
   do {

   ?>
   
 
    <p><?php echo $row_Recordset1['property_numname']; ?></p>
    <p><?php echo $row_Recordset1['property_address1']; ?></p>
    <p><?php echo $row_Recordset1['property_address2']; ?></p>
    <p><?php echo $row_Recordset1['property_towncity']; ?></p>
    <p><?php echo $row_Recordset1['prop_postcode']; ?></p>
    <p><?php echo $row_Recordset1['pic1']; ?></p>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

Page           <?php for ($i = 0; $i <= $totalPages_Recordset1; $i++) {
  $href = sprintf('%s?pageNum_Recordset1=%d%s', $currentPage, $i, $queryString_Recordset1);
  $label = $i + 1;
  echo '<a href="'.$href.'">'.$label.'</a>';
}
?>

<?php
mysql_free_result($Recordset1);
?>
0
Comment
Question by:satmanuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +8
25 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20276265
These:
if(!isset($_REQUEST["keyword_street"]) || $_REQUEST["keyword_street"]=='')
{
      $keyword_street = "<font color=\"red\">Please enter this field!</font>";
}

if(!isset($_REQUEST["keyword_village"]) || $_REQUEST["keyword_village"]=='')
{
      $keyword_village = "<font color=\"red\">Please enter this field!</font>";
}
make sure that $keyword_village and $keyword_street always contain some value. Thus,
if($keyword_village!="" && $keyword_street!="")
will always be true an the header function will execute
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20276345
I get that but what i don't get is why they don't have a value. If they didn't then i wouldn't be seeing my results in the first place?
0
 
LVL 82

Expert Comment

by:hielo
ID: 20276387
> why they don't have a value
you are wrong! you are checking the variables
$keyword_village  && $keyword_street
and you are always setting a value for them. By the time the script "hits" the if, neither of those variables is empty!
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 82

Expert Comment

by:hielo
ID: 20276412
Try this:
if((!isset($_REQUEST["keyword_street"]) || $_REQUEST["keyword_street"]=='') && (!isset($_REQUEST["keyword_village"]) || $_REQUEST["keyword_village"]=='') )


instead of:
if($keyword_village!="" && $keyword_street!="")
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20276531
I replaced the line with your code but nothing changes.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20276597
try this

if(!isset($_GET['pageNum_Recordset1']) {
      
      if(!isset($_REQUEST["keyword_street"]) || $_REQUEST["keyword_street"]=='')
      {
              $keyword_street = "<font color=\"red\">Please enter this field!</font>";
      }
      
      if(!isset($_REQUEST["keyword_village"]) || $_REQUEST["keyword_village"]=='')
      {
              $keyword_village = "<font color=\"red\">Please enter this field!</font>";
      }
             
      if($keyword_village!="" && $keyword_street!="")
      {
              header("Location: keyword2.php?street=$keyword_street&village=$keyword_village");
              // Keyword2.php is the name of the page where my search form is.
             exit();
      }

}
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20276624
I get this when i run a search
Parse error: syntax error, unexpected '{' in /home/melvin/public_html/estateagents/keywordresults.php on line 7
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20276641
forgot a ) .. sorry

if(!isset($_GET['pageNum_Recordset1'])) {
      
      if(!isset($_REQUEST["keyword_street"]) || $_REQUEST["keyword_street"]=='')
      {
              $keyword_street = "<font color=\"red\">Please enter this field!</font>";
      }
      
      if(!isset($_REQUEST["keyword_village"]) || $_REQUEST["keyword_village"]=='')
      {
              $keyword_village = "<font color=\"red\">Please enter this field!</font>";
      }
             
      if($keyword_village!="" && $keyword_street!="")
      {
              header("Location: keyword2.php?street=$keyword_street&village=$keyword_village");
              // Keyword2.php is the name of the page where my search form is.
             exit();
      }

}
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20276693
Ok cool cheers, The search works again and when i click the numbers it doesnt revert to the search page. but now when i click the page numbers the results screw up and show all in the table rather than the results based on my search? Example the results were 2 pages of records. I click page 2 and then the results display all records. So the search results are lost.

any ideas?
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20276725
try this

$keywordstreet = $_REQUEST['keyword_street'];
$keywordvillage = $_REQUEST['keyword_village'];
$K_street = $_POST['keyword_street'];
$K_village = $_POST['keyword_village'];

$queryString_Recordset1 = "&keyword_street=".$_REQUEST['keyword_street']."&keyword_village=".$_REQUEST['keyword_village'];

if ($keywordstreet) {
$test = "one";
}
if ($keywordvillage) {
$test2 = "two";
}
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20276879
just wish i knew how you did that! it works!

Many thanks

i want to do something similar to  the header script for when a user searches something thats not found.
 at the moment i got this line:

if ($totalRows_Recordset1 == 0){
echo "There are no results matching your search criteria <BR> Keyword 1:$K_street <BR> Keyword 2:$K_village <BR><BR> Click Here to search again!";
}

Do you know if its possible to treat this the same as when a user trys to submit no data?
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20276905
$queryString_Recordset1 = "&keyword_street=".$_REQUEST['keyword_street']."&keyword_village=".$_REQUEST['keyword_village'];

That just passes the search criteria to the next page.

 >> Do you know if its possible to treat this the same as when a user trys to submit no data?

You mean redirect the user back to the search page ?
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20277034
yes in essence what i want is the user to get a message on the searchform.php page saying no results found. like i have for when the form is submitted with no data.

Or instead of my code :
echo "There are no results matching your search criteria <BR> Keyword 1:$K_street <BR> Keyword 2:$K_village <BR><BR> Click Here to search again!";
}

have the user directed to a new page where i can state no results were found.
0
 
LVL 12

Expert Comment

by:sandip132
ID: 20287126
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 20287549
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 20287742
Congratulations from ellandrd!!!
0
 
LVL 4

Expert Comment

by:Klaatu01
ID: 20291743
Great to see a relatively new member with obviously valuable skills provide the 2 Millionth solution!  Well done!
0
 
LVL 15

Expert Comment

by:Naser Gabaj
ID: 20309049
Well Done, and 2 Milions Congratulations :)

Regards,

Naser
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20310640
Well done steelseth12 !!
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20330559
Thanks everyone! I was shocked to find that my question earnt me a laptop! i havent a clue what type it is? anyone know?

THANKS EXPERTS EXCHANGE!
0
 
LVL 16

Expert Comment

by:Kiran Paul VJ
ID: 20332609
>>i havent a clue what type it is?

from the EE newsletter

"Each laptop is outfitted with an Intel Core 2 Duo 2.0GHz processor, a 17-inch wide screen display, 2GB of memory, and dual 120GB hard drives. Our superb designers mocked up some flashy desktop wallpapers for the winners new laptops....."

congrats satmanuk!!

kiranvj
0
 
LVL 12

Expert Comment

by:sandip132
ID: 20332631
These winners will also receive limited edition Experts Exchange 2,000,000 solutions shirts, and recognition in their member profiles as being part of EE history.

Cheers!
0
 
LVL 1

Author Comment

by:satmanuk
ID: 20336770
COOL! thats a beast of a laptop!

:O) :O)
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 20374045
I want to be this kind of winner :((
Great!
Welldone & congratz
0
 

Expert Comment

by:sachindurge
ID: 20926832
gud one
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question