Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4535398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:24:00+00:00 2026-05-21T14:24:00+00:00

I’m trying hard to wrap my head around what I’m doing here, but having

  • 0

I’m trying hard to wrap my head around what I’m doing here, but having some difficulty… Any help or direction would be greatly appreciated.

So I have some values in a table I’ve extracted according to an array (brilliantly named $array) I’ve predefined. This is how I did it:

foreach ($array as $value) {

$query = "SELECT * FROM b_table WHERE levelname='$value'";
$result = runquery($query);
$num = mysql_numrows($result);

$i=0;
while ($i < 1) {
$evochance=@mysql_result($result,$i,"evochance");  // These values are integers that will add up to 100. So in one example, $evochance would be 60, 15 and 25 if there were 3 values for the 3 $value that were returned.
$i++;
}

Now I can’t figure out where to go from here. $evochance represent percentage chances that are linked to each $value.
Say the the favourable 60% one is selected via some function, it will then insert the $value it’s linked with into a different table.

I know it won’t help, but the most I came up with was:

if (mt_rand(1,100)<$evochance) {
$valid = "True";
} 
else {
$valid = "False";
}
echo "$value chance: $evochance ($valid)<br />\n"; // Added just to see the output.

Well this is obviously not what I’m looking for. And I can’t really have a dynamic amount of percentages with this method. Plus, this sometimes outputs a False on both and other times a True on both.

So, I’m an amateur learning the ropes and I’ve had a go at it. Any direction is welcome.
Thanks =)


**Edit 3 (cleaned up):

@cdburgess I’m sorry for my weak explanations; I’m in the process of trying to grasp this too. Hope you can make sense of it.

Example of what’s in my array: $array = array('one', 'two', 'three')

Well let’s say there are 3 values in $array like above (Though it won’t always be 3 every time this script is run). I’m grabbing all records from a table that contain those values in a specific field (called ‘levelname’). Since those values are unique to each record, it will only ever pull as many records as there are values. Now each record in that table has a field called evochance. Within that field is a single number between 1 and 100. The 3 records that I queried earlier (Within a foreach ()) will have evochance numbers that sum up to 100. The function I need decides which record I will use based on the ‘evochance’ number it contains. If it’s 99, then I want that to be used 99% of the time this script is run.

HOWEVER… I don’t want a simple weighted chance function for a single number. I want it to select which percentage = true out of n percentages (when n = the number of values in the array). This way, the percentage that returns as true will relate to the levelname so that I can select it (Or at least that’s what I’m trying to do).

Also, for clarification: The record that’s selected will contain unique information in one of its fields (This is one of the unique values from $array that I queried the table with earlier). I then want to UPDATE another table (a_table) with that value.

So you see, the only thing I can’t wrap my head around is the percentage chance selection function… It’s quite complicated to me, and I might be going about it in a really round-about way, so if there’s an alternative way, I’d surely give it a try.
To the answer I’ve received: I’m giving that a go now and seeing what I can do. Thanks for your input =)**

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:24:01+00:00Added an answer on May 21, 2026 at 2:24 pm

    I think I understand what you are asking. If I understand correctly, the “percentage chance” is how often the record should be selected. In order to determine that, you must actually track when a record is selected by incrementing a value each time the record is used. There is nothing “random” about what you are doing, so a mt_rand() or rand() function is not in order.

    Here is what I suggest, if I understood correctly. I will simplify the code for readability:

    <?php
    
    $value = 'one';   // this can be turned into an array and looped through
    $query1 = "SELECT sum(times_chosen) FROM `b_table` WHERE `levelname` = '$value'";
    $count =  /* result from $query1 */
    $count = $count + 1; // increment the value for this selection
    
    // get the list of items by order of percentage chance highest to lowest
    $query2 = "SELECT id, percentage_chance, times_chosen, name FROM `b_table` WHERE `levelname` = '$value' ORDER BY percentage_chance DESC";
    $records = /* results from query 2 */
    // percentage_chance INT (.01 to 1) 10% to 100%
    foreach($records as $row) {
        if(ceil($row['percentage_chance'] * $count) > $row['times_chosen']) {
            // chose this record to use and increment times_chosen
            $selected_record = $row['name'];
            $update_query = "UPDATE `b_table` SET times_chosen = times_chosen + 1 WHERE id = $row['id']";
            /* run query against DB */
            // exit foreach (stop processing records because the selection is made)
            break 1;
        }
    }
    
    // check here to make sure a record was selected, if not, then take record 1 and use it but
    // don't forget to increment times_chosen
    
    ?>
    

    This should explain itself, but in a nutshell, you are telling the routine to order the database records by the percentage chance highest chance first. Then, if percentage chance is greater than total, skip it and go to the next record.

    UPDATE: So, given this set of records:

    $records = array(
        1 => array(
            'id' => 1001, 'percentage_chance' => .67, 'name' => 'Function A', 'times_chosen' => 0,
        ),
        2 => array(
            'id' => 1002, 'percentage_chance' => .21, 'name' => 'Function A', 'times_chosen' => 0,
        ),
        3 => array(
            'id' => 1003, 'percentage_chance' => .12, 'name' => 'Function A', 'times_chosen' => 0,
        )
    );
    

    Record 1 will be chosen 67% of the time, record 2 21% of the time, and record 3 12% of the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.