Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP search query header
how can i echo out the search query results to another page.. I know you have to save it in $_session then header it out but i can figure it out.. help me please! the search bar would be on top of my page.. and would this fix the "Confirm Form Resubmission" prompt?
 
<?php
 
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
{
    $sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH(page_title,page_body) AGAINST ('$searchquery'))";
}
include_once("db_connects.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
    $search_output .= "<hr />$count results for $searchquery<hr />";
     while($row = mysql_fetch_array($query)){
 
    $id = $row["id"];
    $title = $row["title"];
    $link = $row["links"];
    $body = $row["page_body"];
 
    $search_output .= "<a href='".$link."'>".$title."</a> - $body<br>";
 
    } // close while
} else {
    $search_output = "<hr />0 results for $searchquery<hr />";
 
  
}
}
  ?>
Posted 4-Apr-13 19:13pm
Comments
Prasad Khandekar at 5-Apr-13 2:11am
   
Hello Suzanne,
 
Why can't this page output the search reasults? Consider using a framework like CodeIgnitor or Prado.
 
Regards,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi prasad,
 
this code would be in my header which is where the search bar would be. if someone types in a search the results would have to show up on another page. im pretty new to coding and never heard of CodeIgnitor or Prado... i just want to php this result to another page
  Permalink  
Comments
Prasad Khandekar at 5-Apr-13 2:39am
   
Suzane use Have a Question or Comment? button to post the comments.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello Suzzane,
 
You probably do not need to redirect to another page. I am assuming that links are seperated by a semicolon and each link is a well formed html just like the page body. If these assumptions are corret then you can alter your page as shown below.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("db_connects.php");
 
if (isset($_POST['searchquery']) && $_POST['searchquery'] != "") {
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
 
    $sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH(page_title,page_body) AGAINST ('$searchquery'))";
 
    $result = mysql_query($sqlCommand) or die(mysql_error());
    $row = mysql_fetch_row($result);
    if ($row) {?<
<html>
<head>
<title><?=$row[3]?></title>
<?php
$links = exlpode(';', $row[1]);
foreach ($array as $i => $links) {
    echo $links[$i];
}?>
</head>
<body><?=$row[2]?></body>
</html>
<?php
    }
    mysql_free_result($result);
}
 
Regards,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I have solved my own question. now the newpage.php would have to session_start(); then foreach throught the values.
 

<?php
 
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
include_once("db_connects.php");
 
$queryArray = array();
$goodQuery = true;
$search_output = "";
 
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
{
    $sqlCommand = "(SELECT id, links, page_body, page_title AS title FROM pages WHERE MATCH (page_title,page_body) AGAINST ('$searchquery'))";
}
 
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
 
if($count > 1){
 
    $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
 
    while($row = mysql_fetch_assoc($query)){
        $queryArray[] = $row;
    }
}
else {
     $goodQuery = false;
 
}
 
if($goodQuery){
 
    $_SESSION['search_output'] = $queryArray;
    header("Location: newpage.php");
 
    exit;
}
else{
    echo $search_output;
}
}
  ?>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 547
1 ChauhanAjay 180
2 CHill60 135
3 Maciej Los 125
4 Richard Deeming 125
0 Sergey Alexandrovich Kryukov 9,735
1 OriginalGriff 6,987
2 CPallini 5,835
3 George Jonsson 4,017
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100