Start New Discussion within our Web Development Community

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.

which one? And if that has something to do with it I'm guessing php compiler exits the function before its finished executing? How and why? Is it like java in the sense it will exit pre-maturely on error without indicating an error?

which one?

Each one.

lets take this on for example:

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;
}

the while loop is executing and will echo only once, when the while loop has finished and will use the last result.

function check($arr,$name)
{
    $found = false;
    $c=0;
    while(isset($arr[$c])&&!$found)
        {
        $found = $arr[$c]['item_name'] == $name;
        echo "Check: ".$found.", search for: ".$name.", Count: ".$c;
        $c++;
        }
    return $found;
}

Now it will echo during the loop as it executes. This is common logic.

Look here for explaination:

http://php.net/manual/en/control-structures.while.php

Comments
Nicely explained

I did notice

q[0] = query

in my code (q being an undeclared variable) and promptly changed it to

queries[0] = query

but it didn't change the outcome. I keep forgetting PHP is a weakly typed language. I tested

<?php
echo "before";
echo q[0];
echo "after"
?>

the ouput was "beforeafter" in the browswer.

I was hoping that the undefined variable would cause the program to crash but it didn't. So my guess I have another undefined variable somewhere in there and that might explain why nothing is happening.

bump

so I modified the code a 'lil bit:

else if(($vars-1)<1) //1, 1.2, 1.23, 1.3
    {
        echo "<p>in else if, value of vars: ".$vars."</p>";
        $vars=$vars-1.0;
        if($vars==0) //1
        {
            $match = sort_soundex_results($queries[0]);
        }
        else if($vars==.2) //1.2
        {
            echo "<p>made it to else if</p>"; //never executes
            $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]));
        }
    }

result:

begin search

if name
begin gsq

Soundex: SELECT *, SOUNDEX('squat rack') AS src, SOUNDEX(item_name) AS src2, 'squat rack' SOUNDS LIKE item_name AS result FROM Item

if category

End if category, query: Resource id #4, vars: 1.2

in else if, value of vars: 1.2

So after a long reprieve, I narrowed it down. still confused as to what's wrong.

whole code, updated:

function get_soundex_query($str,$member)
{
    echo "begin gsq\n";
    $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 "<p>Soundex: ".$ss."</p>";
    return $q;
}
function sort_mysql_results()
{
    echo "<p>begin smr</p>";
    $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]))
    {
        echo "<p>sort mysql beginning of loop</p>";
        $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 "<p>Num args: ".$num_args.", Iterations: ".$num_it."</p>";
    return $match;
}
function check($arr,$name)
{
    echo "<p>begin check</p>";
    if(!isset($arr) || !isset($name))
    {
        echo "<p>in check, arr and name aren't set!</p>";
    }
    $found = false;
    $c=0;
    while(isset($arr[$c])&&!$found)
    {
        echo "<p>in check beginning in loop</p>";
        $found = $arr[$c]['item_name'] == $name;
        $c++;
    }
    echo "<p>Check: ".$found.", search for: ".$name.", Count: ".$c."</p>";
    return $found;
}
function sort_soundex_results($m_q)
{
    echo "<p>begin ssr</p>";
    if(!isset($m_q))
    {
        echo "<p>In sort_soundex_results, m_q isn't set!</p>";
    }
    $result = array();
    $c = 0;
    $chrs=0;
    $nums=0;
    $base=1;
    $i=0;
    while($i = mysql_fetch_assoc($m_q))
    {
        echo "<p>in sort soundex beginning of loop</p>";
        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 "<p>chrs: ".$chrs.", nums: ".$nums.",base: ".$base.", count: ".$c."</p>";
    return $result;
}
function get_array($m_q)
{
    echo "<p>begin get_array</p>";
    if(!isset($m_q))
    {
        echo "<p>In get_array, m_q isn't set!</p>";
    }
    $result = array();
    $c = 0;
    while($i = mysql_fetch_assoc($m_q))
    {
        echo "<p>in get_array beginning of loop</p>";
        $result[$c] = $i;
        $c++;
    }
    echo "<p>get_array: c: ".$c."</p>";
    return $result;
}
if(isset($_POST['name'])||isset($_POST['category'])||isset($_POST['descr']))
{
    echo "<p>begin search</p>";
    $con = mysql_connect('localhost','jddancks','csc255');
    mysql_select_db('dancks_db',$con);
    $queries = array();
    $vars = 0;
    if(isset($_POST['name'])&&($_POST['name']!=""))
    {
        echo "<p>if name</p>";
        $vars = 1;
        $queries[0] = get_soundex_query($_POST['name'],"item_name");
    }
    if(isset($_POST['category'])&&($_POST['category']!=""))
    {
        echo "<p>if category</p>";
        if($vars>0)
        {
            $vars=1.2;
        }
        else
        {
            $vars=2;
        }
        $queries[1] = mysql_query(sprintf("SELECT ItemID,item_name,descr FROM Item WHERE cat_name='%s'",$_POST['category'])) or die("Second query: ".mysql_error());
        echo "<p>End if category, query: ".$queries[1].", vars: ".$vars."</p>";
    }
    if(isset($_POST['descr'])&&($_POST['descr']!=""))
    {
        echo "<p>if descr</p>";
        if($vars==1.2)
        {
            $vars = 1.23;
        }
        else if($vars==1)
        {
            $vars=1.3;
        }
        else if($vars==2)
        {
            $vars=2.3;
        }
        else
        {
            $vars=3;
        }
        $queries[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
    {
        echo "<p>in else if, value of vars: ".$vars."</p>";
        $vars=$vars-1.0;
        if($vars==0) //1
        {
            $match = sort_soundex_results($queries[0]);
        }
        else if($vars==.2) //1.2
        {
            echo "<p>made it to else if</p>";
            $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]));
        }
    }
}
?>

