0

I currently have a function that is pulling user tickets from my database. It is pulling that data correctly but for some reason instead of echoing the ticket numbers on two separate lines it is placing them in the first row of the table. Can anybody help me figure out why this would be happening?

enter image description here

This is the function:

function findUserTickets() {
    include_once("dbh.php");
    $id = $_SESSION['id'];
    $sql = "SELECT * FROM tickets WHERE uid=$id";
    $result = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_array($result)) {     
        echo "Ticket #".$row['tid'];
    }
    mysqli_close($conn);
}

This is the HTML code:

<ul>
    <?php
    include("includes/functions.inc.php");
    include_once("includes/dbh.php");
    $userID = $_SESSION['id'];
    $strSQL = "SELECT * FROM tickets WHERE uid='$userID'";
    $rs = mysqli_query($conn, $strSQL);
    while ($row = mysqli_fetch_array($rs)) {
    ?>
        <li>
            <div class="hk_sug_domian_name">
                <p>
                    <?php echo findUserTickets(); ?>
                </p>
            </div>

            <div class="hk_domain_price">
                <span class="active_price"><?php echo checkTicketStatus(); ?>  </span>
                <div class="hk_promot_btn"><a class="hk_btn" href="progress.php">View</a></div>
            </div>
        </li>
    <?php 
    }
    mysqli_close($conn);
    ?>
</ul>
3
  • It is echoing on the same line, because in your loop you have echo "Ticket #".$row['tid']; without any line breaks. Commented Dec 28, 2016 at 3:50
  • Also, your code makes no sense. Why are you doing a function, where you select the same table/data, inside a loop where you are selecting the same table/data? There are multiple redundancies here. Commented Dec 28, 2016 at 3:52
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Dec 28, 2016 at 4:16

1 Answer 1

1

Instead of calling of function directly you can use $row

change from

<div class="hk_sug_domian_name">
        <p>
            <?php echo findUserTickets(); ?>
        </p>
</div>

to

<div class="hk_sug_domian_name">
        <p>
            <?php echo "Ticket #".$row['tid']; ?>
        </p>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.