My name is Sol and I'm kinda new here. I'm trying to finish my CS50 PSET 7 and I got stuck. Both in index.html and history.html I have the same problem. I'm trying to populate a table with info taken from the SQL table, but I only manage to get 1 row.
I looked for help in forums, but everyone seems to be coding this part very differently, and before trying that way, I wanted to know if there's a way to do this task the way I'm trying it.
Here's my python:
@app.route("/history")
@login_required
def history():
userId = session["user_id"]
# display all stocks owned in 1 table:
stocks = db.execute("SELECT * FROM stocks WHERE id = ?", userId)
dates = []
prices = []
symbols = []
shares = []
for stock in stocks:
symbols.append(stock['symbol'])
# amount of stocks owned
shares.append(stock['numShares'])
# price when bought or sold
if stock['priceSell'] != 0:
prices.append(stock['priceSell'])
else:
priceBought = -(stock['priceBuy'])
prices.append(priceBought)
# date of transaction
dates.append(stock['date'])
# Ensure there are transactions.
if len(db.execute("SELECT * FROM stocks WHERE id = ?", userId)) == 0:
return apology("You haven't made any transactions yet", 403)
# Get transactions history.
else:
return render_template("history.html", symbols=[symbols], shares=[shares], prices=[prices], dates=[dates])
And this is my HTML:
{% extends "layout.html" %}
{% block title %}
History
{% endblock %}
{% block main %}
<table class = table>
<thead>
<td>Transaction</td>
<td>Symbol</td>
<td>Price</td>
<td>Number of shares</td>
<td>Date</td>
</thead>
<tr>
{% for price in prices %}
{% for p in price %}
{% if p > 0 %}
<td>{{ 'Sell' }}</td> {# poner en color verde #}
{% else %}
<td>{{ 'Purchase' }}</td> {# poner en color azul #}
{% endif %}
{% endfor %}
{% endfor %}
{% for symbol in symbols %}
{% for s in symbol %}
<td>{{ s }}</td>
{% endfor %}
{% endfor %}
{%- for price in prices -%}
{% for p in price %}
<td>{{ p | usd }}</td>
{% endfor %}
{%- endfor -%}
{% for share in shares %}
{% for sh in share %}
<td>{{ sh }}</td>
{% endfor %}
{% endfor %}
{% for date in dates %}
{% for d in date %}
<td>{{ d }}</td>
{% endfor %}
{% endfor %}
</tr>
</table>
{% endblock %}
PYTHON EDIT:
@app.route("/history")
@login_required
def history():
userId = session["user_id"]
# display all stocks owned in 1 table:
stocks = db.execute("SELECT * FROM stocks WHERE id = ? ORDER BY id DESC", userId)
if len(db.execute("SELECT * FROM stocks WHERE id = ?", userId)) == 0:
return apology("You haven't made any transactions yet", 403)
# Get transactions history.
else:
return render_template("history.html", stocks = stocks)
HTML EDIT:
{% extends "layout.html" %}
{% block title %}
History
{% endblock %}
{% block main %}
<table class = table>
<thead>
<td>Transaction</td>
<td>Symbol</td>
<td>Price</td>
<td>Number of shares</td>
<td>Date</td>
</thead>
<tbody>
{% for stock in stocks %}
<tr>
<td>
{% if stock.priceSell > 0 %}
{{ 'Sell' }} {# poner en color verde #}
{% else %}
{{ 'Purchase' }} {# poner en color azul #}
{% endif %}
</td>
<td>{{ stock.symbol }}</td>
<td>
{% if stock.priceSell > 0 %}
{{ stock.priceSell }}
{% else %}
{{ stock.priceBuy }}
{% endif %}
</td>
<td>{{ stock.numShares }}</td>
<td>{{ stock.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
database:stocks = db.execute("SELECT * FROM stocks WHERE id = ? ORDER BY id DESC", userId)
stocks
schema look like? I could see a problem if it has key called "id" and another column for user_id.