PDA

View Full Version : Help with loading id's with gaps between them


thimo
19/07/2013, 10:15 AM
Hello, i got a problem loading my Car IDS from the database. Well its working fine but at this moment it has a dissability to be abled to sell anything. The problem is there is id 1,2,3,5 in the database but the server loads it like this 1,2,3,4 while i want to skip 4 and go to 5. How would i go around to do that? Underneath here is my code:

forward OnCarIDsLoad();
public OnCarIDsLoad()
{
new rows, fields;
cache_get_data(rows, fields, Handle);

new i = 0;
while( rows > i < 10000)
{
VehicleLoad[i] = true;
printf("Car %d created", i);
i++;
}
print("Loading finished");
return 1;
}

Here's a picture of the database
http://imageshack.us/scaled/large/809/5nd.png (http://imageshack.us/photo/my-images/809/5nd.png/) Uploaded with ImageShack.us (http://imageshack.us)

Misiur
19/07/2013, 10:40 AM
I had this problem too. Keep in mind that array indexes start from 0, and database indexes start from 1. I had additional field in enumerator called dbID, which held the database id of record. Iterators are really handy if you don't know how much records are loaded. Also remember that you shouldn't waste any memory, so if you skip some key in array, you are wasting enum size*cell bytes of memory.

thimo
19/07/2013, 10:42 AM
But how would i skip the row if it was empty? it now just loads the next one as the WRONG id

Misiur
19/07/2013, 11:42 AM
What kind of field do you use for ID? It doesn't look like SERIAL (int(11) auto increment unique primary key), because 0 isn't a valid ID.
I strongly suggest that you don't rely on database id being mapped to array index.

Anyway, answer to your original question:

forward OnCarIDsLoad();
public OnCarIDsLoad()
{
new rows, fields;
cache_get_data(rows, fields, Handle);

new i = 0, realid = 0;
while(rows > i < 10000)
{
realid = cache_get_row_int(i, 0, Handle);
VehicleLoad[realid] = true;
printf("Car %d created", realid);
i++;
}
print("Loading finished");
return 1;
}