from that page:

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs\n";
}

foo(1, 2, 3);   
?>

I think I did alright. I did figure out that the problem lay with the if/else statements doing the query so I rewrote them to see if that would fix it and now My code makes it all the way to the end:

$match = array();
    if($vars==3)
    {
        $match = sort_soundex_results($queries[2]);
    }
    else if($vars>2)
    {
        $match = sort_mysql_results(get_array($queries[1]),sort_soundex_results($queries[2]));
    }
    else if($vars>1)
    {
        if($vars==2)
        {
            $match = sort_mysql_results(get_array($queries[1]));
        }
        else if($vars==1.23)
        {
            $match = sort_mysql_results(sort_soundex_results($queries[0]),get_array($queries[1]),sort_soundex_results($queries[2]));
        }
        else if($vars==1.3)
        {
            $match = sort_mysql_results(sort_soundex_results($queries[0]),sort_soundex_results($queries[2]));
        }
        else //1.2
        {
            echo "<p>Made it to else 1.2</p>\n";
            $match = sort_mysql_results(sort_soundex_results($queries[0]),get_array($queries[1]));
        }
    }
    else //1
    {
        $match = sort_mysql_results(sort_soundex_results($queries[0]));
    }
}
?>

I think next time I'll figure out to implement a binary decision tree of some sort.

Although funny you should single it out I am now having trouble with the function you just mentioned. I think the result isnt returning a 2d array like I desired.

I should be able to do:

$match = sort_mysql_results(sort_soundex_results($queries[0]));
echo $match[0]['item_name'].", ".$match[0]['cat_name'].", ".$match[0]['descr'];

have you tried dumping the information to make sure your variable is being populated?

var_dump($match);

Yes I did. results:

begin search

if name
begin gsq

Soundex: SELECT *, SOUNDEX('squat rack') AS src, SOUNDEX(item_name) AS src2, 'squat rack' SOUNDS LIKE item_name AS result FROM Item

if category

End if category, query: Resource id #4, vars: 1.2

Made it to else 1.2

begin ssr

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

in sort soundex beginning of loop

chrs: S, nums: 362,base: 100, count: 1
array(1) { [0]=> array(12) { ["item_name"]=> string(10) "Squat Rack" ["ItemID"]=> string(1) "8" ["cat_name"]=> string(16) "Sports Equipment" ["userID"]=> string(1) "1" ["descr"]=> string(25) "Comes with 275 lbs plates" ["image"]=> string(14) "squat_rack.jpg" ["date"]=> string(19) "2012-10-15 09:34:36" ["highest_bid"]=> string(4) "0.00" ["time_expire"]=> string(19) "2012-12-01 00:00:01" ["src"]=> string(4) "S362" ["src2"]=> string(4) "S362" ["result"]=> string(1) "1" } }

begin get_array

in get_array beginning of loop

in get_array beginning of loop

get_array: c: 2
array(2) { [0]=> array(3) { ["ItemID"]=> string(1) "6" ["item_name"]=> string(19) "Remington 20ga 1100" ["descr"]=> string(14) "you'll love it" } [1]=> array(3) { ["ItemID"]=> string(1) "8" ["item_name"]=> string(10) "Squat Rack" ["descr"]=> string(25) "Comes with 275 lbs plates" } }

