1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I needs some PHP help please!

Discussion in 'General Programming Chat' started by rannybo, Nov 15, 2010.

  1. rannybo

    rannybo Junior Member

    Joined:
    Jul 7, 2007
    Messages:
    175
    Likes Received:
    54
    Hi,

    I have a related articles script that just randomizes the results and instead of randomizing, I need the script to become static. I noticed the math function "rand" inside the code but I do I get change the code in order for the results to always stay the same. The code is below:

    PHP:
    $q $this->db->Query($sqlQuery);
            while (
    $result $this->db->Fetch($q)) {
                    
    $results[]=$result;
            }
            
    $resultsCount count($results);
            if(
    $resultsCount>15){$resultsCount 29;}
            
            for (
    $z 0$z<=$resultsCount$z++) {
                
    $key rand(0count($results));
                while(
    checkArr($keys$key)) {    
                    
    $key rand(0count($results));
                }
                
                
    $search = array(' ','?','%','#',':','!','.','@','$','^','&','*','(',')',';','=','[',']','{','}','/',',','"',"'",''');
                $replace = array('
    -','','','','','','','','','','','','','','','','','','','','','','','',''); 
                $keys[]=$key;   
                $link = str_replace($search, $replace, $results[$key]['
    Title']);
                
                            
                $resultArray[]['
    id']=$results[$key]['id']=$results[$key]['ArticleID'];
                $resultArray[count($resultArray)-1]['
    link']=$link;
                $resultArray[count($resultArray)-1]['
    title']=$results[$key]['Title'];
            }
            return $resultArray;
        }   
     
  2. Twizzler

    Twizzler Newbie

    Joined:
    Nov 4, 2010
    Messages:
    33
    Likes Received:
    7
    Occupation:
    Typing code.
    Location:
    Lost in cyberspace
    That code is a bit ugly but basically I removed the randomize calls and replaced them with the incrementor value so it will always be the same. You will now need to change that to whichever article you want.

    Please note I also had to remove the search = array line since the forum will not allow me to post url's emails etc so I think it is catching the at sign.

    PHP:
    $q $this->db->Query($sqlQuery);
            while (
    $result $this->db->Fetch($q)) {
                    
    $results[]=$result;
            }
            
    $resultsCount count($results);
            if(
    $resultsCount>15){$resultsCount 29;}
     
            for (
    $z 0$z<=$resultsCount$z++) {
                
    $key $z;
                while(
    checkArr($keys$z)) {    
                    
    $key $z;
                }
     
                
    $replace = array('-','','','','','','','','','','','','','','','','','','','','','','','',''); 
                
    $keys[]=$key;   
                
    $link str_replace($search$replace$results[$key]['Title']);
     
     
                
    $resultArray[]['id']=$results[$key]['id']=$results[$key]['ArticleID'];
                
    $resultArray[count($resultArray)-1]['link']=$link;
                
    $resultArray[count($resultArray)-1]['title']=$results[$key]['Title'];
            }
            return 
    $resultArray;
        }
     
    • Thanks Thanks x 1
    Last edited: Nov 15, 2010
  3. rannybo

    rannybo Junior Member

    Joined:
    Jul 7, 2007
    Messages:
    175
    Likes Received:
    54
    Thanks!

    I tried what you stated and the related articles came up blank. Anymore Ideas?
     
  4. Twizzler

    Twizzler Newbie

    Joined:
    Nov 4, 2010
    Messages:
    33
    Likes Received:
    7
    Occupation:
    Typing code.
    Location:
    Lost in cyberspace
    Hard to say the issue since I am just seeing a snippet of the code and I am just correcting that snippet to swap the randomization to increments.
    For example I have no idea what checkArr function does nor do i know what happens with the result array after this data is processed...
    but I did change the code a bit...
    added another
    $key = $z

    due to not knowing what checkArr does..
     
    • Thanks Thanks x 1
    Last edited: Nov 15, 2010
  5. rannybo

    rannybo Junior Member

    Joined:
    Jul 7, 2007
    Messages:
    175
    Likes Received:
    54
    Does this help?

    PHP:
    // all related aricles code is in this function
        
    function prepareQueryForRelatedArticles ($articleTitle$articleText) {
            function 
    checkArr($ar$val) {
                
    $val strtolower($val); if (!$val) return false; if (count($ar)>0
                foreach (
    $ar as $k => $v) {
                    if (
    $val==$v) {return true;}
                }
                return 
    false;
            }
            
            
    //this words are cuting from title
            
    $trash=array('things' 'thing');
            
    $preposition = array('/,/''/\./''/\!/','/\?/','/\%/','/\#/''/@/''/-/''/;/','/\(/','/\)/');
            
            
    //cutting prepositons
            
    $articleTitle=preg_replace($preposition''$articleTitle);
            
    //cutting digits
            
    $articleTitle=preg_replace('/[1-9]/'''$articleTitle);
            

            
    $titleWords explode(' ',$articleTitle);        
            
    $articleText strtolower($articleText);
            
            if (
    $articleText!='')
            {
                
    $max=0;
                foreach(
    $titleWords as $v) {
                    if (!
    $v) continue;
                    
    $v=strtolower($v);
                    
    //if (!checkArr($trash, $v)) 
                    //{
                    //$goodWords[]=$v;
                    
    if (preg_match_all('/'.$v.'./i'$articleText$matches)){
                        
    //echo "$v, ";
                        
    if (count($matches[0])>$max) {
                            
    $max count($matches[0]);
                            
    $goodWord=$v;
                        }
                        
    $goodWords[]=$v;
                    }
                    
    //}
                
    }
                foreach(
    $goodWords as $k => $v) {
                    if (
    $v==$goodWord) {$goodWords[$k]='>'.$v;}
                }
            }
            else
            {
                
    //let's just copy article title if we do not have article text
                
    $goodWords=$titleWords;
            }
            
            
    //faster
            
    $sqlQuery='SELECT  ArticleID, Title ,match(Title, Summary) against(\''.implode(' '$goodWords).'\') as score FROM '.AL_ARTICLES_TABLE.' WHERE MATCH (Title,Summary) AGAINST (\''.implode(' '$goodWords).'\' IN BOOLEAN MODE) order by score desc  LIMIT 100';
            
    //echo $sqlQuery;
            
            
    $q $this->db->Query($sqlQuery);
            while (
    $result $this->db->Fetch($q)) {
                    
    $results[]=$result;
            }
            
    $resultsCount count($results);
            if(
    $resultsCount>15){$resultsCount 29;}
            
            for (
    $z 0$z<=$resultsCount$z++) {
                
    $key rand(0count($results));
                while(
    checkArr($keys$key)) {    
                    
    $key rand(0count($results));
                }
                
                
                
                
    $search = array(' ','?','%','#',':','!','.','@','$','^','&','*','(',')',';','=','[',']','{','}','/',',','"',"'",'?');
                
    $replace = array('-','','','','','','','','','','','','','','','','','','','','','','','',''); 
                
    $keys[]=$key;   
                
    $link str_replace($search$replace$results[$key]['Title']);
                
                            
                
    $resultArray[]['id']=$results[$key]['id']=$results[$key]['ArticleID'];
                
    $resultArray[count($resultArray)-1]['link']=$link;
                
    $resultArray[count($resultArray)-1]['title']=$results[$key]['Title'];
            }
            return 
    $resultArray;
        }
     
  6. rannybo

    rannybo Junior Member

    Joined:
    Jul 7, 2007
    Messages:
    175
    Likes Received:
    54

    Thanks man that did it! Thanks.
     
  7. Twizzler

    Twizzler Newbie

    Joined:
    Nov 4, 2010
    Messages:
    33
    Likes Received:
    7
    Occupation:
    Typing code.
    Location:
    Lost in cyberspace
    No problem. glad i could help :)