Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
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
 
Old August 19th, 2004, 03:03 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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,


 
Old August 19th, 2004, 03:19 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

<><
 
Old August 20th, 2004, 10:40 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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,

 
Old August 21st, 2004, 06:04 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

<><
 
Old August 22nd, 2004, 01:07 AM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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,

 
Old August 23rd, 2004, 12:50 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try this in place of what I said before:

$words = explode('',$data['body']);
$words = array_slice($words,0,50);
$bodies[] = implode(' ',$words);

Snib

<><
 
Old August 23rd, 2004, 11:11 PM
Authorized User
 
Join Date: Jun 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you,
It works.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying query from three tables slims07 .NET Framework 2.0 8 April 2nd, 2008 10:47 AM
Displaying a completed message after the query has sibo32000 Excel VBA 1 February 19th, 2007 11:13 PM
simple query for displaying groups... suraj SQL Language 1 September 2nd, 2005 06:47 AM
Displaying the content of a query nav PHP How-To 2 March 8th, 2005 12:49 PM
data from query not displaying -Dman100- Classic ASP Professional 8 July 19th, 2004 02:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.