开发者

Output Array Elements in Certain Manner

开发者 https://www.devze.com 2023-04-10 03:50 出处:网络
Admin: Please create image urls as images I\'m trying to achieve the following output: // Joseph Dickinson

Admin: Please create image urls as images I'm trying to achieve the following output:

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

Currently, I get the name "Joseph Dickinson" written for each offer like "John Doe 149.99" or "Jane Doe 154.99". I want it ONCE for each, Title like "Need Xbox 360" http://i.stack.imgur.com/ERLbX.png

This page gathers this info through a php file:

<?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 I'm Using

    <?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);
    }   
    ?>

How do I get it to output in that manner开发者_JS百科 I listed above? Would it be easier to use array indexes? How do I achieve the // // above? http://i.stack.imgur.com/dBhyx.png


Try this:

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

If you change $result = $sth->fetchAll(PDO::FETCH_NUM); into $result = $sth->fetchAll(PDO::FETCH_ASSOC); you can get the same result with more readable code:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号