View Single Post
Old 12/09/2019, 10:17 AM   #4
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,177
Reputation: 73
Default Re: Question about Arrays/MySQL

No, you don't need 3 dimensional array, also, there is a wrong placement in your code.
I suggest to use something like that

PHP Code:
enum VEHICLE_DATA
{
    
vModel,
    
vOwner[MAX_PLAYER_NAME+1],
    
vOwnable,
    
vType,
    
vFaction,
    
Float:vPos[4],
    
vColor1,
    
vColor2 //and so on...
};
new 
VehicleData[MAX_VEHICLES][VEHICLE_DATA];

public 
OnPlayerEnterVehicle(playeridvehicleidispassenger//example
{
    if(
VehicleData[vehicleid][vOwnable]) //if 1 = ownable, if 0 = not ownable.
    
{
         new 
str[128];
         
format(str,sizeof(str),"Vehicle owner is %s",VehicleData[vehicleid][vOwner]);
         
SendClientMessage(playerid,-1,str);
    }
    return 
1;

in a gamemodeinit (or vehicle load func) make only not-ownable vehicles loadable.
PHP Code:
public OnGameModeInit()
{
    
mysql_tquery(handle,"SELECT * FROM `vehicles` WHERE `Ownable` != 1","LoadVehicles"); //example for last mysql plugin
    
return 1;
}

forward LoadVehicles();
public 
LoadVehicles()
{
    new 
rowstotal 0;
    
rows cache_num_rows();
    if(
rows 0)
    {
        for(new 
irowsi++)
        {
            
cache_get_value_name_int(i"Model"VehicleData[i][vModel]);
            
cache_get_value_name_int(i"Color1"VehicleData[i][vColor1]);
            
cache_get_value_name_int(i"Color2"VehicleData[i][vColor2]);
            
cache_get_value_name_int(i"Type"VehicleData[i][vType]); //by type, you can access various things
            
cache_get_value_name_int(i"Faction"VehicleData[i][vFaction]); //if a vehicle is owned by a faction, faction ID can be placed here.
            
cache_get_value_name_int(i"PosX"VehicleData[i][vPos[0]]);
            
cache_get_value_name_int(i"PosY"VehicleData[i][vPos[1]]);
            
cache_get_value_name_int(i"PosZ"VehicleData[i][vPos[2]]);
            
cache_get_value_name_int(i"PosA"VehicleData[i][vPos[3]]);
   
            
CreateVehicle(VehicleData[i][vModel],VehicleData[i][vPos[0]],VehicleData[i][vPos[1]],VehicleData[i][vPos[2]],VehicleData[i][vPos[3]],VehicleData[i][vColor1],VehicleData[i][vColor2],900);
        
            
total++;
        }
        
printf("Loaded %i vehicles from %i",total,rows);
    }
    return 
1;

so, for a player, make a dialog to choose which vehicle he wants to be loaded/spawned or you can make it with mSelection (list of a cars)

so, list of the player's cars can be obtained like this
PHP Code:
new name[MAX_PLAYER_NAME+1], query[128];
GetPlayerName(playerid,name,sizeof(name));
mysql_format(handlerquerysizeof(query), "SELECT * FROM `vehicles` WHERE `Owner` = '%e'",name);
new 
Cache:result mysql_query(handle,query,true);
new 
rows cache_num_rows();
if(
rows 0)
{
    new 
str[256];
    
str "ID\tModel"
    
for(new irowsi++)
    {
        new 
temp[2];
        
cache_get_value_name_int(i,"ID",temp[0]);
        
cache_get_value_name_int(i,"Model",temp[1]);
        
format(str,sizeof(str),"%s\n%i\t%i",str,temp[0],temp[1]);//you can also display a Model by its name, if you have a custom function for that.
    
}
    
ShowPlayerDialog(playerid,1234DIALOG_STYLE_TABLIST_HEADERS"Your Vehicles"str"Select" "Close");
}
else 
SendClientMessage(playerid,-1,"You don't own a vehicle");
cache_delete(result); 
loading can be similar, like first one, but now query must be like that
PHP Code:
new query[256],name[MAX_PLAYER_NAME+1];
GetPlayerName(playerid,name,sizeof(name));
mysql_format(handle,query,sizeof(query),"SELECT * FROM `vehicles` WHERE `Ownable` = '1' AND `Owner` = '%s'",name); 
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote