以某种方式输出数组元素
管理员:请将图像网址创建为图像 我正在尝试实现以下输出:
//
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
//
目前,我为每个报价写入了名称“Joseph Dickinson”,例如“John Doe 149.99”或“Jane Doe 154.99”。我想要每个人一次,标题如“需要 Xbox 360” https://i.sstatic.net/ERLbX.png
此页面通过 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
<?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'); ?>
Cost: <input type="text" name="ProductAskingPrice" /> <br>
Shipping: <input type="text" name="ProductAskingShipAmount" /> <br>
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);
}
?>
我如何让它以上面列出的方式输出?使用数组索引会更容易吗?如何实现上面的 // // ? https://i.sstatic.net/dBhyx.png
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"
https://i.sstatic.net/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'); ?>
Cost: <input type="text" name="ProductAskingPrice" /> <br>
Shipping: <input type="text" name="ProductAskingShipAmount" /> <br>
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 I listed above? Would it be easier to use array indexes? How do I achieve the // // above?
https://i.sstatic.net/dBhyx.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
如果将
$result = $sth->fetchAll(PDO::FETCH_NUM);
更改为$result = $sth->fetchAll(PDO::FETCH_ASSOC);< /code> 您可以使用更具可读性的代码获得相同的结果:
Try this:
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: