Sign in to follow this  
Followers 0
robhunter

Create a string from a "for" statment

3 posts in this topic

Hi,

I need to create a string outside a for element, this string must contain the whole parameters that FOR will return... This is my FOR statment code

For $i = 1 To UBound($modulos) - 1
  $rt = IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "rt", 0)
  $trt = ""
  $vrt = ""
  $modulo_rt = ""
  $estado_rt = ""
  $celda_rt = ""
  $modulo_ok = $modulos[$i] & "a"
  $estado_ok = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "estadook", 0))
  $celda_ok = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "celdaok", 0))
  $modulo_ko = $modulos[$i] & "a"
  $estado_ko = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "estadoko", 0))
  $celda_ko = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "celdako", 0))
  If $rt = 1 Then
   $modulo_rt = "rt" & $modulos[$i]
   $estado_rt = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "estadort", 0))
   $celda_rt = _ExcelReadCell($oExcel, IniRead(@ScriptDir & "\" & $entidad & ".ini", $modulos[$i], "celdart", 0))
   $trt = ",'" & $modulo_rt & "'"
   $vrt = ",'" & $celda_rt & "'"
  EndIf
  $all_modulos = $modulo_ok & "," & $modulo_ko & $trt
  $valores_modulos = "'" & $celda_ok & "','" & $celda_ko & "'" & $vrt
  $estado = _ExcelReadCell($oExcel, "B48")
  $hora = _ExcelReadCell($oExcel, "B44")
  $query = "INSERT INTO pas_" & $entidad & " (hora," & $all_modulos & ",estado) VALUES ('" & $hora & "'," & $valores_modulos & ",'" & $estado & "')"
  $Consulta = _EzMySql_Exec($query)
  MsgBox(0, "MySQL2", $Consulta & "Error: " & @error & @CR & _EzMySql_ErrMsg())
Next

this is obviously not working as I want, because It only returns me one string per FOR loop and I don't want this, because I need one query with UBound($modulos) - 1 fields and values ($all_modulos and $valores_modulos)

I hope I'd explained good, thanks.

Share this post


Link to post
Share on other sites



In short, are you looking for something like this:

Local $sAllStrings = ""

For $i = 1 to 10
    ; We do something in the loop, using $sLoopString

    $sLoopString = "Loop " & $i

    $sAllStrings &= $sLoopString & @CRLF
Next

ConsoleWrite($sAllStrings)

Is that a simple version of what you want to do?

Break up the code into more simple components and then post something simple, that is commented to explain what you expect the output to be. Hopefully something we can run as well.

Share this post


Link to post
Share on other sites

Hi, you can use this code

$s_modulos = "hello,goodbye"
Global $entidad = "Test"
$modulos = StringSplit($s_modulos, ",") ; Array de modulos
For $i = 1 To UBound($modulos) - 1
  $rt = 1 ; This, in the real code can be 1 or 0 depending on what the .ini file contains, but doesnt matter now
  $modulo_ok = $modulos[$i] & "_a" ; so it will print "Hello_a"
  $celda_ok = $i
  $modulo_ko = $modulos[$i] & "_b" ; so it will print "Hello_b"
  $celda_ko = $i
  If $rt = 1 Then ; This is because if INI says it has rt=0, this will be skipped but in this example we'll have it always set to 1 so..
   $modulo_rt = "c_" & $modulos[$i] ; this will print "c_Hello"
   $celda_rt = $i
   $trt = "," & $modulo_rt & "" ; this is what I use to print ",c_Hello" for the SQL Query
   $vrt = ",'" & $celda_rt & "'" ; this is what I use to print ",'1'" or ",'2'" for the SQL Query
  EndIf
  $all_modulos = $modulo_ok & "," & $modulo_ko & $trt ; This must print "Hello_a, Hello_b, rt_Something" and "Goodbye_a, Goodbye_b, rt_Goodbye"
  $valores_modulos = "'" & $celda_ok & "','" & $celda_ko & "'" & $vrt ; This must print "'1', '1', '1'" and "'2', '2', '2'"
  $estado = 0
  $hora = "23:40"
  $query = "INSERT INTO pas_" & $entidad & " (hora," & $all_modulos & ",estado) VALUES ('" & $hora & "'," & $valores_modulos & ",'" & $estado & "')"
  ; ^I want this $query to print this: "INSERT INTO pas_test (hora, hello_a, hello_b, rt_something, goodbye_a, goodbye_b, rt_goodbye, estado) VALUES ('23:40','1','1','1','2','2','2','0')"
  MsgBox(0, "", $query)
Next

I've simplified the other one I posted and giving some values (because I was using .ini)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0