Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts

    2 random outputs as a result of another Random Output (PHP and MySQL)

    Hi,
    I am making a new game and I need to somehow select a random "Illness" from a mysql table, then with that Illness id select 2 Secondary Symptoms from a range..

    So if my code selects illness id 1

    I may want to select 2 secondary symptoms from a new table of id 11,12,13 or 14..

    I have this:

    PHP Code:
    $random_row mysql_query("SELECT * FROM initial_problem ORDER BY rand() LIMIT 1");

    while(
    $row mysql_fetch_array($random_row)){
        
        
    $cond_id $row['id'];
        
    $condition $row['condition'];
        } 
    $cond_id gives the random illness id.. so now I need to somehow select 2 id's from a range of four when -

    PHP Code:
    if ($cond_id == 1) { 
    Any ideas?

    Thanks, Steven

  • #2
    Regular Coder
    Join Date
    Aug 2011
    Posts
    120
    Thanks
    1
    Thanked 15 Times in 15 Posts
    I am not completely understanding the issue here. You have one random row from your fist table and you want two more from another table, which shouldn't be a problem. I assume you have a field in your row that contains valid symptom ids from your symptom table and I will assume that you have those in csv format (comma separated variable).

    So...
    PHP Code:
    $symptomID explode ',' $row['symptoms'] );

    $selectedSymptom array_rand $symptomID )

    $randomized_symtoms mysql_query(
    "SELECT * FROM symptoms WHERE
    id='$symptomID[$selectedSymptom[0]]' OR
    id='$symptomID[$selectedSymptom[1]]'"
    ); 
    Obviously, I have not tested this, but it should do the trick.

    Of course it may be easier to provide each illness with a corresponding 'symptom class' and a 'symptom count' so you can...
    PHP Code:
    "SELECT * FROM symptoms WHERE sClass='$indicatedClass' ORDER BY rand() LIMIT '$indicatedLimit'" 

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    hmm.. I have the second table named "secondary_conditions"

    Now the rows are laid out id and sec_condition

    e.g. id = 1 and sec_condition = broken leg
    id = 2 and sec_condition = broken arm

    but clearly only some of these are suitable for the primary condition...

    so if I select id = 1 as the initial condition

    I would like to select from sec_condition where the id is 2 or 4 or 6 or 8 for example.. then only select 2 of these..

    This makes the game more random.. so if Primary Condition = Heart Attack, Sec_Conditions would be id 1 or 3 or 5 or 7 but I only want 2 of these at one time?

  • #4
    Regular Coder
    Join Date
    Aug 2011
    Posts
    120
    Thanks
    1
    Thanked 15 Times in 15 Posts
    The approach that I typed can do that with some alteration. Specifically, build the query text manually.

    some pseudo code ...

    PHP Code:
    $symptomID explode ',' $row['symptoms'] );

    $query "SELECT * FROM symptoms WHERE";
    for (  
    $i 4i Count ($symptomID); $i++)
     {
     if ( 
    $i == )
      {
      
    $query += " id=" .. $symptomID[$i];
      }
      else
      {
      
    $query += " OR id=" .. $symptomID[$i];
      }
     }
    $query += ORDER BY rand() LIMIT " .. $symptomCount; //or wherever you put the count

    $result = mysql_query( $query ); 
    Basically, your question stated in a more codified way answers itself:
    I have a list of IDs
    I want a random selection from those IDs
    so an query will need to look for each id individually
    I also want those to be random
    and I want them to be limited

    in other words, build an expression that covers all of your IDs in an OR manner, get results randomly, and limit them.
    Last edited by M.Jackson; 03-31-2012 at 10:24 PM. Reason: more info


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •