Reputation: 77
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);
?>
Upvotes: 0
Views: 110
Reputation: 3200
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;
}
Upvotes: 1