having real hardtime to remove last comma within a loop

DaNcE-eViL

[H]ard|Gawd
Joined
Oct 31, 2001
Messages
1,128
hey,

I was hoping you guys could point me out in right direction or help me with this coding, simply I'm having hard time to get rid of the last comma "," with mark as red

eg:

"21115","21121","21117",

to

"21115","21121","21117"


if i use this code below it will remove all ","

Code:
Left(str,Len(str)-1)



For above issue, the coding can be found below: mark as yellow

Code:
mySQL3 = "SELECT classification " _
& "FROM   cartRows " _
& "WHERE  idOrder = " & validSQL(idOrder,"I") & " AND Project = 'Image' AND idproduct = '"&IDproduct&"' "
set rs3 = openRSexecute(mySQL3)
if NOT rs3.eof then

RemoveClassificationGroup = rs3("classification")
test = rs3.GetRows()



Dim iRowLoop, iColLoop
For iRowLoop = 0 to UBound(test, 2)
  For iColLoop = 0 to UBound(test, 1)
   [COLOR="Yellow"] Response.Write (""""&(test(iColLoop, iRowLoop)&""",")) 'checking output value[/COLOR]
	'thisArray = Array("21115","21121","21117") 'Testing this value to see if it is working
  Next 'iColLoop
Next 'iRowLoop


else
end if

after that i'd like to put those value into array ()
 
Set the comma in a variable outside of the loop and then write it only if the current loop count isn't the ubound of whatever you don't want to have the trailing comma.
 
Set the comma in a variable outside of the loop and then write it only if the current loop count isn't the ubound of whatever you don't want to have the trailing comma.
Agreed. Take the logic of adding a comma+space outside of your value appending logic.
 
just to throw it out there (pseudocode obviously)
Code:
if array length > 0 {
  out += array[0];

  for (i = 1; i < array length; i++) {
    out += ",";
    out += array[i];
  }
}
there is slight redundancy by appending the first value outside the for loop but then you don't have to have an if inside the loop for the comma.
 
Set the comma in a variable outside of the loop and then write it only if the current loop count isn't the ubound of whatever you don't want to have the trailing comma.

This ^
 
tim_m's is probably the best way to approach it. Your special case is the first element, not the last.
 
thanks guys for advice, i've kind of change the coding to get it working fine....

here is the coding just in case if anyone want to know =)

Code:
mySQL3 = "SELECT classification " _
& "FROM   cartRows " _
& "WHERE  idOrder = " & validSQL(idOrder,"I") & " AND Project = 'Image' AND idproduct = '"&IDproduct&"' "
set rs3 = openRSexecute(mySQL3)
Do While Not rs3.eof
RemoveClassificationGroup = rs3("classification")

test = (RemoveClassificationGroup)


	thisArray = Array(test)
	For i = 0 To UBound(thisArray) - 1
	strSql = strSql & thisArray(i)
	Next
	' grab last element in the array
	strSql = strSql & "'"&thisArray(UBound(thisArray)) &"',"

Response.Write strSql

rs3.MoveNext
Loop

this code below can be place anywhere outside of the above coding!

to remove last comma

Code:
Response.Write Left(strSql,Len(strSql)-1)
 
Now is probably a good time to mention this:

You should not be building SQL queries by concatenating strings like you are doing.
 
Imagine having a search function where you can search Projects, and you build queries like that. Then think about whats going to happen when some jokester does a search for Projects, and searches for "blah; drop table cartRows;"

Can you please tell me why?
 
Thanks for your concern regarding SQL injection I knew about it, however above script is for only internal usage which can pass the information between two different function which I'm going to do thatl later on...

User will not be able to run the SQL injection...:eek:

Once again thanks for advice :D
 
Join() function

This is generally the way to go. Strings are immutable in many languages so it's often slower to concatenate strings over and over instead of just adding to a list and joining at the end. Plus it makes for cleaner code.

IMO, it doesn't matter whether or not this app is for internal use -- write it properly. You never know what's going to happen. E.g. Your network gets hacked and the attacker uses this app to drop a table from your database, then what?
 
Thanks for your concern regarding SQL injection I knew about it, however above script is for only internal usage which can pass the information between two different function which I'm going to do thatl later on...

User will not be able to run the SQL injection...:eek:

Once again thanks for advice :D

Right, but what happens when that join function uses a record in a table that a user put a sql injection attack into? For instance, little Bobby tables in the name column.
 
Right, but what happens when that join function uses a record in a table that a user put a sql injection attack into? For instance, little Bobby tables in the name column.
For someone who struggles to remove a comma from the end of a string, yet knows all about injection attacks. I hate to say it, but I think you're asking the wrong person - the OP knows just enough to be dangerous.
 
Back
Top