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

  • Home
  • SEARCH
  • 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 9133203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:26:45+00:00 2026-06-17T08:26:45+00:00

Context: I’m querying a database to return a specified number of monsters to be

  • 0

Context: I’m querying a database to return a specified number of monsters to be used in a fight within a simple web based RPG.

Problem: I have a straightforward MYSQL query that includes a WHERE clause to determine the “level” of the monsters. In a previous function I’m building an array that includes the levels of the corresponding monsters that should be returned; something like:

$levels = Array( [0] => 3 [1] => 1 [2] => 1 ) 

So in the above array, the Value corresponds to the level. So I’m looking to return 3 monsters — Level 3, Level 1 and Level 1.

Simple enough. Now I want to query MYSQL with the following and presumably use a loop to do the query 3 times to get my 3 monsters; something like:

$query = "SELECT *, CardHP as EncounterCardHP, CardAP as EncounterCardAP";
$query .= " FROM Cards";
$query .= " WHERE CardTypeId = 1 AND CardRarity = {$level}";
$query .= " ORDER BY RAND() LIMIT 1";

So in the above query, the variable {$level} is the Monster Level from the previous array. The loop formatting is where I’m struggling. I’m trying something like:

while ($i = $levels) {
     $query = "SELECT *, CardHP as EncounterCardHP, CardAP as EncounterCardAP";
     $query .= " FROM Cards";
     $query .= " WHERE CardTypeId = 1 AND CardRarity = {$i}";
     $query .= " ORDER BY RAND() LIMIT 1";
 $result_set = mysql_query($query, $connection);
 confirm_query($result_set);
 return $result_set;    
}

I think this is close, but I’m not seeing how I can get this to ultimately give me a single array of monsters from MYSQL. It seems in the above, I’m building 3 separate arrays? Do I need to use array_merge(), or is there a simpler more elegant way to do this? If I were getting all the same level monsters I could simply just run the query 1 time and LIMIT 3, but I need monsters to correspond to what the previous array shows.

Making sense? Help is always appreciated.

  • 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-06-17T08:26:46+00:00Added an answer on June 17, 2026 at 8:26 am

    It was actually much easier than I thought; maybe I over-explained the issue. Here is what I did.

    1) Once I returned my array $levels which looked like this:

    $levels = Array( [0] => 3 [1] => 1 [2] => 2 ) 
    

    2) I created an empty array called $result_set
    2) Then looped through the $levels array with a foreach loop and ran the query with the first $level in the $levels array. However, rather than just returning the $result_set with the first row returned, I inserted the first row into the empty array I created in step 2. After each loop through, I inserted the new row from MYSQL into the now filled $result_set array.

    Here is what it looked like in the end:

    $result_set = array();
    foreach ($levels as $level) {
        $query = "SELECT *, CardHP as EncounterCardHP, CardAP as EncounterCardAP";
        $query .= " FROM Cards";
        $query .= " WHERE CardTypeId = 1 AND CardRarity = {$level}";
        $query .= " ORDER BY RAND() LIMIT 1";
        $result = mysqli_query($query, $connection);
        $result_set[] = mysqli_fetch_array($result);    
        confirm_query($result_set);
    }
    return $result_set;     
    

    My $result_set now has 3 monsters in it, a level 3, level 1 and level 2, all chosen randomly from the database for each respective level.

    Thanks for all the help.

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

Sidebar

Related Questions

Context Simple Java EE 6 (JBoss AS 7.1) CRUD Web App, Using a @Named
Context (doc count) ------------------------- clojure.core/count ([coll]) Returns the number of items in the collection.
Context I want to use JavaFx with clojure. I am aware of http://nailthatbug.net/2011/06/clojure-javafx-2-0-simple-app/ Question:
Context: From my javascript web UI, I launch a long-running (several minutes) operation that
Context: IIS 6 on Windows 2003 Server ASP.NET 3.5 sp1 C# Web Application running
Context: Imagine that you have a standard CherryPy hello word app: def index(self): return
context: I have an ajax request which return data structure with an html key.
Context I am writing a simple JUnit test for the MyObject class. A MyObject
Context: single page web applications written in JavaScript and AMD modules (like require.js) What
Context I'm working on a small web app to store photos. Photos are ordered

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.