Based off some stuff from my last thread I tried to write a search results page using maximum 3 parameters. I used a couple of function to try and get everything organized. One: sort_mysql_results has no defined parameters so I can put in as many parameters as needed. The code itself doesn't throw any errors but it obviously doesn't work. I tried debugging by adding echo to each function to see what is being executed. I find only one executes.
Code: (kinda long, sorry)
<?php
function get_soundex_query($str,$member)
{
$ss = sprintf("SELECT *, SOUNDEX('%s') AS src, SOUNDEX(%s) AS src2",$str,$member);
$ss = $ss.sprintf(", '%s' SOUNDS LIKE %s AS result FROM Item",$str,$member);
$q = mysql_query($ss) or die("Soundex query function:\n".mysql_error().sprintf("\nWith %s as str, %s as member",$str,$member));
echo "Soundex: ".$ss;
return $q;
}
function sort_mysql_results()
{
$match=array();
$c_match=0;
$num_it = 0;
$num_args = func_num_args();
$c = 0;
$args = func_get_args();
while(isset($args[0][$num_it]))
{
$skip=false;
$name = $args[0][$num_it]['item_name'];
$i=1;
while(isset($args[$i])&&!$skip)
{
$skip = !check($args[$i],$name);
$i++;
}
$num_it++;
if(!$skip)
{
$match[$c_match] = $args[0];
$c_match++;
}
}
echo "Num args: ".$num_args.", Iterations: ".$num_it;
return $match;
}
function check($arr,$name)
{
$found = false;
$c=0;
while(isset($arr[$c])&&!$found)
{
$found = $arr[$c]['item_name'] == $name;
$c++;
}
echo "Check: ".$found.", search for: ".$name.", Count: ".$c;
return $found;
}
function sort_soundex_results($m_q)
{
$result = array();
$c = 0;
$chrs=0;
$nums=0;
$base=1;
while($i = mysql_fetch_assoc($m_q))
{
if($chrs==0)
{
$chrs = $i['src'][0];
$nums = intval(substr($i['src'],1));
while(($nums/$base)>1) $base*=10;
$base = $base/10;
}
$chrd = $i['src2'][0];
$numd = intval(substr($i['src2'],1));
if(($chrs==$chrd)&&(abs($nums-$numd)<=$base))
{
$result[$c] = $i;
$c++;
}
}
echo "chrs: ".$chrs.", nums: ".$nums.",base: ".$base.", count: ".$c;
return $result;
}
function get_array($m_q)
{
$result = array();
$c = 0;
while($i = mysql_fetch_assoc($m_q))
{
$result[$c] = $i;
$c++;
}
echo "get_array: c: ".$c;
return $result;
}
if(isset($_POST['name'])||isset($_POST['category'])||isset($_POST['descr']))
{
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('dancks_db',$con);
$queries = array();
$vars = 0;
if(isset($_POST['name'])&&($_POST['name']!=""))
{
$vars = 1;
$q[0] = get_soundex_query($_POST['name'],"item_name");
}
if(isset($_POST['category'])&&($_POST['category']!=""))
{
if($vars>0)
{
$vars=1.2;
}
else
{
$vars=2;
}
$q[1] = mysql_query(sprintf("SELECT ItemID,item_name,descr FROM Item WHERE cat_name='%s'",$_POST['category'])) or die("Second query: ".mysql_error());
}
if(isset($_POST['descr'])&&($_POST['descr']!=""))
{
if($vars==1.2)
{
$vars = 1.23;
}
else if($vars==1)
{
$vars=1.3;
}
else if($vars==2)
{
$vars=2.3;
}
else
{
$vars=3;
}
$q[2] = get_soundex_query($_POST['descr'],"descr");
}
$match = array();
if($vars==3)
{
$match = sort_soundex_results($queries[2]);
}
else if(($vars-1)<1) //1, 1.2, 1.23, 1.3
{
$vars-=1;
if($vars==0) //1
{
$match = sort_soundex_results($queries[0]);
}
else if($vars==.2) //1.2
{
$match = sort_mysql_results(sort_soundex_results($queries[0]),get_array($queries[1]));
}
else if($vars==.23) //1.23
{
$match = sort_mysql_results(sort_soundex_results($queries[0]),get_array($queries[1]),sort_soundex_results($queries[2]));
}
else if($vars==.3) //1.3
{
$match = sort_mysql_results(sort_soundex_results($queries[0]),sort_soundex_results($queries[2]));
}
}
else if(($vars-2)<1) //2, 2.3
{
$vars-=2;
if($vars==0) //2
{
$match = get_array($queries[1]);
}
else if($vars==.3) //2.3
{
$match = sort_mysql_results(get_array($queries[1]),sort_soundex_results($queries[2]));
}
}
}
?>
$_POST['name'] = "squat rack".
$_POST['category'] = "sports equipment.
$_POST['descr'] unset.
$vars tells me what variables are set/queries made. i.e 1.3 means input for 'name and descr' and 1.23 means 'name' 'category' and 'descr' are set. 2 just 'category', etc.
result from running the PHP:
Soundex: SELECT *, SOUNDEX('squat rack') AS src, SOUNDEX(item_name) AS src2, 'squat rack' SOUNDS LIKE item_name AS result FROM Item
I think I'm having a problem with variable scope but I'll be damned if I know what I did wrong.