MySQL R40 - Retrieving Multiple Rows and Splitting Data
#1

I want to retrieve multiple rows from my table and then split the data into a list for a character menu. I just can't get my head around how to do it in R40. Any Ideas? Cheers.
Reply
#2

You can retrieve many rows by using a loop.

pawn Код:
for (new i, j = cache_num_rows(); i != j; i++)
{
    // cache functions to retrieve the data.
    // "i" will be the rowid.
}
If you want help with the list too, post an example of how the list would look like.
Reply
#3

Well, basically. What I am trying to do is have a player register enter their password and then choose a character or create one but the thing I am unsure about is how to split the data into a list so they can choose which char and then load the data for that char.
Reply
#4

So you have the main users table (password and all the important data) and another table for the created characters (and their statistics)? At least, that's how I'd do it.

Then a query that selects the name of each character for that player, a loop through all rows to retrieve the name and re-format it with the rest. When the loop is done, concatenate a text to allow the player create a new character.

When the player responses to an item from the dialog, check if inputtext is equal to the last sentence (to create a new character) to continue with the new name, age, etc otherwise execute a query that selects all the data where character name is the inputtext.
Reply
#5

How would I go about storing the results and then splitting them? I don't quite understand what you are saying. Can you show me an example of how it'd work. I'm not asking for a character system just an example of retrieving looped data and then splitting it into a list.
Reply
#6

When a player registers the main account and gets the unique ID (from auto increment) and then creates the first character, use the ID instead of the name in "characters" table.

pawn Код:
// OnPlayerConnect:
mysql_format(..., "SELECT character_name FROM characters c JOIN players p ON p.id=c.id WHERE username='%e'", ...);
mysql_tquery(..., "OnPlayerCharactersLoad", "i", playerid);

// OnPlayerCharactersLoad:
new string[150], char_name[MAX_PLAYER_NAME]; // increase "string" if necessary
for (new i, j = cache_num_rows(); i != j; i++)
{
    // retrieve character name - fieldid is 0 as we selected only one field
    cache_get_value(i, 0, char_name);

    // "join" it to the string
    strcat(string, char_name);
    strcat(string, "\n");
    // next line added
}

// the last option (item) of the dialog will be to create a new character
strcat(string, "Create Character");

// OnDialogResponse:
if (!strcmp(inputtext, "Create Character"))
{
    // creating new character..
}
else
{
    // load all the data from the character selected
    mysql_format(..., "SELECT * FROM characters WHERE character_name='%e' LIMIT 1", inputtext);
    mysql_tquery(...);
    // in the specified callback, retrieve all the data for the selected character
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)