Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:46:22+00:00 2026-06-17T18:46:22+00:00

I am having some issues editing my code to use the PHP foreach function.

  • 0

I am having some issues editing my code to use the PHP foreach function. My “transactiondata” div below is being used to pull transaction information from my table. I would like to use foreach() to create a new div section for each row in my table. However, when editing my div to include the foreach() I threw a TON of errors so I am back to square 1. What is the best way to do accomplish this?

<?php
include('cn.php');
session_start();

$userUsername = $_SESSION['loggedInUser'];

$sql = "SELECT * FROM transactions WHERE
    UserID = '" . $userUsername . "'";
$result = mysql_query($sql, $cn) or
    die(mysql_error($cn));
$row = mysql_fetch_assoc($result);

$RequestBody = $row['RequestBody'];
$ResponseBody = $row['ResponseBody'];

parse_str($RequestBody);
parse_str($ResponseBody);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $userUsername; ?>'s Profile - Test 2013</title>
<style>
    .userstatustext 
    {
        position: absolute;
        top: 0px; 
        right: 5px;
    }
    .transactiondata
    {
        position: absolute;
        padding: 5px;
        top: 170px;
        left: 75px;
        width: 900px;
        height: 400px;
        overflow-x: hidden;
        background: #B2B2B2;
        border: 1px solid black;
        word-break: break-all; 
        word-wrap: break-word;
    }
</style>    
</head>
<body>
    <table border="0" cellpadding="10">
      <tr>
        <td>
          <img src="../images/api_rat.png">
        </td>
        <td>
          <h1>Transactions - Test 2013</h1>
        </td>
      </tr>
    </table>
<div class="userstatustext">
    <h5>Welcome, <?php echo $userUsername; ?>!</h5>
    <h5><a href="logout.php">[LOGOUT]</a></h5>
</div>
<h3>Your most recent transactions:</h3>
<div class="transactiondata">
<p><h4>Timestamp: </h4><?php echo date("F j, Y g:i a", strtotime($TIMESTAMP));  ?></p>
<p><h4>Payment Status: </h4><?php echo $ACK; ?></p>
<?php 
if(isset($CORRELATIONID))
{
    echo "<p><h4>Correlation ID: </h4></p>";
    echo $CORRELATIONID; 
}
?>
<?php
if(isset($TRANSACTIONID))
{
    echo "<p><h4>Transaction ID: </h4></p>";
    echo $TRANSACTIONID; 
}
?>
<p><h4>Errors Returned (if any): </h4> 
    <a href="https://www.testsite.com/test/search.php?query=<?php echo $ERRORCODE; ?>&search=Search">
        <?php echo $ERRORCODE; ?>
    </a>
</p>
<p><h4>Request Body: </h4><?php echo $RequestBody; ?></p> 
<p><h4>Response Body: </h4><?php echo $ResponseBody; ?></p>
</div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:46:23+00:00Added an answer on June 17, 2026 at 6:46 pm

    Instead of a foreach what you need to use is a while() so you can fetch results into the $row variable while there are any.

    This way for each row your query found you’ll be using mysql_fetch_assoc to retrieve that row into the $row varible and then thread that variable as an array for each column in your table

    I’ve modified your code to match this

    <?php
    include('cn.php');
    session_start();
    
    $userUsername = $_SESSION['loggedInUser'];
    
    $sql = "SELECT * FROM transactions WHERE
        UserID = '" . $userUsername . "'";
    $result = mysql_query($sql, $cn) or
        die(mysql_error($cn));
    
    $RequestBody = $row['RequestBody'];
    $ResponseBody = $row['ResponseBody'];
    
    parse_str($RequestBody);
    parse_str($ResponseBody);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title><?php echo $userUsername; ?>'s Profile - Test 2013</title>
    <style>
        .userstatustext 
        {
            position: absolute;
            top: 0px; 
            right: 5px;
        }
        .transactiondata
        {
            position: absolute;
            padding: 5px;
            top: 170px;
            left: 75px;
            width: 900px;
            height: 400px;
            overflow-x: hidden;
            background: #B2B2B2;
            border: 1px solid black;
            word-break: break-all; 
            word-wrap: break-word;
        }
    </style>    
    </head>
    <body>
        <table border="0" cellpadding="10">
          <tr>
            <td>
              <img src="../images/api_rat.png">
            </td>
            <td>
              <h1>Transactions - Test 2013</h1>
            </td>
          </tr>
        </table>
    <div class="userstatustext">
        <h5>Welcome, <?php echo $userUsername; ?>!</h5>
        <h5><a href="logout.php">[LOGOUT]</a></h5>
    </div>
    <h3>Your most recent transactions:</h3>
    <div class="transactiondata">
    
    <?php 
    while ($row = mysql_fetch_assoc($result))
     {
    ?>
    <p><h4>Timestamp: </h4><?php echo date("F j, Y g:i a", strtotime($row['TIMESTAMP']));  ?></p>
    <p><h4>Payment Status: </h4><?php echo $row['ACK']; ?></p>
    <?php 
        if(isset($row['CORRELATIONID']))
        {
            echo "<p><h4>Correlation ID: </h4></p>";
            echo $row['CORRELATIONID']; 
        }
        if(isset($row['TRANSACTIONID']))
        {
            echo "<p><h4>Transaction ID: </h4></p>";
            echo $row['TRANSACTIONID']; 
        }
    }
    ?>
    <p><h4>Errors Returned (if any): </h4> 
        <a href="https://www.testsite.com/test/search.php?query=<?php echo $ERRORCODE; ?>&search=Search">
            <?php echo $ERRORCODE; ?>
        </a>
    </p>
    <p><h4>Request Body: </h4><?php echo $RequestBody; ?></p> 
    <p><h4>Response Body: </h4><?php echo $ResponseBody; ?></p>
    </div>
    </body>
    </html>
    

    Hope this solves your question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I am still having some issues with this code below. I have received
Having some issues while variable referencing inside foreach loop. This is my CakePHP code
I am having some issues passing commands through to flash from javascript. My code
I am having some issues with a code that is occasionally and sporadically throwing
I'm having some issues getting file uploads to work correctly and the following code
Having some issues running my code local. I have host file setup like this:
Having some issues with C. I have this is my code: // First line
Having some issues with one small function I'm working on for a homework assignment.
I am having some issues with Microsoft Explorer 6/7 and the jQuery clone function.
So I'm having some issues with proper / any use of indexes in Oracle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.