SA-MP Forums Archive
multiple variables to a string - 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: multiple variables to a string (/showthread.php?tid=599578)



multiple variables to a string - izeatfishz - 27.01.2016

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


Re: multiple variables to a string - Virtual1ty - 27.01.2016

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.


Re: multiple variables to a string - izeatfishz - 27.01.2016

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;
}



Re: multiple variables to a string - Virtual1ty - 27.01.2016

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.


Re: multiple variables to a string - izeatfishz - 27.01.2016

Still does the same:



my sql table for reference:




Re: multiple variables to a string - AmigaBlizzard - 27.01.2016

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.


Re: multiple variables to a string - izeatfishz - 28.01.2016

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


Re: multiple variables to a string - izeatfishz - 08.02.2016

bumping


Re: multiple variables to a string - Jefff - 08.02.2016

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;
}



Re: multiple variables to a string - Vince - 08.02.2016

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