Connecting Tech Pros Worldwide Forums | Help | Site Map

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

khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#1: Oct 6 '06
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 ..

bevort's Avatar
Member
 
Join Date: Jul 2006
Posts: 53
#2: Oct 6 '06

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


how about looping?

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

Then loop back again
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#3: Oct 6 '06

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


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:
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#4: Oct 15 '06

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


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
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#5: Oct 15 '06

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


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:
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#6: Oct 16 '06

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


Quote:
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]
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#7: Oct 17 '06

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


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
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#8: Oct 18 '06

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


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. ?>
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#9: Oct 18 '06

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


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:
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#10: Oct 18 '06

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


Thanx You Very Much this is Done .Thanks a lote dear friend
khalidbaloch's Avatar
Member
 
Join Date: Oct 2006
Location: karachi,pakistan
Posts: 61
#11: Oct 18 '06

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


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 ..
Member
 
Join Date: Oct 2006
Posts: 50
#12: Oct 18 '06

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


Modulus!!!
Closed Thread