Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Category table issue...
Old 06-25-2011, 11:49 AM Category table issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Hi,

I have attempted doing this myself, and having some issues (I expected)..

HTML Code:
<div align="center"><b>Categories / Subcategories</b>:
<p>
PHP Code:
<?php

$link 
mysql_connect("host""user""pass");
mysql_select_db("db"$link);

$result mysql_query("SELECT * from categories");

while(
$row mysql_fetch_array($result)) {

// Adjustable table -- <b>Problem is below</b>

$tr 1;
$td 4;

for(
$i 0$i $tr$i++) 
{
     echo 
'<table>';
     echo 
'<tr>';
     for(
$j 0$j $td$j++)
     {
          echo 
'<td align="center">';
          echo 
$row['category_name'] . '&nbsp;';
     }
     echo 
'</tr> </td> </table>';
}
}

?>
HTML Code:
</div>
Ok, I am not sure if the code is even on track to do what I am trying for, which is to display 4 columns (across the page) and 1 row (down the page) for now to test. Here's the url to see what I am currently getting:

http://reunitemysite.com/cats.php

I am getting the category displayed 4 times across the page, then a line break then the next category displayed 4 times, etc..etc.. until each category is displayed.

Help anyone?
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-25-2011, 12:29 PM Re: Category table issue...
andrei155's Avatar
CEO of BLD Hosting

Posts: 1,514
Name: Andrei
Location: Canada
Trades: 6
why are you looping if you're only doing it once
__________________
No Overselling Guarantee
Now Includes a Free Domain
BLD Hosting -
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
andrei155 is offline
Reply With Quote
View Public Profile Visit andrei155's homepage!
 
Old 06-25-2011, 12:33 PM Re: Category table issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
I am looping because I didn't know any other way to get the data onto the page. If you can help further, that would be fantastic. I should know more php, but because I learn as I go, I have lot's of issues. I can't afford the schooling, so I try to piece things together.

Thank you
Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 06-25-2011 at 12:34 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 06-25-2011, 12:40 PM Re: Category table issue...
andrei155's Avatar
CEO of BLD Hosting

Posts: 1,514
Name: Andrei
Location: Canada
Trades: 6
Nevermind. I was asking about the big loop and why it was in a loop. But I realized it's an adjustable table so it won't always loop once.

Okay so is your j variable in charge of the columns? Try putting

Code:
echo '<tr>';


and

Code:
echo  '</td></tr>'; 


inside the j loop.
__________________
No Overselling Guarantee
Now Includes a Free Domain
BLD Hosting -
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Last edited by andrei155; 06-25-2011 at 12:42 PM..
andrei155 is offline
Reply With Quote
View Public Profile Visit andrei155's homepage!
 
Old 06-25-2011, 12:55 PM Re: Category table issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by andrei155 View Post
Nevermind. I was asking about the big loop and why it was in a loop. But I realized it's an adjustable table so it won't always loop once.

Okay so is your j variable in charge of the columns? Try putting

Code:
echo '<tr>';


and

Code:
echo  '</td></tr>'; 


inside the j loop.
Tried that, and the result is still incorrect, have a look at the output here:
http://reunitemysite.com/cats.php

I am looking for this output:

HTML Code:
Shopping      Social Networking      Test Category      Another Category

Another Category      Another Category      Another Category      Another Category
But aligned correctly, as long as I am achieving multiple rows of categories and multiple columns as well.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 06-25-2011, 01:08 PM Re: Category table issue...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I dont get it, what are you trying to do? Do you want to place the categories as

Code:
Category 1 | Category 2 | Category 3 | Category 4
Category 5 | Category 6 | Category 7 | Category 8
Category 9 | and so on...
or will you always have just 4 categories?

I'm guessing the first one, in which case I would do something like this:
PHP Code:
echo '<table>';
$first_row true;
$i 0;
$td 4;
while(
$row mysql_fetch_array($result)) {
   if (
$i $td == 0) {
      if (
$first_row) {
         echo 
'<tr>';
         
$first_row false;
      } else {
         echo 
'</tr><tr>';
      }
   }
   echo 
'<td>' $row['category_name'] . '</td>';
}

echo 
'</tr></table>'
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-25-2011, 01:14 PM Re: Category table issue...
andrei155's Avatar
CEO of BLD Hosting

Posts: 1,514
Name: Andrei
Location: Canada
Trades: 6
Sorry,

You'll need to close your td cell tag within the loop to start a new cell the next time it passes through.

Just add

Code:
 echo '</td>';
in the j loop and remove it from the i loop.
__________________
No Overselling Guarantee
Now Includes a Free Domain
BLD Hosting -
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
andrei155 is offline
Reply With Quote
View Public Profile Visit andrei155's homepage!
 
Old 06-25-2011, 02:28 PM Re: Category table issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by andrei155 View Post
Sorry,

You'll need to close your td cell tag within the loop to start a new cell the next time it passes through.

Just add

Code:
 echo '</td>';
in the j loop and remove it from the i loop.

That did not work. Still having the same issue, 4 of each category displayed in each row.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 06-25-2011, 03:28 PM Re: Category table issue...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Did you try it my way?
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-26-2011, 09:53 AM Re: Category table issue...
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Hi Brian, good lad for tackling it yourself!

You don't want the for loop inside the while - that's telling the program to output four things for each and every row, which is why you're getting each category four times. Just one loop is fine.

Normally you would do it all in a while loop but using a table gives a complication - you'll need a colspan on the last td unless you have 4n records precisely (or put empty cells in). Life would be easier if you do this with CSS.

Consider what you need to do: start and end tr tags as necessary - so you keep a count (or use the mod operator) and output a "</tr>" after very four td's.

To handle the last cell, loop through all rows bar one, then process the final row on its own after the loop.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 06-26-2011, 02:11 PM Re: Category table issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by PaulW View Post
Hi Brian, good lad for tackling it yourself!

You don't want the for loop inside the while - that's telling the program to output four things for each and every row, which is why you're getting each category four times. Just one loop is fine.
In what code are you referring to about the for loop? ( Post #1 or Post #6 )

Quote:
Normally you would do it all in a while loop but using a table gives a complication - you'll need a colspan on the last td unless you have 4n records precisely (or put empty cells in). Life would be easier if you do this with CSS.
I will try this, but I may have an issue here, but will post back.

Quote:
Consider what you need to do: start and end tr tags as necessary - so you keep a count (or use the mod operator) and output a "</tr>" after very four td's.
I will try this as well.

Quote:
To handle the last cell, loop through all rows bar one, then process the final row on its own after the loop.
Ok, will try this

Thanks alot.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Category table issue...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 2.07493 seconds with 12 queries