SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 27/01/2016, 07:34 PM   #1
izeatfishz
Huge Clucker
 
izeatfishz's Avatar
 
Join Date: Jun 2012
Posts: 231
Reputation: 43
Default multiple variables to a string

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
izeatfishz is offline   Reply With Quote
Old 27/01/2016, 08:31 PM   #2
Virtual1ty
Banned
 
Join Date: Jun 2009
Location: Croatia (Hrvatska)
Posts: 734
Reputation: 102
Default Re: multiple variables to a string

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.
Virtual1ty is offline   Reply With Quote
Old 27/01/2016, 08:35 PM   #3
izeatfishz
Huge Clucker
 
izeatfishz's Avatar
 
Join Date: Jun 2012
Posts: 231
Reputation: 43
Default Re: multiple variables to a string

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

Code:
//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;
}
izeatfishz is offline   Reply With Quote
Old 27/01/2016, 08:44 PM   #4
Virtual1ty
Banned
 
Join Date: Jun 2009
Location: Croatia (Hrvatska)
Posts: 734
Reputation: 102
Default Re: multiple variables to a string

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.

Code:
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.
Virtual1ty is offline   Reply With Quote
Old 27/01/2016, 09:07 PM   #5
izeatfishz
Huge Clucker
 
izeatfishz's Avatar
 
Join Date: Jun 2012
Posts: 231
Reputation: 43
Default Re: multiple variables to a string

Still does the same:



my sql table for reference:

izeatfishz is offline   Reply With Quote
Old 27/01/2016, 09:54 PM   #6
AmigaBlizzard
Huge Clucker
 
Join Date: Jul 2012
Posts: 320
Reputation: 60
Default Re: multiple variables to a string

PHP Code:
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.
AmigaBlizzard is offline   Reply With Quote
Old 28/01/2016, 10:29 PM   #7
izeatfishz
Huge Clucker
 
izeatfishz's Avatar
 
Join Date: Jun 2012
Posts: 231
Reputation: 43
Default Re: multiple variables to a string

Quote:
Originally Posted by AmigaBlizzard View Post
PHP Code:
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
izeatfishz is offline   Reply With Quote
Old 08/02/2016, 07:37 PM   #8
izeatfishz
Huge Clucker
 
izeatfishz's Avatar
 
Join Date: Jun 2012
Posts: 231
Reputation: 43
Default Re: multiple variables to a string

bumping
izeatfishz is offline   Reply With Quote
Old 08/02/2016, 09:53 PM   #9
Jefff
Banned
 
Join Date: Dec 2007
Posts: 2,594
Reputation: 326
Default Re: multiple variables to a string

pawn Code:
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;
}
Jefff is offline   Reply With Quote
Old 08/02/2016, 10:41 PM   #10
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,083
Reputation: 2655
Default Re: multiple variables to a string

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 Code:
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 Code:
SELECT TABLE_SCHEMAGROUP_CONCAT(TABLE_NAME SEPARATOR ', ') AS tables FROM information_schema.TABLES WHERE TABLE_SCHEMA 'samp-server' GROUP BY TABLE_SCHEMA 
__________________
Vince is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String Variables MrMou6 Scripting Help 7 11/10/2013 03:38 PM
SSCANF is not splitting string to Variables Admigo Scripting Help 2 18/06/2013 08:11 PM
Sscanf not splitting string according to Variables [MG]Dimi Scripting Help 9 18/06/2013 07:28 PM
String error /variables help :( stylerof619 Scripting Help 19 06/09/2011 05:05 PM
[SOLVED] Checking multiple variables for their values CaHbKo Help Archive 5 01/03/2010 01:04 PM


All times are GMT. The time now is 08:27 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.