How to check if entry contains word and print random entry from table PHP, MySQL

如何检查条目包含文字和打印表随机进入PHP,MySQL

Tags: php mysql
标签: php mysql

问题 (Question)

Based on a user's profile, I'm looking to see if a column from their profile contains certain terms, and if they do, select one random entry from a specific table.

Also, in the end, I want to only print one of the entries. So if their profile contains all 5 words, I want it the PHP to still only print one random entry each time the page is refreshed.

Could someone please point me in the right direction? The code I have at the minute is as follows;

<?php

   session_start();
   include 'includes/Connect.php';
   $username=$_SESSION['myusername'];

   $query = "SELECT `CC` FROM  `$username`";              
   $result = mysql_query($query);
   $pos1 = strpos($result,'Honesty');
   $pos2 = strpos($result,'Loyalty');
   $pos3 = strpos($result,'Trust');
   $pos4 = strpos($result,'Empathy');
   $pos5 = strpos($result,'Respect');

   while($Aff = mysql_fetch_array($result)){
   $data = array();

   if($pos1!==false){
       $query1 = "SELECT Affirmation FROM `Aff CC1` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query1); $x++) 
       {
       $data[] = mysql_fetch_assoc($query1);
       }
   }
   if($pos2!==false){
       $query2 = "SELECT Affirmation FROM `Aff CC2` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query2); $x++) 
       {
       $data[] = mysql_fetch_assoc($query2);
       }
   }
   if($pos3!==false){
       $query3 = "SELECT Affirmation FROM `Aff CC3` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query3); $x++) 
       {
       $data[] = mysql_fetch_assoc($query3);
       }
   }
   if($pos4!==false){
       $query4 = "SELECT Affirmation FROM `Aff CC4` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query4); $x++) 
       {
       $data[] = mysql_fetch_assoc($query4);
       }
   }
   if($pos5!==false){
       $query5 = "SELECT Affirmation FROM `Aff CC5` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query5); $x++) 
       {
       $data[] = mysql_fetch_assoc($query5);
       }
   }   
  }
 ?> 

So I've done this instead and I'm still getting nothing.

session_start();
                                include 'includes/Connect.php';
                                $username=$_SESSION['myusername'];

                                $query = "SELECT `CC` FROM `Profile` where `Username`=`$username`";              
                                $result = mysql_query($query);
                                $pos1 = strpos($result,'Honesty');
                                $pos2 = strpos($result,'Loyalty');
                                $pos3 = strpos($result,'Trust');
                                $pos4 = strpos($result,'Empathy');
                                $pos5 = strpos($result,'Respect');
                                $data = array();

                                for ($x = 0; $x < mysql_num_rows($result); $x++) {

                                    if($pos1!==false){
                                        $CCID=1;
                                    }

                                    if($pos2!==false){
                                        $CCID=2;
                                    }

                                    if($pos3!==false){
                                        $CCID=3;
                                    }

                                    if($pos4!==false){
                                        $CCID=4;
                                    }

                                    if($pos5!==false){
                                        $CCID=5;
                                    }

                                    $querymain = "SELECT `Affirmation` FROM `Affs` WHERE CCID=$CCID ORDER BY RAND() LIMIT 1";
                                    $resultmain = mysql_query($querymain);

                                    for ($x = 0; $x < mysql_num_rows($resultmain); $x++) 
                                    {
                                    $data[] = mysql_fetch_assoc($resultmain);
                                    }
                                }

                                echo json_encode($data);
                                mysql_close($conn);

                                ?>

基于用户的配置文件,我想看看他们的轮廓的一列包含一定的条件,如果他们这样做,选择从一个特定的表的一个随机输入。

同时,在最后,我只想打印一个条目。如果他们的配置文件包含所有5个的话,我想它PHP还只能打印一个随机输入每个页面刷新时间。

可能有人请告诉我正确的方向吗?我在一分钟的代码如下;