begin smr

sort mysql beginning of loop

begin check

in check beginning in loop

in check beginning in loop

Check: 1, search for: Squat Rack, Count: 2

Num args: 2, Iterations: 1
array(1) { [0]=> array(1) { [0]=> array(12) { ["item_name"]=> string(10) "Squat Rack" ["ItemID"]=> string(1) "8" ["cat_name"]=> string(16) "Sports Equipment" ["userID"]=> string(1) "1" ["descr"]=> string(25) "Comes with 275 lbs plates" ["image"]=> string(14) "squat_rack.jpg" ["date"]=> string(19) "2012-10-15 09:34:36" ["highest_bid"]=> string(4) "0.00" ["time_expire"]=> string(19) "2012-12-01 00:00:01" ["src"]=> string(4) "S362" ["src2"]=> string(4) "S362" ["result"]=> string(1) "1" } } }
Search Results:

Notice: Undefined index: ItemID in /u/students/j/j.d.dancks/public_html/onestopshop/newsearchresults.php on line 234

Notice: Undefined index: item_name in /u/students/j/j.d.dancks/public_html/onestopshop/newsearchresults.php on line 234

Notice: Undefined index: cat_name in /u/students/j/j.d.dancks/public_html/onestopshop/newsearchresults.php on line 234

It looks OK. I don't understand it.

can you advise this part of code:
Notice: Undefined index: ItemID in /u/students/j/j.d.dancks/public_html/onestopshop/newsearchresults.php on line 234

From this:

<body>\n
<h1>Search Results:</h1>\n
<hr />\n";
        if(count($match)>0)
        {
                $num=0;
                echo "<table>\n
                <tr>\n
                <td>Name</td><td>Category</td><td>Description</td>\n
                </tr>\n";
                while(isset($match[$num]))
                {
                        echo "<tr>\n
                        <a href=\"product2.php?id=".$match[$num]['ItemID']."\"><td>".$match[$num]['item_name']."</td><td>".$match[$num]['cat_name']."</td><td>".$match[$num]['descr']."</td></a>\n
                        </tr>\n";
                        $num++;
                }
                echo "</table>\n";
        }
        else
        {
                echo "<p>No matches found. Go back and try different search criteria</p>\n";
        }
        echo "</body>\n
                </html>";
}

Line 14 echos the contents of an instance of $match. hence "ItemID"

I added

echo "<h2>VALUE OF match[num]</h2>\n";
        echo "<p>space</p><p>space</p><p>space</p>\n";
        var_dump($match[0]);

result (formatted):

array(1) 
{ 
    [0]=> array(12) 
    { 
        ["item_name"]=> string(10) "Squat Rack" 
        ["ItemID"]=> string(1) "8" 
        ["cat_name"]=> string(16) "Sports Equipment" 
        ["userID"]=> string(1) "1" 
        ["descr"]=> string(25) "Comes with 275 lbs plates" 
        ["image"]=> string(14) "squat_rack.jpg" 
        ["date"]=> string(19) "2012-10-15 09:34:36" 
        ["highest_bid"]=> string(4) "0.00" 
        ["time_expire"]=> string(19) "2012-12-01 00:00:01" 
        ["src"]=> string(4) "S362" 
        ["src2"]=> string(4) "S362" 
        ["result"]=> string(1) "1" 
    } 
}

The plot thickens. Any ideas what I should look for?

solved. Modified sort_mysql_results:

function sort_mysql_results()
{
    echo "<p>begin smr</p>";
    $match=array();
    $c_match=0;
    $num_it = 0;
    $c = 0;
    $args = func_get_args();
    while(isset($args[0][$num_it]))
    {
        echo "<p>sort mysql beginning of loop</p>";
        $skip=false;
        $name = $args[0][$num_it]['item_name'];
        $i=1;
        while(isset($args[$i])&&!$skip)
        {
            $skip = !check($args[$i],$name);
            $i++;
        }
        if(!$skip) //if found in another parameter
        {
            $match[$c_match] = $args[0][$num_it];
            $c_match++;
        }
        $num_it++;
    }
    echo "<p>Num args: ".$num_args.", Iterations: ".$num_it."</p>";
    var_dump($match);
    return $match;
}

Not only did it not save the proper array, it saved a 2D array making $match a 3D array.

This question has already been answered. Start a new discussion instead.