Retrieving data from multiple rows and printing [Help]
#1

I'm working on a simple faction system.
I want to make a cmd which retrieves every faction and its ID.

I was thinking in something like

Код:
while(SOMETHING) {
    SendClientMessage(FACTIONS)
}
But I'm not sure about how I make it. Help please?


This is what I have so far but it's not working.

Код:
CMD:faclist(playerid, params[])
{
        if(!AdminOnDuty(playerid) && pInfo[playerid][pAdmin] >= 6) return SendClientMessage(playerid, -1, NODUTY);
		
		SendClientMessage(playerid, ORANGE_ADMIN, "Factions:");
		new ccount;
		new sql[80], row;	
		format(sql, sizeof(sql), "SELECT * FROM factions ORDER BY fID DESC");
		mysql_query(g_SQL, sql);
		cache_get_row_count(row);
		ccount = row;
		for(new x = 1; x <= ccount; x++)
	    {	   
	    	mysql_format(g_SQL, sql, sizeof(sql), "SELECT * FROM `factions` WHERE `fID` = %d", x);
		mysql_query(g_SQL, sql);
		cache_get_value_int(0, "ID", fInfo[x][fID]);		
		cache_get_value_name(0, "Name", fInfo[x][fName]); 	
	    	new string[50];
	        format(string, sizeof(string), "ID %d: %s", fInfo[x][fID], fInfo[x][fName]);
	        SendClientMessage(playerid, ORANGE_ADMIN, string);
	    }
        return 1;
}
Reply
#2

I think I have found where is my problem, but I can't find a solution.
While I'm trying to load factions I'm getting:

Код:
[02:47:46] [plugins/mysql] cache_get_value_name_int: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name: invalid row index '0' (number of rows: '0')
[02:47:46] [plugins/mysql] cache_get_value_name_int: invalid row index '0' (number of rows: '0')
Load factions:
Код:
LoadFaccs()
{
	new Query[180];
	new sql[80];	
	format(sql, sizeof(sql), "SELECT * FROM facciones ORDER BY fID DESC");
	mysql_query(g_SQL, sql);
	new rows = cache_get_row_count(rows);
	for(new x = 0, j = rows ; x < j; x++)
	{
	    mysql_format(g_SQL, Query, sizeof(Query), "SELECT * FROM `facciones` WHERE `fID` = %d", x);
		mysql_query(g_SQL, Query);
		cache_get_value_int(0, "fID", fInfo[x][fID]);		
	 	cache_get_value_name(0, "fNombre", fInfo[x][fNombre]);		
		cache_get_value_name(0, "fRango1", fInfo[x][fRango1]);
		cache_get_value_name(0, "fRango2", fInfo[x][fRango2]);
		cache_get_value_name(0, "fRango3", fInfo[x][fRango3]);
		cache_get_value_name(0, "fRango4", fInfo[x][fRango4]);
		cache_get_value_name(0, "fRango5", fInfo[x][fRango5]);
		cache_get_value_name(0, "fRango6", fInfo[x][fRango6]);
		cache_get_value_name(0, "fRango7", fInfo[x][fRango7]);	
		cache_get_value_int(0, "fTipo", fInfo[x][fTipo]);		
		
	 	if(TotalFaccs < x) TotalFaccs = x;
	 	}
	printf("Facciones cargadas: %d (MAX: %d)", TotalFaccs, MAX_FACTIONS);
}
Reply
#3

Help I don't know why I'm getting this invalid row index. I know It has something to do with deleted rows, but I need to be able to delete them.
Reply
#4

You do NOT need the second query. At all. You already have the entire result set from the first query. Remove the second query and replace 0 with x in the cache_get_value statements.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
You do NOT need the second query. At all. You already have the entire result set from the first query. Remove the second query and replace 0 with x in the cache_get_value statements.
Ok, now I'm not getting the row index 0, but I'm trying to make a faction list command. I can actually do that but I'm having troubles with deleted rows. Ex.:

Factions table:

Код:
ID | NAME
--------------
0    No // I don't need a ID 0
1    Goverment
2    LSPD
4    LSMD
My command:

Код:
CMD:factlist(playerid, params[])
{
    if(!AdminOnDuty(playerid) && pInfo[playerid][pAdmin] >= 6) return SendClientMessage(playerid, -1, NODUTY);
	SendClientMessage(playerid, ORANGE_ADMIN, "Factions:");
    for(new x=1; x<=TotalFaccs; x++)
    {
    	new string[500];
        format(string, sizeof(string), "ID %d: %s", x, fInfo[x][fName]);
        SendClientMessage(playerid, ORANGE_ADMIN, string);
    }
    return 1;
}
This will print:

Factions:
1. Goverment
2. LSPD
3. No


And I have no clue why. I know that the deleted row it's giving me some troubles, but I want a dynamic faction system so I must be able to delete a row and not screw up the whole code :P
Reply
#6

Use y_iterate. You can Iter_Add faction id, and loop it through foreach. For those deleted you can use Iter_Remove and it won't be listed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)