2 Replies - 1099 Views - Last Post: 20 April 2013 - 04:36 AM

#1 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

[php/jq] Live search problem

Posted 14 April 2013 - 06:08 AM

Hi,

I tried to search forum within this tags but cannot receive any results. So there is my problem:

I want to make live search which shows me results in specified div. I made this script which is located at the bottom of index page:
$(function(){
$("#q").keyup(function(){
	var q = $(this).val();
	
	$.get("base.php?q="+q, function(data){
		if(q){
			$(".results").show();
			$(".results").html(data);
		} else{
			$(".results").hide();
			$(".results").html("");
		}
	});
});
});

I have put two titles in db: 'league of legends' and 'league of heroes'. I want to fetch titles based on query from search input. Last time i tried to this, php file gave me everything from db and insert it at the top of file instead of selected in js div (.results).

there is php file:
<?php
//connection stuff
$c = mysql_connect("", "", "");
$db = mysql_select_db("", $c);

//page number
if($_GET["page"]){
	$pagenum = $_GET["page"];
} else{
	$pagenum = 1;
}

$rowsperpage = 2;
$offset = ($pagenum - 1) * $rowsperpage;

//keywords
$t = trim(eregi_replace(" +", " ", $_GET["q"]));
$x = explode(" ", $t);

foreach($x as $z){
	$w++;
	if($w==1){
		$u .= "channel LIKE '%$z%'";
	} else{
		$u .= "OR channel LIKE '%$z%'";
	}
}

//page of results
$q = mysql_query("SELECT * FROM channels WHERE $u ORDER BY id DESC LIMIT $offset, $rowsperpage");
$page_nums = mysql_num_rows($q);

$total_q = mysql_query("SELECT * FROM channels WHERE $u");
$total_nums = mysql_num_rows($total_q);
$total_pages = ceil($total_nums/$rowsperpage);

if($total_nums){
	if($pagenum<1||$pagenum>$total_pages){
		header("Location: base.php?q=$t");
	}
	while($r=mysql_fetch_array($q)){
		$channel = $r["channel"];
		$id = $r["id"];
		
		echo '<div class="result"><a class="link" href="?channel='.$id.'">'.$channel.'</a></div>';
	}
	$range = 2;
	
	if($pagenum>1){
		$page = $pagenum - 1;
		$first = '<a class="page" id="1">First</a> ';
		$prev = '<a class="page" id="'.$page.'">Prev</a> ';
	}
	
	if($pagenum<$total_pages){
		$page = $pagenum + 1;
		$next = '<a class="page" id="'.$page.'">Next</a> ';
		$last = '<a class="page" id="'.$total_pages.'">Last</a> ';
	}
	
	for($page=($pagenum-$range); $page<=($pagenum+$range); $page++){
		if($page>=1&&$page<=$total_pages){
			if($total_pages>1){
				if($page==$pagenum){
					$nav .= '<span class="pagenum">'.$page.'</span> ';
				} else{
					$nav .= '<a class="page" id="'.$page.'">'.$page.'</a> ';
				}
			}
		}
	}
	
	echo $first . $prev . $nav . $next . $last;
} else{
	echo 'No results for <b>"'.$t.'"</b>';
}
?>


I want this shadow box to appear if results were found, ofc with results in it (this box has 'results' class).
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: [php/jq] Live search problem

#2 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,848
  • Joined: 30-April 10

Re: [php/jq] Live search problem

Posted 17 April 2013 - 03:54 PM

in your script you use q instead of data for the if statement, try data

The function is returning the information from your script in a variable called "data", not "q".

This post has been edited by laytonsdad: 17 April 2013 - 04:13 PM

Was This Post Helpful? 1
  • +
  • -

#3 Muphet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: [php/jq] Live search problem

Posted 20 April 2013 - 04:36 AM

oh wow, it actually works. Thanks mate, I really appreciate this
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1