Show query results on dialog
#1

Hi

I made this simple code to show player own vehicles (Reading data from MySQL database) but the thing is, it only displays the first result from the query, instead of displaying me all the vehicles that belong to the player.

For example, In my database I have 2 bikes owned by "Tester" but the command only shows up the dialog with the data of the first bike that was registered on the database with the owner described above.

Any help will be apreciated

PHP код:
CMD:mybikes(playerid,params[])
{
    if(
IsSpawned[playerid] == 0) return SendClientMessage(playerid,-1,"You must be spawned");
    new 
query[150];
    
mysql_format(mysqlquerysizeof(query),"SELECT * FROM `cars` WHERE `Owner` = '%e'"GetName(playerid));
    
mysql_tquery(mysqlquery"ShowPlayerBikes""i"playerid);
    
printf("%s",query);
    return 
1;
}
public 
ShowPlayerBikes(playerid)
{
    print(
"ShowPlayerBikes is getting called!");
    
SendClientMessage(playerid,-1,"DEBUG** ShowPlayerBikes callback is getting called properly" );
    new 
rows cache_num_rows();
    for (new 
0rowsi++)
     {
        if(
rows// check if there are rows or not
        
{
            new 
string[180];
             
CarInfo[i][cID] = cache_get_field_content_int(0"ID");
             
CarInfo[i][cModel] = cache_get_field_content_int(0"Model");
            
format(string,sizeof(string), "**Bike ID: %i - MODEL ID: %i\n",CarInfo[i][cID], CarInfo[i][cModel]);
            
ShowPlayerDialog(playeridMyBikesDIALOG_STYLE_LIST "Your bikes" string "Ok" "Close");
            
printf("Number of rows for Username %s: %i",GetName(playerid), rows);
         }
         else
        {
            
SendClientMessage(playerid,-1,"According to our database, you don't own any bike!");
        }
     }
     return 
1;

Reply
#2

PHP код:
public ShowPlayerBikes(playerid)
{
    new 
rows cache_num_rows();
    if(
rows// Checks to see if you have a bike before initiating for loop.
    
{
        new 
string[512], tmpstr[128]; // Creates string to hold 1 line of dialog and another string to store whole dialog.
        
for (new 0rowsi++) // Each time it loops it will create another line in your dialog.
        
{
            
CarInfo[i][cID] = cache_get_field_content_int(0"ID");
            
CarInfo[i][cModel] = cache_get_field_content_int(0"Model");
            
format(string,sizeof(string), "**Bike ID: %i - MODEL ID: %i\n"CarInfo[i][cID], CarInfo[i][cModel]);
            
strcat(stringtmpstrsizeof(string)); // This adds the new line to your dialog.
         
}
         
ShowPlayerDialog(playeridMyBikesDIALOG_STYLE_LIST "Your bikes" string "Ok" "Close"); // After all the lines are made, dialog will open
    
}
    else
        
SendClientMessage(playerid,-1,"According to our database, you don't own any bike!");
    return 
1;

Should work, but code untested.
Reply
#3

You only retrieve the first row by setting the rowid to 0. Why do you store to CarInfo array which is being overwritten over and over again? Either use local variables or a player-array (with a limit of how many vehicles a player can own).

Код:
cache_get_field_content_int(i, "ID"); 
CarInfo[i][cModel] = cache_get_field_content_int(i, "Model");
Reply
#4

Quote:
Originally Posted by JordanZaundd
Посмотреть сообщение
PHP код:
    format(string,sizeof(string), "**Bike ID: %i - MODEL ID: %i\n"CarInfo[i][cID], CarInfo[i][cModel]);
    
strcat(stringtmpstrsizeof(string)); // This adds the new line to your dialog. 
You're formating string and stracting the text of tmpstr into string while tmpstr is empty, you've to format tmpstr and strcat it into string:

PHP код:
    format(tmpstr,sizeof(tmpstr), "**Bike ID: %i - MODEL ID: %i\n"CarInfo[i][cID], CarInfo[i][cModel]);
    
strcat(stringtmpstrsizeof(string)); // This adds the new line to your dialog. 
other bug/mistake already mentioned by Konstantinos.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)