Welcome Guest, Not a member yet? Register   Sign In
Need some help Internal server error
#1

[eluser]megabyte[/eluser]
I'm guessing this might be a load issue?

I'm not sure.

It worked when there was a single term in the glossary table, now there are 47.

I am using a tag based system where definitions are pulled out of a database table

So if there is you echo this:

Code:
$string = "the [brown] quick [fox] jumped over the fence.";
echo $string;

It searches the string for words enclosed in [] brackets and searches the glossary table.



Code:
function parse_all_tags($input)
    {
        $CI =& get_instance();
        
        $regex = '#\[(.*?)]#';
    
        // just as a test switch the brakets
        if (is_array($input)) {
            
            $term = strtolower($input[1]); // to lower case
            
            // get the definintion using the term
            $query = $CI->db->get_where('glossary', array('LOWER(term)' => $term));
                        
            if($query->num_rows() > 0)
            {
                $row = $query->row();
                
                $input = '<a href="#" class="tool-tip">definition.'" rel="'.$row->term.'">'.$term.'</a>';
            }
            
            //$str = preg_replace('/\s+/', '', $str); // strip whitespace
            
        }
    
        return preg_replace_callback($regex, 'parse_all_tags', $input);
    }


If I comment out the return there is no server error


anyone able to help me figure out why I'm getting a server error now?
#2

[eluser]treeface[/eluser]
Not entirely sure just yet what's going wrong, but it looks like you've got a pretty malformed $input variable there. The single quote ' is overridden by the double quote ". Also it looks like you close out the opening anchor tag twice. I'm not entirely sure what you want to do with the word "definition", but here's my best guess at what you were aiming for:

<b>OK I can't figure out how to properly format this post so it shows up properly...sorry can't paste the valid string here</b>

So you can see there that what you had was spitting back syntax errors for me.

Code:
//This is a valid string:
$derp = "See, I can use apostrophes in here and it doesn't matter because double-quotes override single quotes."

//This is invalid and will give you syntax errors:
$merp = 'This one uses a single quote to encapsulate the string so it gets all confused by words like doesn't';

$nowNoticeAllTheRedHere = "That's because I didn't use quotes properly";
#3

[eluser]megabyte[/eluser]
treeface, its not as parsing error.

its the preg_replace_callback that's causing the internal server error.

I appreciate the help, sadly that's not the issue! Tongue


I thought it may be an endless loop issue so i set the limit on preg replace callback and that wasnt it either.
#4

[eluser]megabyte[/eluser]
Just when I thought all hope was lost, I remembered CI's build in auto link function.

So the solution:

Code:
function parse_all_tags($str)
    {
        $CI =& get_instance();
        
        $regex = '#\[(.*?)]#';
        
        if( preg_match_all($regex, $str, $matches) )
        {
            //print_r($matches);
            
            for ($i = 0; $i < count($matches[1]); $i++)
            {
                $CI->db->like('term', $matches[1][$i], 'both');
                $CI->db->limit(1);
                $query = $CI->db->get('glossary');
                        
                if($query->num_rows() > 0)
                {
                    $row = $query->row();
                    
                    $str = str_replace($matches[0][$i], '<a href="#" class="tool-tip">definition.'" rel="'.$row->term.'">'.$matches[1][$i].'</a>', $str);
                    
                    $query->free_result();
                    
                }
                else
                {
                    $str = str_replace($matches[0][$i], $matches[1][$i], $str);
                }
                
            }
        }
    
        return $str;
    }




Theme © iAndrew 2016 - Forum software by © MyBB