Can I randomize the data i get from mysql?

Discussion in 'PHP' started by baris22, Oct 25, 2008.

  1. #1
    hello all,

    is it possible to randomize the data i get from the database.
    say i have got a row called title and in that row i have got tis text:

    this is a title

    When i run the query can i display this row like:

    is a this title

    or

    title a this is

    or so on...

    Thanks
     
    baris22, Oct 25, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Heh, no, there is no real Sql function to do that. You could make a php function that will though. What I would suggest doing is making a function that accepts a string, then first off use the spliti() function:

    
    function jumbleWords($theString){
    	$data = spliti(" ", $theString);
    
    PHP:
    That will then put every word in it's own array slot. Then it's a simple shuffle and send back:

    
    	$toReturn = "";
    	shuffle($data);
    	foreach ($data as $word) {
    		$toReturn .= $word . " ";
    	}
    	return $toReturn;
    }
    
    PHP:
    That should randomize things for you. If your wondering how to use this:

    
    $result = mysql_fetch_array() // Put your sql stuff here //
    echo jumbleWords($result['title']);
    
    PHP:
    Simple as that. Remember to include the function in you php file. . .
     
    ToddMicheau, Oct 25, 2008 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you very much. it worked perfect. But there is a small problem for me. I was using another function to link the words. which was:

    
    
    function stringToLinks($keywords) {
    
    	$wordList=preg_split("/\s+/",$keywords);
    	$whiteList=preg_split("/\S+/",$keywords);
    	
    	define("stl_replaceStr",".,/<>?!@#$%^&*()-=_+[]\\{}|;:'\"`~\x00..\x20");
    	
    	for ($t=0; $t<count($wordList); $t++) {
    	
    		$rTrimmedWord=rtrim($wordList[$t],stl_replaceStr);
    		$wordPostPunctuation=substr($wordList[$t],strlen($rTrimmedWord));
    		
    		$strippedWord=ltrim($rTrimmedWord,stl_replaceStr);
    		$wordPrePunctuation=substr($rTrimmedWord,0,
    			strlen($rTrimmedWord)-strlen($strippedWord)
    		);
    		
    		$result.=
    			//$wordPrePunctuation.
    			($strippedWord!=''?
    				' <a href="http://localhost/son/search.php?q='.urlencode($strippedWord).'" class="big">'.
    				htmlspecialchars($strippedWord).
    				'</a>': ''
    			).$wordPostPunctuation1. // degistirdim
    			($t<count($whiteList) ? htmlspecialchars($whiteList[$t]) : '');
    			
    	}
    	
    	return $result;
    	
    }
    
    
    PHP:
    when i use your function i can not use this function. How can i use 2 function at the same time?

    thanks
     
    baris22, Oct 25, 2008 IP
  4. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh, misunderstood you but realised it after posting, nvm...
     
    Pos1tron, Oct 25, 2008 IP
  5. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Odd, you mean:

    
    $result = mysql_fetch_array() // Put your sql stuff here //
    echo stringToLinks(jumbleWords($result['title'])); //$result['title'] is of course your keword list //
    
    PHP:
    Doesn't work? It's the same formatting I assumed, each work seperated by a space. . .

    I can update the function to make the keyword into a link like stringToLinks() does if you want.
     
    ToddMicheau, Oct 25, 2008 IP
    baris22 likes this.
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Thank you very much. I managed to get it working.

    I really appreciated.
     
    baris22, Oct 25, 2008 IP
  7. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #7
    No problem at all =]
     
    ToddMicheau, Oct 25, 2008 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    hello again!

    I need another small function. When i display the data there are some words which is less than 3 characters.

    I need a function like this

    
    
    $short = array('of', 'all', 'in', 'at');
    $title = str_replace($short, "", $title);
    
    
    PHP:
    but this should replace them only if they are a word by themselves. they should not get replaced if they are in with another word.

    this is the function you made.
    
    
    function jumbleWords($theString){    
    $data = spliti(" ", $theString);
    
    $toReturn = "";    
    shuffle($data);    
    foreach ($data as $word) {        
    $toReturn .= $word . " ";    
    }
    return $toReturn;
    
    
    }
    
    
    PHP:
    thanks
     
    baris22, Nov 1, 2008 IP