2013-01-24 360 views
-4

我有一些問題編輯我的代碼使用PHP的foreach函數。我的「transactiondata」div用於從我的表中提取交易信息。我想使用foreach()爲我的表中的每一行創建一個新的div部分。然而,當編輯我的div來包含foreach()時,我拋出了一大堆錯誤,所以我回到了方塊1.完成這個任務的最好方法是什麼?PHP的foreach問題

<?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> 
+3

什麼的foreach?在你的代碼中沒有一個... –

+0

你得到了什麼錯誤? –

+0

@MarcB可能不是單次調用'$ row = mysql_fetch_assoc($ result);'他想在foreach中調用它,或者在... – shadyyx

回答

1

取而代之的是你需要使用的是一個while(),所以你可以在有任何結果的時候將結果提取到$ row變量中。

這種方式使每一行查詢發現,你將使用mysql_fetch_assoc來檢索該行到$行varible然後線程該變量在你的表中的每個列的數組

我已經修改了你的代碼以匹配該

<?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> 

希望這能解決你的問題

1

可能您希望輸出查詢返回的每一行。

因此使用while而不是期望foreach

while($row = mysql_fetch_assoc($result)) { 
    // DO YOUR STUFF HERE 
} 

我也建議不要使用mysql_*功能,因爲它們現在已經過時,但使用PDO或至少mysqli_*代替。