Printing appropriately based on values retrieved

Printing appropriately based on values retrieved

Postby NikosGr » Sat Nov 02, 2013 8:53 pm

Code: Select all
for row in newdata:
(host, refs, city, useros, browser, visits, hits, downloads) = row
    if downloads != 'No download yet':
        print( '<td><select>' )
        for n, download in enumerate( downloads ):
            if n == 0:
                op_selected = 'selected'
            else:
                op_selected = ''
            print( '<option %s> %s </option>' % (op_selected, download) )
        print( '</select></td>' )
    else:
        print( '<td><center><b><font color=white> 'No downloads yet' </td>' )


The above code is supposed to print a select menu in case it has found tha user has downloaded files from my website and if he didn't to just print a string 'No downloads yet'.
Unfortunately as i have it, it prints 'No downloads yet' inside a select menu giving the impression that the 'No downloads yet' was actually a file.
how should i write it to have the result i want it to be?


Code: Select all
if downloads != No download yet!':
    print( '<td><select>' )
    for n, download in enumerate( downloads ):
        if n == 0:
            op_selected = 'selected'
        else:
            op_selected = ''
        print( '<option %s> %s </option>' % (op_selected, download) )
    print( '</select></td>' )
    times = times + 1

if times == 0:
    print( '<td><center><b><font color=white> No download yet! </td>' )


I just tried this this didn't work either :(
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Printing appropriately based on values retrieved

Postby ochichinyezaboombwa » Mon Nov 04, 2013 8:06 pm

1st, you have some syntax errors (single quotes within single quotes,....);
2nd, we have no idea what's your variable "downloads" holds.
Just try:
Code: Select all
(host, refs, city, useros, browser, visits, hits, downloads) = row
print "<p> %s </pt>" % (downloads)
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Printing appropriately based on values retrieved

Postby NikosGr » Mon Nov 04, 2013 8:17 pm

I have managed to make my code work by:
Code: Select all
create table visitors
(
  counterID integer(5) not null,
  host varchar(50) not null,
  refs varchar(25) not null,
  city varchar(20) not null,
  userOS varchar(10) not null,
  browser varchar(10) not null,
  visits datetime not null,
  hits integer(5) not null default 1,
  downloads varchar(50) not null default '',
  foreign key (counterID) references counters(ID),
  unique index (visits)
 )ENGINE = MYISAM;


Code: Select all
# add this visitor entry into database (hits && downloads are defaulted)
cur.execute('''INSERT INTO visitors (counterID, refs, host, city, useros, browser, visits) VALUES (%s, %s, %s, %s, %s, %s, %s)''',  (cID, ref, host, city, useros, browser, lastvisit) )

and later on ....
Code: Select all
torrents = []
# check if visitor has downloaded movies
for download in downloads:
    if download != '':
        torrents.append( download )
# present visitor's movie picks if any
if torrents:
    print( '<td><select>' )
        for n, torrent in enumerate( torrents ):
        if n == 0:
            op_selected = 'selected'
        else:
            op_selected = ''
        print( '<option %s> %s </option>' % (op_selected, torrent) )
    print( '</select></td>' )
else:
    print( '<td><center><b><font color=white> None Download yet! </td>' )
break

Please since this column you mentioned is able to store a Python's list datatype could you tell me what needs alternation in:
1. MySQL's visitor's table definition time
2. python database cur.execute method
3. retrieval time
Thank you very much for anyone wishes to give me a hand here.
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Printing appropriately based on values retrieved

Postby ochichinyezaboombwa » Wed Nov 06, 2013 3:35 am

Your 2nd post has code completely unrelated to your 1st post. Why? Did you try my advice # 2?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron