write better search engine in php

my limited mysql knowledge always produced inaccurate search results when i write the board search engine. i want to do better this time for the new board so i’m reading this and trying out mysql regular expression and full text search. i’m pretty excited.

oh also, leslie reminded me. this perl style coding will save you from using back slashes and stuff like

<?=$var?>

PHP CODE:

<?php
f(isset($j_sidebar)) { 
	echo <<<EOF 
	<div id="side"> 
	{$j_sidebar} 
	</div>
	<!--/side--> 
	EOF; 
} 
echo <<<EOF 
<div id="main"> 
{$j_main} 
</div><!--/main--> 
</div><!--/outer--> 
</body> 
</html> 
EOF;
?>

vs.

PHP CODE:

if(isset($j_sidebar)) { 
	echo "<div id=\"side\"> $j_sidebar </div><!--side-->"; 
} 
echo "<div id=\"main\"> $j_main </div><!--/main--> </div><!--/outer--> </body> </html>";</code>

i’ve been using the second for too long, it’s hard to change but it certainly doesn’t hurt to try something different :)

hehe another tip from leslie, you can insert multiple records into one table using just one query:

INSERT INTO blah (blah, blah) VALUES (blah, blah), (blah, blah), ....

the mystery of querying

if i know (or remember) more from my database class, i may have an answer for this. i was just coding my new board and was curious about which of the following query is faster:

SELECT m.regdate, m2.postnum FROM dmb_members m, dmb_members_post m2 WHERE m.uid=m2.uid AND m.uid = '".$userid."' LIMIT 1

or

SELECT m.regdate, m2.postnum FROM dmb_members m, dmb_members_post m2 WHERE m2.uid = '".$userid."' AND m.uid = '".$userid."' LIMIT 1

m.uid is a primary index while m2.uid is an index. what would be your guess?
Continue reading

save arrays into database

i’ve known this for a while but forgot to share it. there is a very easy way you can store array into database as a string. then you can grab the string from the database and convert it back into an array.

code example:

<?php 
$myarray[] = "meow"; 
$myarray[] = "woof"; 
$myarray[meow] = "cool"; 
$myarray[woof] = "cool"; 
$myarray[thisrules][0] = "Mmmm"; 
$text_myarray = serialize($myarray); 
// store it into my database (depend on the length of your array, declare the column as text or varchar) 
// pull it out of my database, suppose $out_of_my_database stores the text from your database column 
// then $get_myarray = unserialize($out_of_my_database); echo "<pre>"; print_r($get_myarray); 
?>

you should get

Array
(
    [0] => meow
    [1] => woof
    [meow] => cool
    [woof] => cool
    [thisrules] => Array
        (
            [0] => Mmmm
        )

)

separate template from code

when you do serious coding, it’s always a good idea to separate your layout design (template) from your logic code. i’m at the beginning stage rebuilding dmb. one thing i want to do very badly is to get rid of all templates in the database. every time i load a template right now it causes me a query. i want to store my templates in files because file_get_contents is less expensive than a query. and i just figured out a very easy way to do that.

store your template in a file. it can be html, text or php, whatever suits your need.

put this in your html file, name it “test.html”

this is so $test

and then use the code below:

<?php 
$test = "cool"; 
$meep = file_get_contents("test.html"); 
eval("\$meep = \"$meep\";"); 
echo $meep; 
?>

and it will print out “this is so cool” ^_^