PDA

View Full Version : Sql & PVars


Lajko1
10/04/2017, 07:28 PM
Hey guys, I just can't figure it out, what I'm trying to do is to combine 2 different strings into 1 to explain it a bit, I'm just learning a bit about sql, and database and so on so what I'm trying to save is the following, I'm using dialog where I insert a name, and I'm using another dialog where I insert lastname saving them to two different PVars.
However I want to combine those 2 (name and lastname) PVars into 1 and to save it as full name.

So what I tried to do is the following:

Dialog Response for inserting the name
new charname[128];
format(charname,sizeof(charname),"%s",inputtext);
SetPVarString(playerid,"CharName",charname);

Dialog Response for inserting the lastname
new charlastname[128];
format(charlastname,sizeof(charlastname),"%s",inputtext);
SetPVarString(playerid,"CharLastName",charlastname);

"Function" and few commented attempts what I tried to do to combine those two strings:

SetCharName(playerid)
{
new setname[128];
new fname[128];
new lname[128];
//format(setname,sizeof(setname),"%s_%s",CharData[playerid][CharName],CharData[playerid][CharLastName]);
//format(setname,sizeof(setname),"%s_%s",GetPVarString(playerid, "CharName", setname, sizeof(setname)),GetPVarString(playerid, "CharLastName", setname, sizeof(setname)));
//SetPlayerName(playerid,setname);

format(setname,sizeof(setname),"%s_%s",GetPVarString(playerid, "CharName", fname, sizeof(fname)) ,GetPVarString(playerid, "CharLastName", lname, sizeof(lname)));
SetPVarString(playerid, "PlayerName", setname);
GetPVarString(playerid, "PlayerName", setname, sizeof(setname));
return 1;
}

And how I tried to save:

format(szQuery, sizeof(szQuery), "INSERT INTO `characters` (username, yearofbirth, monthofbirth, dayofbirth) VALUES ('%s', '%d', '%d','%d')", SetCharName(playerid) //code goes on...

Just can't figure it out how I can combine and save two strings into one so it will be saved as full name firstname_lastname. Thanks for answer in advance as this is eating my nerves ;)

I know the code at the end went messy but I tried a lot of stuff to make it work, so I just posted the last attempt what I tried.

AndreiWow
10/04/2017, 07:54 PM
Uh, try creating the function this way...

SetCharName(playerid, firstname, lastname)
{
put your save code in here and use firstname to save the first name and lastname to save the last name.

}

And then after you save both variables just call the function this way
SetCharName(playerid, firstnamevar, lastnamevar); :?


Like.. lets say that you use first dialog for firstname
save what the player entered in a variable

new name[2];
name[0] = "Jack";

and then show the second dialog, save what he entered in another variable, or use a 2d

name[1] = "Rickman"

And when you call the function

SetCharName(playerid, name[0], name[1]);
the code that is under that function will run, you will probably put the save code there.

So if you've made the function this way
SetCharName(playerid, firstname, lastname)

then you will use firstname to save name[0] which is jack, and lastname to save name[1] which is Rickman


and to combine them just use

format(string, sizeof(string), "%s_%s", firstname, lastname);
inside the function

Lajko1
10/04/2017, 07:58 PM
Yeah problem is cuz my save code / string combine code doesn't work ^^ so need someone to correct my mistake or to lead me in right direction somehow.

AndreiWow
10/04/2017, 08:00 PM
Yeah problem is cuz my save code / string combine code doesn't work ^^ so need someone to correct my mistake or to lead me in right direction somehow.

If you make the function as I told you, you should be able to combine them by just doing this...

format(string, sizeof(string), "%s_%s", firstname, lastname);

Lajko1
10/04/2017, 08:08 PM
new charname[128];
format(charname,sizeof(charname),"%s",inputtext);
SetPVarString(playerid,"CharName",charname);

And how to save this properly into variable? it's inputtext dialog, is my way correct or..?

AndreiWow
10/04/2017, 08:26 PM
new charname[128];
format(charname,sizeof(charname),"%s",inputtext);
SetPVarString(playerid,"CharName",charname);

And how to save this properly into variable? it's inputtext dialog, is my way correct or..?

Create the variable somewhere at the top to be global... and probably

format(charname, sizeof(charname),"%s", inputtext);

variable = charname;

didn't work too much with dialogs but this should work.

Lajko1
11/04/2017, 04:54 AM
will try to do your way, however if I use this:

new charname[128];
format(charname,sizeof(charname),"%s",inputtext);
SetPVarString(playerid,"CharName",charname);

and try with this:

CMD:test(playerid, params[])
{
new msg[28];
format(msg, sizeof(msg),"%s",GetPVarString(playerid,"CharName",msg, sizeof(msg)));
SendClientMessage(playerid,-1,msg);
return 1;
}

it says "1" and nothing else, soo I used PVars in wrong way (still learning about PVars) or something is wrong at dialogs phase where I'm trying to save to PVar.