Gaining data from database
#1

Hello,

I need to know how to put the data you get out of the database(MySQL), in a ShowPlayerDialog list style. Like I'm trying to create a banlist command, it needs to get the people who are banned out of the database and store it into a dialog style list. And when I click on it twice(aka click on ok) it'll show stuff to like unban, change days, for the player I clicked on.

No need to post whole code, just an example code is enough.

- Kevin
Reply
#2

MySql database? what plugin ?
Reply
#3

Using BlueG.
Reply
#4

I know how to do it now, I've started it, it needs to be done with for loops. But I'm really stuck at this point right now.

This is what I got:
pawn Код:
enum BanList {
    BanID,
    Name,
}
new BanListDialog[][BanList];

COMMAND:banlist(playerid, params[])
{
    if(PlayerInfo[playerid][AdminLevel] >= 2) {
        new banQuery[500];
        format(banQuery,sizeof(banQuery),"SELECT * FROM bans");
        mysql_query(banQuery);
        mysql_store_result();
       


    }  
    else {
    return 0;
    }
    return 1;
}
Reply
#5

ok so, i use r7 and:

Код:
COMMAND:banlist (playerid, params[]) {
	/**
	 * Your checks
	 * [...]
	 * if is admin, and etc..
	 */
	 
	 mysql_function_query (myconn, "SELECT * FROM bans", true, "OnGamemodeHaveBans", "i", playerid);

	/**
	 * Command ended
	 * now we wait the mysql response
	 */
}

forward OnGamemodeHaveBans(playerid);
public OnGamemodeHaveBans(playerid)
{
	/**
	 * Player is still connected?
	 */
	 
	if (!IsPlayerConnected (playerid)) return 1;
	
	/**
	 * He is here
	 * Go to get data
	 * we enabled cache data so use cache functions
	 */
	 
	new rows, fields;
	cache_get_data(rows, fields);
	
	if (!rows) {
	    /**
		 * No players are banned
		 * Do something
		 */
	}
	else
	{
	    /**
		 * Someone is banned
		 * So create the dialog
		 */
		 
		 new tmp [24];//24=>for the name
		 new final_string [600]; // mb u need more, mb u need less DO U
		 for (new i = 0; i < rows; i ++)
		 {
		 	cache_get_field_content (i, "name", tmp); // for the size: 24 chars for the nick
			strcat (final_string, tmp);
			strcat (final_string, " - ");
            cache_get_field_content (i, "time", tmp);
            // ok, the time is stored as integer
            // u need to format it
            // so do u it
            
			/**
			 * Do more
			 * What you want to take from database?
			 * Just use cache_get_field_content (i, TABLE_NAME, tmp);
			 * And you have the value in tmp
			 * Remember:
			 * If this is a string just use tmp
			 * If is a integer use strval
			 * If is a float floatstr
			 */
			 
			 /**
			  * [...]
			  */
			  
			/**
			 * Ok now we need to add a \n to the string (at the and or when you do the last read change
			 * strcat (final_string, " - ");
			 * to
			 * strcat (final_string, "\n");
			 */
		 }
		 
		 /**
          * Now we have everything in final_string
          * Just create a dialog with final_string as text
          */
	}
}
Maybe you want something like this?
Reply
#6

I've managed to get the code working after your tips, thanks. But how in the hell am I going to put this in ondialogresponse so it'll change THAT person. and not someone else.

pawn Код:
COMMAND:banlist(playerid, params[])
{
    if(PlayerInfo[playerid][AdminLevel] >= 2) {
        new banQuery[500], finalString[600], rows;
        format(banQuery,sizeof(banQuery),"SELECT * FROM bans WHERE HasExpired ='0'");
        mysql_query(banQuery);
        mysql_store_result();
        rows = mysql_num_rows();
        if(!rows)
        {
            ShowPlayerDialog(playerid, DIALOG_NO_BANS, DIALOG_STYLE_MSGBOX,"{ff6600}No bans","Unfortunatly there weren't any bans in the database","Ok","Cancel");
        }
        else {
            new tmp[MAX_PLAYER_NAME];
            for (new i = 0; i < rows; ++i)
            {
                while(mysql_fetch_row_format(banQuery, "|"))
                {
                    mysql_fetch_field_row(tmp, "BannedPlayer");
                    strcat(finalString,tmp);
                    strcat(finalString,"\n");
                }
            }
            strcat(finalString, tmp);
            ShowPlayerDialog(playerid,DIALOG_BANLIST,DIALOG_STYLE_LIST, "{ff6600}Banlist", finalString,"Change","Cancel");
        }
        mysql_free_result();

    }  
    else {
    return 0;
    }
    return 1;
}
Reply
#7

top:
Код:
new player_banlist_ids[MAX_PLAYERS][MAX_ROWS][24];
Код:
            for (new i = 0; i < rows; ++i)
            {
                while(mysql_fetch_row_format(banQuery, "|"))
                {
                    mysql_fetch_field_row(tmp, "BannedPlayer");
                    strcat(finalString,tmp);
                    strcat(finalString,"\n");
                    player_banlist_ids[playerid][i] = tmp;
                }
            }
Now try player_banlist_ids[playerid][listitem] in ondialogresponse to get player name and when you do a query

[...] WHERE name = '%s'", player_banlist_ids[playerid][listitem]);

If you have an "id" of the ban you can use it and not the player name (so we just use new player_banlist_ids[MAX_PLAYERS][MAX_ROWS]; but no problem) this code should work..

MAX_ROWS is the max of player a dialog can handle
Reply
#8

That didn't get me anywhere yet I'm sorry. Anyone else?

Idea:

When a player types / banlist it shows a dialog with the bans out the databases (DONE)
Switch through the listitems on ondialogresponse for that player so case selectedBan[playerid]; or something.
Anyone?
Reply
#9

pawn Код:
OnDialogRespone

use the dialogid
Reply
#10

Dont have certained answer but it seems zh3r0 Were master at this all Cause he stored eclips account on databse.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)