Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    Array not working

    I don't think this is the right way of writing the array. It's not working:
    PHP Code:
        $newSearchTheseArr = array(
            for (
    $j=0$j count($newSearchTheseArr); $j++){
                
    $query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
                if(
    $j!=count($newSearchTheseArr)-1){
                    
    $query.=" +";
                    }else{
                    
    //removes the OR from the last line and replaces with the following    
                    
    $query.= " > 3";
                    }
                }
            ); 
    I get:
    Parse error: syntax error, unexpected T_FOR, expecting ')' in ...\searchLinkFiles\linkTextareaSearchResultFunctioning.php on line 67

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'll take a stab:
    Edit:
    Guess I should clarify. The error you're receiving is because PHP doesn't know what the value of the below is in context to how to put it into an array. So, its telling you that it only knows how to treat it as a string, so you need to insert the data as a string. This is wrong for what you want to do, but keep reading and I'll show you what it is.

    PHP Code:
        $newSearchTheseArr = array(
            for (
    $j=0$j count($newSearchTheseArr); $j++){ 
    This says that you're trying to create an array with the inner value of a loop referring to the current array. I can't see how this would work, count($newSearchTheseArr) would be undefinable since it doesn't know how to construct the fields correctly. I'm guessing you want to check if $newSearchTheseArr is an array, so do so like:
    PHP Code:
    // Don't use is_array; $newSearchTheseArr should have been forward declared as one so this would be true.
    $countSearchArr count($newSearchTheseArr);
    if (
    $countSearchArr 0// or if ($countSearchArr)
    {
            for (
    $j=0$j $countSearchArr$j++){
                
    $query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
                if(
    $j!=$countSearchArr-1){
                    
    $query.=" +";
                    }else{
                    
    //removes the OR from the last line and replaces with the following    
                    
    $query.= " > 3";
                    }
                }

    I don't think the query will be valid with the '+' or >3, but at least it should execute the PHP.
    Last edited by Fou-Lu; 07-30-2008 at 09:55 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    I'll take a stab:
    PHP Code:
        $newSearchTheseArr = array(
            for (
    $j=0$j count($newSearchTheseArr); $j++){ 
    This says that you're trying to create an array with the inner value of a loop referring to the current array. I can't see how this would work, count($newSearchTheseArr) would be undefinable since it doesn't know how to construct the fields correctly. I'm guessing you want to check if $newSearchTheseArr is an array, so do so like:
    PHP Code:
    $countSearchArr count($newSearchTheseArr);
    if (
    $countSearchArr 0)
    {
            for (
    $j=0$j $countSearchArr$j++){
                
    $query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
                if(
    $j!=$countSearchArr-1){
                    
    $query.=" +";
                    }else{
                    
    //removes the OR from the last line and replaces with the following    
                    
    $query.= " > 3";
                    }
                }

    I don't think the query will be valid with the '+' or >3, but at least it should execute the PHP.
    Ok. I need to step back.
    How do you filter out the small words after exploding the string?
    PHP Code:
    $searchThese "the who and but Moses flock Jethro father priest Midian flock backside desert mountain Horeb angel appeared flame midst looked behold burned consumed Moses aside great sight burnt";
    $searchTheseArray explode(' '$searchThese);
    print_r($searchTheseArray);
    $newSearchTheseArray array_filter($searchTheseArray'small_word');

    function 
    small_word($word)
    {
        return 
    strlen($word) > 4;

    Compare bible texts (and other tools):
    TheWheelofGod

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That looks correct for you're filter. You provide it with an array and pretty much say 'if the strlen of the item is > 4, keep it, otherwise dump it'.
    Are you having troubles with this block of code not working properly?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Ok well. It seems that there was one phrase missing:
    PHP Code:
    print_r($newSearchTheseArray); 
    At the end:
    PHP Code:
    $searchThese "the who and but Moses flock Jethro father priest Midian flock backside desert mountain Horeb angel appeared flame midst looked behold burned consumed Moses aside great sight burnt";
    $searchTheseArray explode(' '$searchThese);
    print_r($searchTheseArray);
    $newSearchTheseArray array_filter($searchTheseArray'small_word');

    function 
    small_word($word)
    {
        return 
    strlen($word) > 4;
    }
    print_r($newSearchTheseArray); 
    But I want it renumbered so that I can pass it through a For-loop.
    This is what I get so far:
    Array ( [4] => Moses [5] => flock [6] => Jethro [7] => father [8] => priest [9] => Midian [10] => flock [11] => backside [12] => desert [13] => mountain [14] => Horeb [15] => angel [16] => appeared [17] => flame [18] => midst [19] => looked [20] => behold [21] => burned [22] => consumed [23] => Moses [24] => aside [25] => great [26] => sight [27] => burnt )
    Compare bible texts (and other tools):
    TheWheelofGod

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh, I see what you mean. You're indexes are keeping their positions. Gotcha. The easiest solution would be to remove the for loop and run with a foreach loop - you don't need to use the indexes anyway so a foreach would provide what you needed. Another solution would be to use array_walk to recreate you're keys - too bothersome IMO. I'm pretty sure that PHP has a built in function like array_[map/walk/filter] that resets the indexes, but off hand I'm not certain what it is :|
    Use a foreach, much easier.

    Edit:
    Thats right. And to add more stuff to the end (like the + and > 3), use some simple logic. Use something like:
    PHP Code:
    $i 0;
    foreach (
    $arr AS $val)
    {
        if (
    $i++ > 0)
        {
            
    $str .= ' +';
        }
    }
    $str trim($str' +');
    $str .= ' > 3'
    for example. Foreach is the pain since it doesn't track count, but there is always a way around things :P
    Last edited by Fou-Lu; 07-30-2008 at 10:08 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •