andy_b_1502 Posted May 5, 2012 Share Posted May 5, 2012 Hi Everyone, I have a script for a search box, it is not working as expected i need to know what to change in it safely. The search is for UK postcodes, for testing it i'm using my postcode WS122SH. The results are this: Click here to try the search on google You searched for: "ws122sh" Results for ; ws122sh ws122sh Next 10 >> Showing results 1 to of "ws122sh" I know i have WS122SH in my DB, so that IS a result, so i know that if numrows == 0 isnt working right? but not sure how to change it? i deleted it before but it caused syntax errors, any one please help. Thanks you. <?PHP session_start(); include('php only scripts/db.php'); ?> <!DOCTYPE html> <head> <title>Removalspace.com</title> <style type="text/css"> <!-- body { background-image: url(styles/downloaded%20styles/todo/todo/images/bg.png); } --> </style> <link href="styles/downloaded styles/todo/todo/css/style.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" href="styles/downloaded styles/todo/todo/css/style9.css" /> <link rel="stylesheet" type="text/css" href="styles/downloaded styles/todo/todo/css/demo.css" /> <link href='http://fonts.googleapis.com/css?family=Terminal+Dosis' rel='stylesheet' type='text/css' /> <style type="text/css"> <!-- .Stile1 {color: #333333} --> </style> </head> <body> <!--start container --> <div id="container"> <header> <nav> <div id="logo"><a href="index.php"><img src="images/header2.png" alt="Logo here" width="219" height="161" /></a> </div> <div id="search-top"> <form method="post" action="search.php"> <input type="text" name="strSearch" onFocus="if(this.value=='Search')this.value='';" onBlur="if(this.value=='')this.value='Search';" value="Search" id="search-field"/> <input type="submit" value="" id="search-btn"/> </form> </div> <div id="nav_social"><a href="http://www.facebook.com/pages/Removalspace/181434181939226"><img src="styles/downloaded styles/todo/todo/images/facebook_32.png" alt="Become a fan" width="32" height="32" /></a><a href="#"><img src="styles/downloaded styles/todo/todo/images/twitter_32.png" alt="Follows on Twitter" /></a><a href="#"><img src="styles/downloaded styles/todo/todo/images/linkedin_32.png" alt="Linked in" /></a><a href="contact.php"><img src="styles/downloaded styles/todo/todo/images/email_32.png" alt="Contact" width="32" height="32" /></a> </div> </nav> </header> <p><a href="removals.php">Search Removals</a></p> <p><a href="storage.php">Search Storage</a></p> <p><a href="about.php">About</a></p> <p><a href="contact.php">Contact</a></p> <div class="content"> <!--star main --> <main></main> <!--end main --> <!--start middle --> <middle> <div class="section_slogan"> <?PHP // validate postcode search function is_valid_uk_postcode($postcode) { $pattern = "/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/"; if (preg_match($pattern, $postcode)) { return TRUE; } $this->validation->set_message('is_valid_uk_postcode', 'That is not a valid %s.'); return FALSE; } $error_message = ""; if((!isset($_POST['strSearch'])) || (strlen(trim($_POST['strSearch'])) <5) || (trim($_POST['strSearch']) != preg_replace("/[^a-zA-Z0-9\_]/", "", trim($_POST['strSearch'])))) { $error_message = "You must enter a valid postcode<br>"; $error_message = $error_message . "Valid postcodes must not contain and spaces and consist of letters and numbers only.<br>"; $error_message = $error_message . 'Your invalid name was: <font color="red">' . $_POST['strSearch'] . "</font><hr>"; }else{ $postcode = mysql_real_escape_string(trim($_POST['strSearch'])); } // Get the search variable from URL $postcode_results = $_POST['strSearch']; $trimmed = trim($postcode_results); //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($postcode_results)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } // Build SQL Query $query = "(SELECT postcode, company_name FROM companies WHERE postcode like '%$trimmed%') ORDER BY postcode"; // EDIT HERE and specify your table and field names for the SQL query // 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: "" . $postcode_results . ""</p>";"<br>"; "<hr>"; // begin to show results set echo "<a href=\"localarea.php?var=$postcode_results\">Results for $q;</a>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row['postcode']; $name = $row['company_name']; echo " $title" ; echo " $company_name" ; $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=$postcode_results\"><< Prev 10</a>  "; } // 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=$postcode_results\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> <?php echo "<a href=\"localarea.php?var=$var\">"$postcode_results"</a>";?> </div> </middle> </div> <!--end middle --> <!--start footer --> <footer> <div id="footer"></div> </footer> <!--end footer --> </div> <!--end container --> <!-- Free template distributed by http://freehtml5templates.com --> </body> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> </html> Quote Link to comment Share on other sites More sharing options...
andy_b_1502 Posted May 5, 2012 Author Share Posted May 5, 2012 the desired results have since changed, since my previous post... The below script is used for displaying results from my DB. Users can click any of the results which goes to a viewpage "view00.php" This works great, what i'm after though is a mix between the search script "search.php" and "view00.php" I have tried to create this but need a little help. If you need more information let me know. Here is the 'display script'.... the results send user to view00.php <?PHP session_start(); include ('php only scripts/db.php'); ?> <?php $what_services = "R"; $query = "SELECT * FROM companies WHERE what_services = '$what_services' ORDER BY approved DESC, company_name ASC"; $result = mysql_query($query); while ( $row = mysql_fetch_array($result)) { $approved = $row['approved']; $what_services = $row['what_services']; SWITCH ($approved) { case 0: break; case 1: ?> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption><a href="#"><?PHP echo $row['company_name']; ?></a></figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="#"><?PHP echo $row['street1'] . "<br>" . $row['street2'] . "<br>" . $row['city'] . "," . $row['postcode'] . "<br>phone: " . $row['phone'] ?></a> </figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="#"><?PHP echo $row['company_name']; ?></a> </figure> </div> <?PHP break; case 2: ?> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="#"><?PHP echo $row['company_name']; ?></a> </figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="#"><?PHP echo $row['company_name']; ?></a> </figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="#"><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></a></figure> </div> <?PHP break; case 3: ?> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption><a href="view00.php?id=<?PHP echo $row['id']; ?>"><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></a></figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption> <a href="view00.php?id=<?PHP echo $row['id']; ?>"><?PHP echo $row['company_name']; ?></a> </figure> </div> <div class="abox"> <figure> <fcapion> <h1> </h1> </fcaption><a href="view00.php?id=<?PHP echo $row['id']; ?>"><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></a></figure> </div> <p> <?PHP break; } } ?> here is view00.php: <?PHP session_start(); include('php only scripts/db.php'); $id = $_GET['id']; $query ="SELECT * FROM companies WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); ?> <table> <tr> <td valign="top"><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></td> <td valign="top"> <?PHP echo $row['street1'] . "<br>" . $row['street2'] . "<br>" . $row['city'] . "," . $row['postcode'] . "<br>phone: " . $row['phone'] . "<br>email: " . $row['email'] . "<br>website: " . $row['website'] ; ?> </td> </tr> <tr> <td><?PHP echo nl2br($row['premiumuser_description']); ?></td> </tr></table> Here is the search box form used on the homepage: <form method="post" action="search.php"> <input type="text" name="strSearch" onFocus="if(this.value=='Search')this.value='';" onBlur="if(this.value=='')this.value='Search';" value="Search" id="search-field"/> <input type="submit" value="" id="search-btn"/> </form> And here is search.php for that form: <?PHP session_start(); include('php only scripts/db.php'); ?> <!DOCTYPE html> <head> <title>Removalspace.com</title> <style type="text/css"> <!-- body { background-image: url(styles/downloaded%20styles/todo/todo/images/bg.png); } --> </style> <link href="styles/downloaded styles/todo/todo/css/style.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" href="styles/downloaded styles/todo/todo/css/style9.css" /> <link rel="stylesheet" type="text/css" href="styles/downloaded styles/todo/todo/css/demo.css" /> <link href='http://fonts.googleapis.com/css?family=Terminal+Dosis' rel='stylesheet' type='text/css' /> <style type="text/css"> <!-- .Stile1 {color: #333333} --> </style> </head> <body> <!--start container --> <div id="container"> <header> <nav> <div id="logo"><a href="index.php"><img src="images/header2.png" alt="Logo here" width="219" height="161" /></a> </div> <div id="search-top"> <form method="post" action="search.php"> <input type="text" name="strSearch" onFocus="if(this.value=='Search')this.value='';" onBlur="if(this.value=='')this.value='Search';" value="Search" id="search-field"/> <input type="submit" value="" id="search-btn"/> </form> </div> <div id="nav_social"><a href="http://www.facebook.com/pages/Removalspace/181434181939226"><img src="styles/downloaded styles/todo/todo/images/facebook_32.png" alt="Become a fan" width="32" height="32" /></a><a href="#"><img src="styles/downloaded styles/todo/todo/images/twitter_32.png" alt="Follows on Twitter" /></a><a href="#"><img src="styles/downloaded styles/todo/todo/images/linkedin_32.png" alt="Linked in" /></a><a href="contact.php"><img src="styles/downloaded styles/todo/todo/images/email_32.png" alt="Contact" width="32" height="32" /></a> </div> </nav> </header> <p><a href="removals.php">Search Removals</a></p> <p><a href="storage.php">Search Storage</a></p> <p><a href="about.php">About</a></p> <p><a href="contact.php">Contact</a></p> <div class="content"> <!--star main --> <main></main> <!--end main --> <!--start middle --> <middle> <div class="section_slogan"> <?PHP // validate postcode search function is_valid_uk_postcode($postcode) { $pattern = "/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/"; if (preg_match($pattern, $postcode)) { return TRUE; } $this->validation->set_message('is_valid_uk_postcode', 'That is not a valid %s.'); return FALSE; } $error_message = ""; if((!isset($_POST['strSearch'])) || (strlen(trim($_POST['strSearch'])) <5) || (trim($_POST['strSearch']) != preg_replace("/[^a-zA-Z0-9\_]/", "", trim($_POST['strSearch'])))) { $error_message = "You must enter a valid postcode<br>"; $error_message = $error_message . "Valid postcodes must not contain and spaces and consist of letters and numbers only.<br>"; $error_message = $error_message . 'Your invalid name was: <font color="red">' . $_POST['strSearch'] . "</font><hr>"; }else{ $postcode = mysql_real_escape_string(trim($_POST['strSearch'])); } // Get the search variable from URL $postcode_results = $_POST['strSearch']; $trimmed = trim($postcode_results); //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($postcode_results)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } // Build SQL Query $query = "(SELECT * FROM companies WHERE postcode like '%$trimmed%') ORDER BY postcode"; // EDIT HERE and specify your table and field names for the SQL query // If we have no results, offer a google search as an alternative if ($numrows == 0) { } // 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: "" . $postcode_results . ""</p>";"<br>"; "<hr>"; // begin to show results set echo "<a href=\"localarea.php?postcode_results=$postcode_results\">Results for $q;</a>"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row['postcode']; $name = $row['company_name']; echo " $title" ; echo " $company_name" ; $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=$postcode_results\"><< Prev 10</a>  "; } // 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=$postcode_results\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> <?php echo "<a href=\"view00.php?postcode_results=$postcode_results\">"$postcode_results"</a>";?> <a href="view00.php?id=<?PHP echo $row['id']; ?>"><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></a> Noticed i have put in <a href="view00.php?id to try to pass the id of the results from the table but on click there's NOTHING in the view page, also iv'e noticed that on hover it's: www.removalspace.com/view00.php?postcode_results="ws122sh". So the id isn't being found, how do i get it to go to view00.php using the id? I know theres a lot here but i am stuck here Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.