 |
PHP How-To Post your "How do I do this with PHP?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 19th, 2004, 03:03 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
displaying the query
Hello,
I'm wondering if there is a way to output query result in horizontal instead of vertical way.
For example,
Right now it displays:
Title | Author | Date | Body |
***********************************
Title1 | author1 | date1 | body1|
Title2 | author2 | date2 | body2|
Title3 | author3 | date3 | body3|
Iâd like it to look like :
Title || title1 |title2 | title3 |
Author || author1 | author2| author3 |
Date || date1 | date2 | date3 |
Body || body1 | body2 | body3 |
Does anyone know how to do that?
Thank you,
|

August 19th, 2004, 03:19 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Code:
$query = mysql_query("query text");
$titles = Array();
$authors = Array();
$dates = Array();
$bodies = Array();
while($data = mysql_fetch_assoc($query))
{
$titles[] = $data['title'];
$authors[] = $data['author'];
$dates[] = $data['date'];
$bodies[] = $data['body'];
}
echo "<table><tr><td>titles</td>";
foreach($titles as $title)
{
echo "<td>$title</td>";
}
echo "</tr><tr><td>authors</td>";
foreach($authors as $author)
{
echo "<td>$author</td>";
}
echo "</tr><tr><td>dates</td>";
foreach($dates as $date)
{
echo "<td>$date</td>";
}
echo "</tr><tr><td>body</td>";
foreach($bodies as $body)
{
echo "<td>$body</td>";
}
echo "</tr></table>";
HTH,
Snib
<><
|

August 20th, 2004, 10:40 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you,
That works. Could you please tell me if I want body to be trimmed and I have a trim function trimbody($text), where should I put it : when I create array or while I output $bodies as $body.
such as in line:
$bodies[] = trimbody($data['body']);
Or in line:
foreach($bodies as $body)
{
echo "<td>trimbody($body)</td>";
}
I tried both, but it gave me errors.
Please help,
|

August 21st, 2004, 06:04 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Say you want the first 50 words, try this:
Code:
$query = mysql_query("query text");
$titles = Array();
$authors = Array();
$dates = Array();
$bodies = Array();
while($data = mysql_fetch_assoc($query))
{
$titles[] = $data['title'];
$authors[] = $data['author'];
$dates[] = $data['date'];
$words = explode(' ',$data['body']);
$bodies[] = array_slice($words,0,50) . " ...";
}
echo "<table><tr><td>titles</td>";
foreach($titles as $title)
{
echo "<td>$title</td>";
}
echo "</tr><tr><td>authors</td>";
foreach($authors as $author)
{
echo "<td>$author</td>";
}
echo "</tr><tr><td>dates</td>";
foreach($dates as $date)
{
echo "<td>$date</td>";
}
echo "</tr><tr><td>body</td>";
foreach($bodies as $body)
{
echo "<td>$body</td>";
}
echo "</tr></table>";
Snib
<><
|

August 22nd, 2004, 01:07 AM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OK, I did that:
$words = explode(' ',$data['body']);
$bodies[] = array_slice($words,0,50) . " ...";
foreach($bodies as $body)
{
echo "<td>$body</td>";
}
The result printed on the screen is:
Body: | Array ... | Array ... | Array ... | Array ... |
How to fix that so it print text (first 50 words instead of word Array) of the body?
Thank you,
|

August 23rd, 2004, 12:50 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Try this in place of what I said before:
$words = explode('',$data['body']);
$words = array_slice($words,0,50);
$bodies[] = implode(' ',$words);
Snib
<><
|

August 23rd, 2004, 11:11 PM
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you,
It works.
|
|
 |