Forum Moderators: open

Message Too Old, No Replies

Hard Search with three tables

Hard Search

         

MrGecko

11:03 pm on Jul 19, 2008 (gmt 0)

10+ Year Member



Hello I am working on a search engine for my site and I am needing help to have the tables in one quarry so I can limit it and have it only show like 10 per a page.
The structures are different from each other so I would need a way to tell which table it is from so I can display the result right for that table.
I don't know if this is important but here is how my structure is for the tables.

Table applications
id int(11)
location int(11)
name varchar(255)
version varchar(255)
icon varchar(255)
file varchar(255)
time int(11)
utime int(11)
category varchar(255)
plat int(1)
sos varchar(255)
os varchar(255)
architecture varchar(255)
requirements mediumtext(0)
sdescription varchar(255)
description mediumtext(0)
keywords varchar(255)
history mediumtext(0)
downloads int(11)
vdownloads int(11)
apppath varchar(255)

Table posts
id int(11)
data mediumtext(0)
forum int(11)
thread int(11)
username varchar(255)
useremail varchar(255)
time int(11)

Table reviews
id int(11)
app int(11)
data mediumtext(0)
username varchar(255)
useremail varchar(255)
time int(11)

and here is the code I got now for retrieving how many there is in each.


$app_query = "SELECT * FROM applications WHERE";
$posts_query = "SELECT * FROM posts WHERE";
$reviews_query = "SELECT * FROM reviews WHERE";
$i=0;
foreach ($qs as $qed) {
if ($i!=0)
$app_query .= " OR";
$app_query .= " name LIKE '%$qed%' OR description LIKE '%$qed%' OR category LIKE '%$qed%' OR keywords LIKE '%$qed%' AND hidden=0";
if ($i!=0)
$posts_query .= " OR";
$posts_query .= " username LIKE '%$qed%' OR data LIKE '%$qed%'";
if ($i!=0)
$reviews_query .= " OR";
$reviews_query .= " username LIKE '%$qed%' OR data LIKE '%$qed%'";
$i++;
}
$result = mysql_query($app_query);
$numapps = mysql_num_rows($result);
mysql_free_result($result);
$result = mysql_query($posts_query);
$numposts = mysql_num_rows($result);
mysql_free_result($result);
$result = mysql_query($reviews_query);
$numreviewss = mysql_num_rows($result);
mysql_free_result($result);

Thanks for any help,
Mr. Gecko

MrGecko

4:01 am on Jul 20, 2008 (gmt 0)

10+ Year Member



I just decided to just make an array with every thing that was found.

rocknbil

4:28 pm on Jul 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at limit?

Putting found elements in an array is a solution, but one that will use up an unnecessary amount of memory. Additionally it will have to read in the whole table of results, and if it's quite large, will slow you down.

Something like this: you have a "get_foot_links" routine that counts the total results. You parse through the count and output links based on how many you want per page (logic only, not working code:)


$per_page=10;
for ($i=1;$i<=$total_records;$i+=$per_page) {
$end = $i+$per_page;
$foot_links .= "<a href=\"yourscript?start=$i&amp;per=$per_page\">$i\-$end</a> ¦ ";
}

(Simplified, you have to do something special for the last link if it's not equally divisible by $per_page.)

Then you use the start and per variables in your query string to build the limit clause in your select:


$select = "select * from $table";
if ($per_page > 0) {
$start=($start>0)?$start:0;
$select .= " limit $start,$per";
}

Which should give you

select * from $table where [conditions] limit 20, 10;

which would start on the 20th record found, grab the next 10, and stop querying at that point.

MrGecko

6:11 pm on Jul 20, 2008 (gmt 0)

10+ Year Member



I know how to limit per a page it is just that I have three tables to read from and I want to have it mixed.