以某种方式输出数组元素

时间:2011-10-04 00:21:45

标签: php mysql arrays multidimensional-array array-splice

管理员:请将图片网址设为图片 我正在尝试实现以下输出:

//
    Joseph Dickinson
    Title: Need Xbox 360
    Comment: I need one quick!
    on 2011-09-15
    John Doe 149.99
    Jane Doe 154.99
    Diana Matthews 160.00
    Amanda Koste 174.99
//

目前,我为“John Doe 149.99”或“Jane Doe 154.99”等每个优惠写了“Joseph Dickinson”这个名字。我希望每个人都有,像“需要Xbox 360”这样的标题 http://i.stack.imgur.com/ERLbX.png

此页面通过php文件收集此信息:

<?php require_once('inc/db/dbc.php'); ?>
<?php
#GET User (Buyer) Info and General Listing Info
$pdo = new PDO($h1, $u, $p);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
  SELECT uFName, uLName, listTitle, listCmt, listDt, mBCFName, mBCLName, moAmt, moDtOff
  FROM User U, Listing L, Merchant M, MerchantOffer MO
  WHERE U.uID = L.uID
  and L.listID = MO.listID
  and M.mID = MO.mId
  LIMIT 0,5
;');
$sth->execute(array());

?>

PHP我正在使用

    <?php $usrBrowser = $_SERVER['HTTP_USER_AGENT']; $todayDt = date('Y-m-d');  ?>
    <form id="AddListing" method="post" action="#">
        <input type="hidden" name="theUserID" value="" /> <br>
        Product Wanted: <input type="text" name="ListingTitle" /> <br>
        Listing Length:<?php require_once('inc/php/listingLengths.php'); ?>
        &nbsp;&nbsp;&nbsp;Cost: <input type="text" name="ProductAskingPrice" /> <br>
        &nbsp;&nbsp;&nbsp;Shipping: <input type="text" name="ProductAskingShipAmount" /> <br>
        &nbsp;&nbsp;&nbsp;Additional Comment: <input type="text" name="ListingComment" /> <br>  
        <input type="hidden" name="ListingDateOfEntry" value="<?php echo $todayDt; ?>" /> <br>  
        <input type="hidden" name="ListingUserBrowserType" value="<?php echo $usrBrowser; ?>" /> <br>
        <input type="submit" value="List" />
    </form>
    <?php 
    $result = $sth->fetchAll(PDO::FETCH_NUM);
    #print_r($result); //or var_dump($result); for more info
    foreach($result as $row){
        $half = array_splice($row,0,5);
        echo implode("<br> ",$half)."<br />".implode(" ",$row);
    }   
    ?>

如何以上面列出的方式输出?使用数组索引会更容易吗?我如何实现// //以上?  http://i.stack.imgur.com/dBhyx.png

1 个答案:

答案 0 :(得分:0)

试试这个:

foreach($result as $row)  
  {  
  print $row[0] . ' ' . $row[1] . '<br>Title: ' . $row[2] . '<br>Comment: ' . $row[3] ...   . '<br>';
  }  

如果您将$result = $sth->fetchAll(PDO::FETCH_NUM);更改为$result = $sth->fetchAll(PDO::FETCH_ASSOC);,您可以使用更易读的代码获得相同的结果:

print $row['uFName'] . ' ' . $row['uLName'] . '<br>Title: ' . $row['listTitle'] . '<br>Comment: ' . $row['listCmt'] ... . '<br>';