SA-MP Forums Archive
Vehicle wont show correctly - 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: Vehicle wont show correctly (/showthread.php?tid=677045)



Vehicle wont show correctly - MarianImmortalGod - 28.06.2020

Hello.

I want to do a /v cmd that show's your vehicles that you own.

Code:
CMD:v(playerid)
{
	new string[128], count;
	mysql_format(handle, string, sizeof(string), "SELECT * FROM `cars` WHERE `Owner` = '%s' ORDER BY `ID` DESC", GetName(playerid));
	new Cache:result = mysql_query(handle, string);

	format(string, sizeof(string), "ID\tModel\n");
	for(new i = 1; i <= cache_num_rows(); i++)
	{
		format(string, sizeof string, "%s%d\t%s\n", string, i, CarInfo[i][CarOwner]);
		count++;
	}
	if(count == 0) return SCM(playerid, -1, "You don't own any cars.");
	
	cache_delete(result);
	ShowPlayerDialog(playerid, DIALOG_CARS, DIALOG_STYLE_TABLIST_HEADERS, "Cars:", string, "Select", "Quit");
	return 1;
}
It shows the correct number like 2 vehicles but the problem is that i get someone else vehicle.
If in the database the car number 1 is mine then car number 2 is someone else car and my seccond car is the car with id 3, i will get first two cars 1 and 2 not 1 and 3.

Any ideas ?

EDIT: solved, i added.

Code:
cache_get_value_int(i, "ID", carid);
		format(string, sizeof(string), "%s%d\t%s\n", string, i, CarInfo[carid][CarOwner]);
But now i have the problem that if i sellect car 2 it give's me someone else car from db, is like i am selecting car database id not the menu car id.


Re: Vehicle wont show correctly - SkyFlare - 28.06.2020

Quote:
Originally Posted by MarianImmortalGod
View Post
PHP Code:
mysql_format(handlestringsizeof(string), "SELECT * FROM `cars` WHERE `Owner` = '%s' ORDER BY `ID` DESC"GetName(playerid)); 
First of all, you should be doing this MySQL check when a player spawns, storing the content in an Enumerator.
that way players are not directly looking in the MySQL Database, no chance for abuse and it's faster.


Re: Vehicle wont show correctly - Kwarde - 28.06.2020

Quote:

But now i have the problem that if i sellect car 2 it give's me someone else car from db, is like i am selecting car database id not the menu car id.

What's the code under OnDialogResponse()?

Also:
Code:
for(new i = 1; i <= cache_num_rows(); i++)
I suggest you to use:
Code:
for (new i, rows = cache_num_rows(); i < rows; i++)
//Or, even better yet:
new rows;
cache_get_row_count(rows);
for (new i; i < rows; i++)
(Or, i = 1 and i <= rows. However, 0 is always the first row so this piece of code skips the first result).
When you use cache_num_rows() in the statement part of the for() loop, it gets called every loop. When you assign it the way I did, cache_num_rows() is called once.
Also: https://forum.sa-mp.com/showpost.php...4&postcount=12

Also also, I highly recommend you to use threaded queries.
If your MySQL server would be lagging somehow, your entire server will freeze for everyone after using /v untill the results come in and are handled.


Re: Vehicle wont show correctly - MarianImmortalGod - 28.06.2020

Quote:
Originally Posted by Kwarde
View Post
What's the code under OnDialogResponse()?

Also:
Code:
for(new i = 1; i <= cache_num_rows(); i++)
I suggest you to use:
Code:
for (new i, rows = cache_num_rows(); i < rows; i++)
//Or, even better yet:
new rows;
cache_get_row_count(rows);
for (new i; i < rows; i++)
(Or, i = 1 and i <= rows. However, 0 is always the first row so this piece of code skips the first result).
When you use cache_num_rows() in the statement part of the for() loop, it gets called every loop. When you assign it the way I did, cache_num_rows() is called once.
Also: https://forum.sa-mp.com/showpost.php...4&postcount=12

Also also, I highly recommend you to use threaded queries.
If your MySQL server would be lagging somehow, your entire server will freeze for everyone after using /v untill the results come in and are handled.
Oh thanks man, that is better.

Code:
case DIALOG_CARS:
    	{
    		new string[64], carid = listitem;

    		format(string, sizeof(string), "Selected: %d - PosX: %f", carid, CarInfo[carid][CarPosX]);
    		SCM(playerid, -1, string);
    	}
This code i use to check theyr coords if it matches my cars and not someone else.


Re: Vehicle wont show correctly - Kwarde - 28.06.2020

I thought so. listitem is not the same as the carid.
Let's say you have three results:

* Infernus (carid 5)[listitem = 0]
* Turismo (carid 9)[listitem = 1]
* NRG (carid 12)[listitem = 2]

I think that says enough . You would either have to create some PVars to store the carid to the right listitem, or run the SQL code again to run the same code as in /v. If no data was altered, the listitem would be the same as the row number.


Re: Vehicle wont show correctly - MarianImmortalGod - 28.06.2020

Quote:
Originally Posted by Kwarde
View Post
I thought so. listitem is not the same as the carid.
Let's say you have three results:

* Infernus (carid 5)[listitem = 0]
* Turismo (carid 9)[listitem = 1]
* NRG (carid 12)[listitem = 2]

I think that says enough . You would either have to create some PVars to store the carid to the right listitem, or run the SQL code again to run the same code as in /v. If no data was altered, the listitem would be the same as the row number.
Thanks verry much my friend, you are the best.
Everything works fine now.

Solved:

I added in my cmd.

Code:
new count = 0;
Selected[playerid][count] = carid;
count++;
and in the dialog i used

Code:
Selected[playerid][listitem]
Thanks @Kwarde