Jump to content

Search feature


aeafisme23

Recommended Posts

This is a snippet of code from the search page i created. I have rigged it to pull results by erasing part of this

$searchcode_query = "SELECT * FROM contact_log WHERE station_id = '".$codes."'";

to

$searchcode_query = "SELECT * FROM contact_log ";

 

I understand my problem lies within the station_id = '".$codes . This script is being modified from another project i did using AREACODES such as 220 , 574 , etc. So the next line of code that i believe is the problem is the defining of $codes as it is converting or looking for an integer value (a number like 220), so when i put in text like (WUSA) it will say it searched for 0 'zero' because it's looking for an integer.

	foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

I am trying to figure out how i can modify this code to look for text and not an integer and still keep my code structure if at all possible. Any help would greatly be appreciated. Below is just more code in case you need it. if there is more code you need let me know :) Thanks again.

 

// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//trim whitespace from variable
$searchcode = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $searchcode), array(""));

if( !empty($searchcode) ) {
foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

$searchcode_query = "SELECT * FROM contact_log";

//Store the results in a variable or die if query fails
$result = mysql_query($searchcode_query) or die("Error executing MySQL query<br />".mysql_error());

$count = mysql_num_rows($result);
}

 

 

Link to comment
Share on other sites

I decided it might be more beneficial than waiting for someone to ask for more code so here is the complete page of code for viewing the record.

 

<?php
//Get variables from config.php to connect to mysql server
require 'config.php';

// connect to the mysql database server.
$connect = mysql_connect($dbhost, $dbusername, $dbuserpass) or die("Unable to connect to SQL server..<br />".mysql_error());
$selectDB = mysql_select_db($dbname) or die("Unable to select database.<br />".mysql_error());

//trim whitespace from variable
$searchcode = preg_replace('/\s+/', ' ', trim($_GET['search']));

//seperate multiple keywords into array space delimited
$keywords = array_diff(explode(" ", $searchcode), array(""));

if( !empty($searchcode) ) {
foreach($keywords as $k => $v) 
$keywords[$k] = (int)$v;	
$codes = implode(",", $keywords);

$searchcode_query = "SELECT * FROM contact_log";

//Store the results in a variable or die if query fails
$result = mysql_query($searchcode_query) or die("Error executing MySQL query<br />".mysql_error());

$count = mysql_num_rows($result);
}

/** html code **/
echo "<center><h1>Admin View / Delete Contact Log</h1><br><center><b>Other Admin Options Include: <a href=\"add_record_contact_log.php\">add a record</a> | <a href=\"view_records_contact_log.php\">view a record</a></b><br>
<br>Please put in an Stations Call Letters in all caps (KCNC, KFMB, WUSA, etc) to filter your search and the proceed to delete or to come back to other admin options.</center><br></center>";

//If users doesn't enter anything into search box tell them to.
if( empty($searchcode) ){
echo "<html><head>";
echo "<title>Admin View / Delete Records</title>";
echo "</head>";
echo "<body onLoad=\"self.focus();document.searchform.search.focus()\">";
echo "<center>";
echo "<br /><form name=\"searchform\" method=\"GET\" action=\"view_records_contact_log.php\">";
echo "<input type=\"text\" name=\"search\" size=\"20\" TABINDEX=\"1\" />";
echo " <input type=\"submit\" value=\"Search\" />";
echo "</form>";
} else {
if( $count == 0 ){
	echo "<center>Your query returned no results from the database, redirect.</center>";
} else {
	echo "<center><b>Admin - View Records for Contact Log: ".$codes."</b></center><br>";
	$row_count = 0;

	// Define the colours for the alternating rows 
	$colour_odd = "#FDFDEA"; 
	$colour_even = "#E0E0C7"; 

	// If Remainder of $row_count divided by 2 == 0. 
	$row_color = (($row_count % 2) == 0) ? $colour_even : $colour_odd; 

	while( $row = mysql_fetch_array($result) ) {
		//table background color = row_color variable
		echo "<center><table width=\"800\" cellpadding=\"0\" cellspacing=\"2\">";
		echo "<tr>";
		echo "<td width=\"50\" valign=\"top\">".$row['station_id']."</td>";
		echo "<td width=\"100\" valign=\"top\">".$row['contact_date']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['contact_person']."</td>";
		echo "<td width=\"150\" valign=\"top\">".$row['contact_kv']."</td>";
		echo "<td width=\"300\" valign=\"top\">".$row['contact_notes']."</td>";

		echo "<td width=\"50\" valign=\"top\"><a href=\"delete_record_contact_log.php?id=".$row['id']."\">Delete</a></td>";
		echo "</tr></table></center>";
		$row_count++;
	}

}
}

if( isset($result) ) {
mysql_free_result($result);
mysql_close($connect);
}
?>

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.