0

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 %}
5
  • This likely does not generate valid html (right-click the page, view Source, select all,copy, paste into validator). I think the problem is the html has only one <tr> tag therefore the table has one row. Jul 11, 2022 at 16:46
  • I've changed it, but still get just one row. In python, I simplified the code by just getting the data from the SQL database:stocks = db.execute("SELECT * FROM stocks WHERE id = ? ORDER BY id DESC", userId)
    – Avanoc
    Jul 16, 2022 at 15:04
  • What does the stocks schema look like? I could see a problem if it has key called "id" and another column for user_id. Jul 16, 2022 at 19:33
  • 'stocks' ('id' INTEGER PRIMARY KEY NOT NULL DEFAULT 'users', 'symbol' TEXT NOT NULL, 'numShares' INTEGER NOT NULL,'date' date, 'priceBuy' REAL NOT NULL DEFAULT 0, 'priceSell' REAL NOT NULL DEFAULT 0) I think I see what you mean. I'm using an id column that's not related to the userId (which was my intention).
    – Avanoc
    Jul 18, 2022 at 20:22
  • 1
    I did it!!! Thank you DinoCoderSaurus for putting me on the right path :D
    – Avanoc
    Jul 19, 2022 at 22:40

0

You must log in to answer this question.

Browse other questions tagged .