By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

[SOLVED] how to close td tr after every three mysql record ?

khalidbaloch
P: 61
hi : all Friend i am a new member of this comunity as well in php myqsl
i want learn that how to dispaly three or two record from a mysql table
for example ..
mysql table category has 21 columns, and i wish to close html tag TD TR after ever three myqsl columns .the mysql record should be shown as bellow

record 1 record 2 record 3
record 4 record 5 record 6
record 7 record 8 record 9
record 10 record 11 record 12
record 13 record 14 record 15
record 16 record 17 record 18
record 19 record 20 record 21

i apologize for my poor english i dont know whether anyone of you will understand me what i meam to ask ..
Oct 6 '06 #1
Share this Question
Share on Google+
11 Replies


bevort
P: 53
how about looping?

echo("<tr><td>");
echo( record 1);
echo( record 1);
echo( record 1);
echo("<tr><td>");

Then loop back again
Oct 6 '06 #2

ronverdonk
Expert 2.5K+
P: 4,258
This should do it
[PHP]echo "<table">;
$i = 0;
while ($rec = mysql_fetch_assoc(....)) {
$i++;
switch $i {
case 1 : echo "<tr><td>" . $rec[$i] . "</td>";
break;
case 2 : echo "<td>" . $rec[$i] . "</td>";
break;
case 3 : echo "<td>" . $rec[$i] . "</td></tr>";
$i=0;
break;
}
}
if ($i < 3)
echo "</tr>";
echo "</table>";[/PHP]

Ronald :cool:
Oct 6 '06 #3

khalidbaloch
P: 61
thanx for your reply ronald but i did not uderstand . i am a newbhi in php mysql .please show me an example by using a mysql databse strurture

Thanks againg in advance,

i hope you will undestand what i mean to say
Oct 15 '06 #4

ronverdonk
Expert 2.5K+
P: 4,258
How can I do that since I don't know what you db structure looks like, or at least how your table row looks.

Ronald :cool:
Oct 15 '06 #5

khalidbaloch
P: 61
sorry i forgot to tell my my database structure here is my database structure..

i have a table which holds two column ,,

table | columns |
category | Id ,name |
Here is the code (do not forget to fill in your own values for host, user, psw and db!).
[php]
$conn = mysql_connect("host", "user", "psw")
or die("Cannot connect to server: " . mysql_error());
mysql_select_db("db")
or die("Cannot select database: " . mysql_error());
$res = mysql_query("SELECT * FROM category")
or die("Select errro: " . mysql_error());
echo "<table">;
$i = 0;
while ($row = mysql_fetch_assoc($res)) {
$i++;
switch $i {
case 1 : echo "<tr><td>{$row['Id']}</td><td>{$row['name']}</td>";
break;
case 2 : echo "<td>{$row['Id']}</td><td>{$row['name']}</td>";
break;
case 3 : echo "<td>{$row['Id']}</td><td>{$row['name']}</td></tr>";
$i=0;
break;
}
}
if ($i < 3)
echo "</tr>";
echo "</table>"; [/php]
Oct 16 '06 #6

khalidbaloch
P: 61
sorry to distrube you agian n again n again .but yet my problem is not solved.
i put the code given by you in ur last reply .and i changed the detabase variables accordingly.when i put these code in my php page and pointed my browser to to this.
i got this parse error .
Parse error: parse error, unexpected ';' in D:\wwwroot\faq\index.php on line 8..

however i was able to solve this error by my own ..
acctully the error was caused by misstype in line 8

echo "<table">;
and then i changed this to
echo "<table>";

after making changes . i retried to poined my browser to this page .
but even this time i was dissopointed . there was an other error on line 12 and this time .i could not solve the error ..
this was the error .
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in D:\wwwroot\faq\index.php on line 12

please resovle my this problem ...hope i will be get the solution in last you next reply .. may god Bless you
Oct 17 '06 #7

khalidbaloch
P: 61
sorry to distrube you agian n again n again .but yet my problem is not solved.
i put the code given by you in ur last reply .and i changed the detabase variables accordingly.when i put these code in my php page and pointed my browser to to this.
i got this parse error .
Parse error: parse error, unexpected ';' in D:\wwwroot\faq\index.php on line 8..

however i was able to solve this error by my own ..
acctully the error was caused by misstyping in line 8

echo "<table">;
and then i changed this to
echo "<table>";

after making changes . i retried and poined my browser to this page .
but even this time i was dissopointed . there was an other error on line 12..
and this time .i could not solve the error ..
this was the error .
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in D:\wwwroot\faq\index.php on line 12 ..

after making neccesory changes the code looked like Bellow

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $conn = mysql_connect("localhost", "root", "mypass") 
  3.     or die("Cannot connect to server: " . mysql_error());
  4. mysql_select_db("faq") 
  5.     or die("Cannot select database: " . mysql_error());
  6. $res = mysql_query("SELECT * FROM category") 
  7.     or die("Select errro: " . mysql_error());
  8. echo "<table>";
  9. $i = 0;
  10. while ($row = mysql_fetch_assoc($res)) {
  11.   $i++;
  12.   switch $i {
  13.     case 1 : echo "<tr><td>{$row['Id']}</td><td>{$row['name']}</td>";
  14.              break;
  15.     case 2 : echo "<td>{$row['Id']}</td><td>{$row['name']}</td>";
  16.              break;
  17.     case 3 : echo "<td>{$row['Id']}</td><td>{$row['name']}</td></tr>";
  18.              $i=0;
  19.              break;
  20.   }
  21. }
  22. if ($i < 3) 
  23.    echo "</tr>";
  24. echo "</table>"; 
  25. ?>
Oct 18 '06 #8

ronverdonk
Expert 2.5K+
P: 4,258
I expect you to know SOME PHP, how else can you make an apoplication like this?!!!
I have not tested it and there are , of course, some typing errors in it!
The error is in the switch statement, which should be (of course)
[PHP]switch ( $i ) {
[/PHP]

If you have problems with code like this, it is time you took a tutorial.

Ronald :cool:
Oct 18 '06 #9

khalidbaloch
P: 61
Thanx You Very Much this is Done .Thanks a lote dear friend
Oct 18 '06 #10

khalidbaloch
P: 61
trhanx alots ronald this done ,let me tell you that i have almost completed my first php application .its a faq system ,, when it is ready i will launch my first script in this site phpbuilder.ifastnet.com in order to know php better i i would use friends like you and comunuty like this .thanx alot for you sincer corporation ..
Oct 18 '06 #11

P: 50
Modulus!!!
Oct 18 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.