Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
<?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
Comments
Prasad Khandekar 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,

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


PHP
<?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;
}
}
  ?>
 
Share this answer
 
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
<?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,
 
Share this answer
 
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
 
Share this answer
 
Comments
Prasad Khandekar 5-Apr-13 2:39am    
Suzane use Have a Question or Comment? button to post the comments.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900