This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Loop Help

Help for those learning Tcl or writing their own scripts.
Post Reply
R
Rolet
Voice
Posts: 3
Joined: Mon Dec 01, 2008 8:33 am

Loop Help

Post by Rolet »

hey i made this code for my ircd network but it seems the loop wont work, it will show one user but if 2 users have the same ip it wont, any help would be greatful, code below :)

Code: Select all

       set sql "SELECT ip, username FROM `ircusers` WHERE ip = '$text'"
       set result [mysqlsel $db_handle $sql -list]
       if {$result > 0} {

for {set i 1} {$i < 10} {incr i 1} { 

       set record [lindex $result $i];
       set ip [lindex $record 0];
       set username [lindex $record 1];


       set sql2 "SELECT joindate FROM `forumusers` WHERE username='$username'"
       set result2 [mysqlsel $db_handle $sql2 -list]

        set record2 [lindex $result2 $i];
        set joindate [lindex $record2 0]; 
also tried loop as

Code: Select all

for {set i 0} {$i < 10} {incr i} {
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Since you are using lists, have you considered using a foreach-loop? Saves you the hazzle of manually retrieving the row item for each iteration.

I am somewhat puzzled by why you use index $i rather than index 0 in your second query. Being separate tables, there is no way of guaranteeing the same ordering, especially without any sorting directive.
In fact, I would suggest you do a (sql)join instead, which would actually save you one query along with having all the info available at once.

That would be done something like below, edit to suite your needs. If needed, use some sql-client to verify the output of the query.

Code: Select all

set sql "SELECT ip.ircusers, username.ircusers, joindate.forumusers FROM ircusers, forumusers WHERE username.ircusers = username.forumusers AND ip.ircusers = '[mysqlescape $text]'"
foreach [list ip username joindate] [mysqlsel $sql -flatlist] {
 #puthelp "PRIVMSG somewhere :IP: $ip, Username: $username, Join date: $joindate"
}
NML_375
Post Reply