这是可行的,但似乎不是正确的编码方式
我不知道该怎么办
foreach ($data as $tag) {
if ($tag==1) echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
if ($tag==2) echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
if ($tag==3) echo "<a onclick=\"window.location.href='/buscatag/3'\">Peitos grandes</a>,\n";
if ($tag==4) echo "<a onclick=\"window.location.href='/buscatag/4'\">Nudez</a>,\n";
if ($tag==5) echo "<a onclick=\"window.location.href='/buscatag/5'\">Seinen</a>,\n";
if ($tag==6) echo "<a onclick=\"window.location.href='/buscatag/6'\">Violência<a/>,\n";
if ($tag==7) echo "<a onclick=\"window.location.href='/buscatag/7'\">Vida Cotidiana</a>,\n";
if ($tag==8) echo "<a onclick=\"window.location.href='/buscatag/8'\">Harém</>,\n";
if ($tag==9) echo "<a onclick=\"window.location.href='/buscatag/9'\">Ação</a>,\n";
if ($tag==10) echo "<a onclick=\"window.location.href='/buscatag/10'\">Shonen</a>,\n";
if ($tag==11) echo "<a onclick=\"window.location.href='/buscatag/11'\">Super Poderes</a>,\n";
if ($tag==12) echo "<a onclick=\"window.location.href='/buscatag/12'\">Aventura</a>,\n";
if ($tag==13) echo "<a onclick=\"window.location.href='/buscatag/13'\">Fantasia</a>,\n";
if ($tag==14) echo "<a onclick=\"window.location.href='/buscatag/14'\">Loli</a>,\n";
}
答案 0 :(得分:3)
// Define $lookup as
$lookup = [1 => 'Coméda', 2 => 'Ecchi',]; // etc
// Note the relation between key and value
foreach ($data as $tag) {
echo "<a onclick=\"window.location.href='/buscatag/{$tag}'\">" . $lookup[$tag] . "</a>,\n";
}
答案 1 :(得分:1)
在代码中,您可以立即将数字放入标签
尝试
$names = ['name1', 'name2'...];
foreach ($data as $tag) {
echo sprintf("<a onclick=\"window.location.href='/buscatag/%s'\">%s</a>,\n", $tag, $names[$tag-1]);
}
答案 2 :(得分:0)
当您要多次测试同一变量时,可以使用开关使其变得更好:
from Tkinter import *
import ttk
import sqlite3
import signal
import time
import sys
from pirc522 import RFID
# --- classes ---
def delete(self):
selected_item = self.tree.selection()[0] ## get selected item
self.tree.delete(selected_item)
class Item(object):
def __init__(self, unq_id, name, qty, price):
self.unq_id = unq_id
self.product_name = name
self.price = price
self.qty = qty
class Cart(object):
def __init__(self, master=None):
self.content = dict()
self.tree= ttk.Treeview(root, column=("column1", "column2", "column3","column4"), show='headings')
self.tree.heading("#1", text="ITEM ID")
self.tree.column("#1", anchor = "center", width=135)
self.tree.heading("#2", text="ITEM NAME")
self.tree.column("#2", anchor = "center", width=135)
self.tree.heading("#3", text="QUANTITY")
self.tree.column("#3", anchor = "center", width=135)
self.tree.heading("#4", text="PRICE(Rs)")
self.tree.column("#4", anchor = "center", width=135)
self.tree.button_del = Button(root, text="del",command=delete(self))
self.tree.button_del.pack()
self.tree.pack()
def update(self, unq_id,product_name,price):
if unq_id not in self.content:
item = Item(unq_id,product_name,1,price)
treeRow= Item.unq_id,item.product_name,item.qty,item.price)#tuple
self.tree.insert("", END, values=treeRow)
else:
#Already exists
item = self.content.get(unq_id)
for index in self.tree.get_children():
if unq_id == self.tree.item(index)['values'][0]:
x = index
item.qty=item.qty+1
treeRow=(item.unq_id,item.product_name,item.qty,item.price)
self.tree.item(x, values = treeRow)
self.content.update({item.unq_id: item})
return
def get_total(self):
return sum([v.price * v.qty for _, v in self.content.iteritems()])
def get_num_items(self):
return sum([v.qty for _, v in self.content.iteritems()])
'''
def remove_item(self, key):
self.content.pop(key)
'''
def get_item(self, key):
return self.content.get(key)
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.database = "smartShoppingCart.db"
self.v = StringVar()
self.cart = Cart(master)
conn = sqlite3.connect(self.database)
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY, Name TEXT, Qty INT, Price REAL, rfidTag INT)")
#below test case ...will be removed later
#cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (1, 'Banana', 1, 2., 230)")
#cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (2, 'Eggs', 2, 5., 131)")
#cur.execute("INSERT INTO profile(id,Name,Qty,Price,rfidTag) VALUES (3, 'Donut', 3, 1., 128)")
#above test case ...will be removed later
conn.commit()
conn.close()
Label(root, anchor=W, fg="green", justify=RIGHT, font=("Helvetica", 16), text="Total").pack(side = LEFT)
Label(root, anchor=W, fg="red", justify=RIGHT, font=("Helvetica", 16), textvariable=self.v).pack(side = RIGHT)
self.v.set("0.0")
def insertItemToCart(self, rfidTag):
#fetch data from database
string ="SELECT * FROM profile WHERE rfidTag = "+str(rfidTag)
conn = sqlite3.connect(self.database)
cur = conn.cursor()
cur.execute(string)
rows = cur.fetchall()
row = rows[0]
conn.close()
#update cart
unq_id=row[4]
name=row[1]
price=row[3]
self.cart.update(unq_id, name, price)
# --- functions ---
def end_read(signal,frame):
global run
print("\nCtrl+C captured, ending read.")
run = False
rdr.cleanup()
sys.exit()
def check_rdif():
(error, data) = rdr.request()
if not error:
print("\nDetected: " + format(data, "02x"))
(error, uid) = rdr.anticoll()
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
print(uid[0])
app.insertItemToCart(uid[0])
app.v.set(str(app.cart.get_total()))
print "You have %i items in your cart for a total of $%.02f" % (app.cart.get_num_items(), app.cart.get_total())
if run:
# repeate after 20ms (0.2s)
root.after(20, check_rdif)
# --- main ---
signal.signal(signal.SIGINT, end_read)
run = True
root = Tk()
root.geometry("800x300")
root.title('Smart shopping cart')
rdr = RFID()
app = Application(master=root)
print("Starting")
check_rdif()
self.tree.mainloop()