<?php

   session_start();
   include 'includes/Connect.php';
   $username=$_SESSION['myusername'];

   $query = "SELECT `CC` FROM  `$username`";              
   $result = mysql_query($query);
   $pos1 = strpos($result,'Honesty');
   $pos2 = strpos($result,'Loyalty');
   $pos3 = strpos($result,'Trust');
   $pos4 = strpos($result,'Empathy');
   $pos5 = strpos($result,'Respect');

   while($Aff = mysql_fetch_array($result)){
   $data = array();

   if($pos1!==false){
       $query1 = "SELECT Affirmation FROM `Aff CC1` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query1); $x++) 
       {
       $data[] = mysql_fetch_assoc($query1);
       }
   }
   if($pos2!==false){
       $query2 = "SELECT Affirmation FROM `Aff CC2` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query2); $x++) 
       {
       $data[] = mysql_fetch_assoc($query2);
       }
   }
   if($pos3!==false){
       $query3 = "SELECT Affirmation FROM `Aff CC3` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query3); $x++) 
       {
       $data[] = mysql_fetch_assoc($query3);
       }
   }
   if($pos4!==false){
       $query4 = "SELECT Affirmation FROM `Aff CC4` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query4); $x++) 
       {
       $data[] = mysql_fetch_assoc($query4);
       }
   }
   if($pos5!==false){
       $query5 = "SELECT Affirmation FROM `Aff CC5` ORDER BY RAND() LIMIT 1";
       for ($x = 0; $x < mysql_num_rows($query5); $x++) 
       {
       $data[] = mysql_fetch_assoc($query5);
       }
   }   
  }
 ?> 

所以我这样做,相反,我还什么都没得到。

session_start();
                                include 'includes/Connect.php';
                                $username=$_SESSION['myusername'];

                                $query = "SELECT `CC` FROM `Profile` where `Username`=`$username`";              
                                $result = mysql_query($query);
                                $pos1 = strpos($result,'Honesty');
                                $pos2 = strpos($result,'Loyalty');
                                $pos3 = strpos($result,'Trust');
                                $pos4 = strpos($result,'Empathy');
                                $pos5 = strpos($result,'Respect');
                                $data = array();

                                for ($x = 0; $x < mysql_num_rows($result); $x++) {

                                    if($pos1!==false){
                                        $CCID=1;
                                    }

                                    if($pos2!==false){
                                        $CCID=2;
                                    }

                                    if($pos3!==false){
                                        $CCID=3;
                                    }

                                    if($pos4!==false){
                                        $CCID=4;
                                    }

                                    if($pos5!==false){
                                        $CCID=5;
                                    }

                                    $querymain = "SELECT `Affirmation` FROM `Affs` WHERE CCID=$CCID ORDER BY RAND() LIMIT 1";
                                    $resultmain = mysql_query($querymain);

                                    for ($x = 0; $x < mysql_num_rows($resultmain); $x++) 
                                    {
                                    $data[] = mysql_fetch_assoc($resultmain);
                                    }
                                }

                                echo json_encode($data);
                                mysql_close($conn);

                                ?>

最佳答案 (Best Answer)

Okay, so you've got an array with up to X amount of items. You can just pick a random item out of the array, similar to how you're picking a random item from the db.

Basically, just do a count on your $data array to see how many items you have in there. Then pick a random number between 0 and however many items there are, minus 1. You can now access that specific item from within the array.

Try something like this after you have your $data array built:

<?php

$line_count = count($data) - 1;

print "RANDOM LINE: ".$data[get_random_line($line_count)];


function get_random_line($line_count) {
    mt_srand(microtime() * 1000000);
    $random_number = rand(0, $line_count);
    return $random_number;
}

好的,所以你有一个阵列上的物品X。你可以只选择一个随机项的数组,类似于你如何从数据库中随机选择一个项目。

基本上,只是做一个指望你$data阵列看你有多少项目。然后选择一个0,但许多项目有之间的随机数,减去1。现在你可以从阵列中访问该特定项目。

试着这样的事情后,你有你的$data阵列建立:

<?php

$line_count = count($data) - 1;

print "RANDOM LINE: ".$data[get_random_line($line_count)];


function get_random_line($line_count) {
    mt_srand(microtime() * 1000000);
    $random_number = rand(0, $line_count);
    return $random_number;
}

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22080869