Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Single MySQL column to PHP multi column 1

Status
Not open for further replies.

hoppy2

Programmer
Joined
Sep 14, 2003
Messages
8
Location
AU
Hello PHP experts

I have a table of about 2000 categories in a MySQL database in two columns
"id" and "Description"

I am using the code below to bring the list back into a browser for clients to view.

Problem with this code is it only gives me a single column list of the category on my php page..

I need my code to get the single column of 2000 items listed under "Description" in my
database and to display it as a multi column php page.

Any code change ideas to achieve this?

As always any assistance is greatly appreciated.

Best Regards Hoppy

<?php
//connect to database using the mysql_connect function()
$db = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;);

//Select the database
mysql_select_db(&quot;quotesplease3&quot; ,$db);

//Create a query string and SQL statement
$sqlquery = mysql_query(&quot;SELECT * FROM category ORDER BY Description&quot; ,$db);

//Build table
echo (&quot;<table border ='0'&quot;);

//fetch the next row from the query and store it in an array
while ($tablerows = mysql_fetch_row($sqlquery))
{

//Insert data from database into table
echo(&quot;<tr><td>$tablerows[1]</td></tr> &quot;);
}
//close the database
echo &quot;</table>&quot;;
?>

</html>
 
If I understand you correctly, you wish to make this single list span multiple table columns.

One way to do it is to maintain a counter of the number of columns you have output. When that counter gets to the number of columns you wish to output, start a new table row and reset the counter to 1.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Yes sleipnir214 I think you understand me but just in case because the English language isn't always terribly precise and is therefore open to misunderstandings.

When I get the results from MySQL database and display them in my .php page I get a single column of 2000 items.

I would like that one single column of items to be magically transformed into say five or six columns across my .php page for my clients to view.

Hope this clarifies things.

Thanks for your help.

Hoppy
 
What you are trying to accomplish would be much easier if it was okay for the descriptions to go left to right instead of down.

If that's okay, basically what you want to do is determine how many columns you want (we'll call that $numcol) and create a counter ($count).

- When $count = 0: <tr><td>$description</td>
- When $count = $numcol: <td>$description</td></tr> & reset the value of $count to 0.
- For all other values: <td>$description</td>

 
hello,

I have the same problem here.

I want to show my pictures from left to right, in 4 columns, under each picture will be the name of the pictures.

How can I manage that output? I'm not good in count. =(

Please help me.
 
The secret is inserting a pair of &quot;</tr><tr>&quot; tags at the appropriate time to start a new row.

As you output the HTML necessary to have the browser show the images, maintain a counter. When that counter gets to the number of columns, set the counter back to 1 and output &quot;</tr><tr>&quot; in your output stream.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top