multiple variables to a string
#1

I'm wanting to fetch 'charges' from my sql table, and to attach many of them in one string, for example

Let's say these are the charges

CHAR_NAME, Hit and run, other variables
CHAR_NAME, Murder, other variables

And attach them to a string so if I printed the string it would return

'Hit and Run, Murder'

I've tried this so far:

for(new z = 0; z < cache_get_row_count(); z++)
{
cache_get_field_content(0, "Reason", list);
//format(list, sizeof(list), "%s", list);
strcat(charges, list);
}
SetPVarString(playerid, "mdcreturn", charges);

But it currently only returns:

'Hit and run, Hit and run' and not 'Hit and Run, Murder' as it should

Any help would be amazing
Reply
#2

We're not psychic. We can not guess what other code you have in place because you didn't show us.
A general advice is this: post the whole function, just edit the uneccessary things out.

Now onto your problem, first, for what do you need the 'z' variable and your loop if you don't use it? You could've just used "while".

Also show us how you actually format the "charges" string at the start.

What you've given us just isn't enough to help you at all I'm sorry.
I could only guess it is something like an "OBOE", you're looping through your rows+1 or something like that.
Reply
#3

You're right, my bad, let me show you the whole thing, It was rushed hence the lack of efficiency:

Код:
//display charges for debugging
CMD:charges(playerid, params[])
{
	new charge[200];
    GetPVarString(playerid, "mdcreturn", charge, sizeof(charge));
    SCM(playerid, -1, charge);
	return 1;
}


//load charges, loads on connect

public CheckCharges(playerid)
{
    new charges[520];
    new list[520];
    new list2[520];
   	if(cache_get_row_count() == 0)
	{
		format(charges, sizeof(charges), "No charges found...");
		SetPVarString(playerid, "mdcreturn", charges);
		return 1;
	}
	else
	{
		for(new z = 0; z < cache_get_row_count(); z++)
		{
			cache_get_field_content(0, "Reason", list);
			format(list2, sizeof(list2), "%s ", list);
			strins(charges, list2, 0, 128);
 		}
		SetPVarString(playerid, "mdcreturn", charges);
	}
	return 1;
}
Reply
#4

Aha, just as I thought. Well I could go on about how that's not a good scheme to be doing but whatever.

You call "cache_get_row_count" twice, not a big deal but could be improved by just calling it ONCE.

Код:
public CheckCharges(playerid)
{
	new charges[520], rows;
	rows = cache_get_row_count();
	if(rows == 0)
	{
		format(charges, sizeof(charges), "No charges found...");
		SetPVarString(playerid, "mdcreturn", charges);
		return 1;
	}
	else
	{
		while (rows--)
		{
			cache_get_field_content(0, "Reason", list);
			format(charges, sizeof(charges), "%s%s,", charges, list);
		}
		SetPVarString(playerid, "mdcreturn", charges);
	}
	return 1;
}
Should work, untested though.
Reply
#5

Still does the same:



my sql table for reference:

Reply
#6

PHP код:
cache_get_field_content(z"Reason", list); 
Always reading the first row (row 0) is your problem.
You need the z variable to point to the correct row to read the result.
Reply
#7

Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
PHP код:
cache_get_field_content(z"Reason", list); 
Always reading the first row (row 0) is your problem.
You need the z variable to point to the correct row to read the result.
But now I've used the row code and not using the z code, if I replace it with row anyway and not z it just prints no string at all
Reply
#8

bumping
Reply
#9

pawn Код:
public CheckCharges(playerid)
{
    new rows = cache_get_row_count();
    if(rows == 0) SetPVarString(playerid, "mdcreturn", "No charges found...");
    else
    {
        new list[128], charges[520];
        for(new i = 0; i < rows; i++)
        {
            cache_get_field_content(i, "Reason", list);
            format(charges, sizeof(charges), "%s%s ", charges, list);
        }
        print(charges);
        SetPVarString(playerid, "mdcreturn", charges);
    }
    return 1;
}
Reply
#10

Should be simple enough if you fetch it as string directly from MySQL using GROUP_CONCAT(). This does exactly what you want and you don't need to process it any further in Pawn.

PHP код:
SELECT NameGROUP_CONCAT(Reason SEPARATOR ', ') AS Crimes FROM yourtable WHERE Name 'Avril' GROUP BY Name 
And while we're at it, this one may be useful to list all the current tables in your database:
PHP код:
SELECT TABLE_SCHEMAGROUP_CONCAT(TABLE_NAME SEPARATOR ', ') AS tables FROM information_schema.TABLES WHERE TABLE_SCHEMA 'samp-server' GROUP BY TABLE_SCHEMA 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)