SA-MP Forums Archive
Problem rank - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Problem rank (/showthread.php?tid=665374)



Problem rank - KamilPolska - 01.04.2019

How to replace it with the newer BlueG MySQL ?
Code:
stock LoadRank()
{
    new query[512], id, type, ranks[256], rank[MAX_RANK][MAX_RANK_LEN];
    mysql_query("SELECT * FROM `rankname`");
    mysql_store_result();

    while(mysql_fetch_row_format(query, "|"))
    {
        sscanf(query, "p<|>dds[256]", id, type, ranks);
        sscanf(ranks, "p<,>A<s[25]>()[10]", rank);

        if(type == 1)
        {
            for(new i = 0; i < MAX_RANK; i++)
            {
                if(strlen(rank[i]) > 1) format(FracRank[id][i], 25, "%s", rank[i]);
            }
        }
    }
    mysql_free_result();
    print("Ranks were read");
}



Re: Problem rank - YenTy - 02.04.2019

There's an example how to use it - https://sampforum.blast.hk/showthread.php?tid=627520

Place where calling LoadRank function:
Code:
mysql_format(MySQL, query, sizeof(query), "SELECT * FROM rankname");
mysql_tquery(MySQL, query, "LoadRank", #, #);
public LoadRank();
Code:
forward LoadRank();
public LoadRank()
{
	new results = cache_num_rows();
	if(results > 0)	// if there is any results
	{
		for(new i = 0; i < results; i++)
		{
			cache_get_value(i, "name of variable in database", yourStringVariable, size of string); // string
			cache_get_value_int(i, "name of variable in database", yourIntVariable); // saving value to the variable
			cache_get_value_float(i, "name of variable in database", yourFloatVariable); // saving value to the variable
		}
		print("Ranks were read");
	}
	else 	// There is no results
	{

	}
	return 1;
}



Re: Problem rank - KamilPolska - 02.04.2019

Thank you!!


Re: Problem rank - KamilPolska - 02.04.2019

Why do not the ranks want to load?
Code:
CMD:loadallrank(playerid, params[])
{
    if(PlayerInfo[playerid][Wlasciciel] == 0) return 1;
    SendClientMessage(playerid, -1, "Loading ranks.");

    LoadAllRank();

    new str[32];
    for(new id = 1; id < MAX_FRAC; id++)
    {
        if(strlen(FractionNames[id]) > 1)
        {
            format(str, 32, "%s", FractionNames[id]);
            SendClientMessage(playerid, -1, str);
            for(new i = 0; i < MAX_RANG; i++)
            {
                if(strlen(FracRang[id][i]) > 1)
                {
                    SendClientMessage(playerid, -1, FracRang[id][i]);
                }
            }
        }
    }
    SendClientMessage(playerid, -1, "OK");
    return 1;
}

forward LoadAllRank();
public LoadAllRank()
{
	new query[1500];
	mysql_format(g_SQL, query, sizeof(query), "SELECT * FROM `namerank`");
	mysql_tquery(g_SQL, query, "LoadRank");
	return 1;
}

forward LoadRank();
public LoadRank()
{
	new id, type, ranks[256];

	new results = cache_num_rows();
	if(results > 0)
	{
		for(new i = 0; i < results; i++)
		{
			cache_get_value_int(i, "ID", id);
			cache_get_value_int(i, "type", type);
			cache_get_value_name(i, "ranks", ranks, MAX_RANG_LEN);
		}
		print("Ranks were read");
	}
	else
	{

	}
	return 1;
}
In the database I have this saved
ID: 1
ranks: -, -, -, -, -, -, Chief,
type: 1


Re: Problem rank - YenTy - 02.04.2019

What kind of variable is this - "ranks"? If it stores multiple ranks then you're not implementing code properly. Code above included loop refers to every rank in the database which you want to load. I assume that you don't want to load everyting into one string. You've loaded them and put in the variable like id, type and "ranks" but what are you doing next? Is this all? If so, in this case every loop owerwrites it.

I can't see your database tree or global variables if there are any. Also! Your variables in the function are local, did u noticed it?


Re: Problem rank - KamilPolska - 02.04.2019

I do not know if this is it
Code:
CMD:saveranks(playerid, params[])
{
	for(new i=0;i<MAX_FRAC;i++)
    {
        if(RANG_ApplyChanges[0][i]) EDIT_SaveRanks(0, i);
    }
    for(new i=0;i<MAX_ORG;i++)
    {
        if(RANG_ApplyChanges[1][i]) EDIT_SaveRanks(1, i);
    }
    SendClientMessage(playerid, -1, "Save ranks!");
    return 1;
}

stock EDIT_SaveRanks(typ, uid)
{
    new lStr[256], query[512];

    for(new i = 0; i < MAX_RANG; i++)
    {
        if(strlen((typ == 0) ? (FracRang[uid][i]) : (FamRang[uid][i])) < 2)
        {
            format(lStr, 256, "%s-, ", lStr);
        }
        else
        {
            format(lStr, 256, "%s%s, ", lStr, (typ == 0) ? (FracRang[uid][i]) : (FamRang[uid][i]));
        }
    }
    strdel(lStr, strlen(lStr)-2, strlen(lStr));

    format(query, 512, "SELECT `ID` FROM namerank WHERE `ID` = '%d' AND `type` = '%d'", uid, type+1);
    mysql_query(g_SQL, query);

    if(cache_num_rows() > 0)
    {
        format(query, 512, "UPDATE namerank SET ranks = '%s' WHERE `ID` = '%d' AND `type` = '%d'", lStr, uid, type+1);
    }
    else
    {
        format(query, 512, "INSERT INTO namerank (ranks, ID, type) VALUES ('%s', '%d', '%d')", lStr, uid, type+1);
    }
    mysql_query(g_SQL, query);
    RANG_ApplyChanges[type][uid] = false;
    return 1;
}
Old code
Code:
new query[512], id, type, ranks[256], rank[MAX_RANG][MAX_RANG_LEN];



Re: Problem rank - KamilPolska - 02.04.2019

No, I did not notice. "ranks" are all ranks, eg Cadet, Police Officer, Police II. "rank" is one rank, eg Cadet.


Re: Problem rank - YenTy - 02.04.2019

Edit: below code id broken. Try to use your first code except included new functions. For example this line
sscanf(ranks, "p<,>A<s[25]>()[10]", rank); You need to apply already logical moves

Code:
forward LoadRank();
public LoadRank()
{
	new id, type, ranks[256], rank[MAX_RANK][MAX_RANK_LEN];

	new results = cache_num_rows();
	if(results > 0)
	{
		for(new i = 0; i < results; i++)
		{
			cache_get_value_int(i, "ID", id);
			cache_get_value_int(i, "type", type);
			cache_get_value_name(i, "ranks", ranks, MAX_RANG_LEN);

                     if(type == 1)
                     {
                           for(new j = 0; j < MAX_RANK; j++)
                           {
                                 if(strlen(rank[j]) > 1) format(FracRank[id][j], 25, "%s", rank[j]);
                           }
                     }
		}
		print("Ranks were read");
	}
	else
	{

	}
	return 1;
}



Re: Problem rank - KamilPolska - 02.04.2019

I added and works! Thanks!!
Code:
sscanf(query, "p<|>dds[256]", id, type, ranks);
sscanf(ranks, "p<,>A<s[25]>()[10]", rank);
As to whether it will be needed?
sscanf(query, "p<|>dds[256]", id, type, ranks);

EDIT: One more problem
Ranks in the database have been recorded.
ranks: R000, R111, R222, R333, R444, R555, R666, R777, R888, R999

All ranks have not been loaded, I do not know why.
R000, R111, R222, R333 and the rest do not. Why?


Re: Problem rank - KamilPolska - 03.04.2019

The database is 0-9 ranks. Loaded ranks 0-5 why?
Code:
forward LoadRank();
public LoadRank()
{
	new query[500], id, type, ranks[256], rank[MAX_RANG][MAX_RANG_LEN];

	new results = cache_num_rows();

	if(results > 0)
	{
		for(new i = 0; i < results; i++)
		{
			cache_get_value_int(i, "ID", id);
			cache_get_value_int(i, "type", type);
			cache_get_value_name(i, "ranks", ranks, MAX_RANG_LEN);

			sscanf(query, "p<|>dds[256]", id, type, ranks);
			sscanf(ranks, "p<,>A<s[25]>()[10]", rank);

            if(type == 1)
            {
                for(new j = 0; j < MAX_RANG; j++)
                {
                    if(strlen(rank[j]) > 1) format(FracRang[id][j], 25, "%s", rank[j]);
                }
            }
		}
		print("Ranks were read");
	}
	else
	{

	}
	return 1;
}



Re: Problem rank - Logic_ - 03.04.2019

Can you please show us your MySQL table structure here?


Re: Problem rank - KamilPolska - 03.04.2019

I do not know if this is a MySQL table.
Код:
TableRank()
{
	new query[500];
	strcat(query, "CREATE TABLE IF NOT EXISTS `namerank` (");
	strcat(query, "`ID` INT(11) NOT NULL AUTO_INCREMENT,");
	strcat(query, "`type` INT(11) NOT NULL,");
	strcat(query, "`ranks` VARCHAR(256) NOT NULL,");
	strcat(query, "PRIMARY KEY (`ID`), UNIQUE KEY `ID` (`ID`))");
	mysql_tquery(g_SQL, query);
	return 1;
}
EDIT:



Re: Problem rank - KamilPolska - 04.04.2019

@REF


Re: Problem rank - KamilPolska - 04.04.2019

Thanks!! fix rank
Код:
cache_get_value_name(i, "ranks", ranks, 1000);



Re: Problem rank - Logic_ - 04.04.2019

Oh no. Don't do this. This is not what MySQL is made for!! You should take a look at Vince's tutorial about "weapon saving" and database saving. Why these? Because they use an amazing feature of MySQL and that is unique and foreign keys.


Re: Problem rank - KamilPolska - 05.04.2019

Thank you. I will read about it later