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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • khalidbaloch
    New Member
    • Oct 2006
    • 61

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

    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 ..
    Last edited by khalidbaloch; Oct 6 '06, 01:06 PM. Reason: Misspelling
  • bevort
    New Member
    • Jul 2006
    • 53

    #2
    how about looping?

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

    Then loop back again

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      This should do it
      [PHP]echo "<table">;
      $i = 0;
      while ($rec = mysql_fetch_ass oc(....)) {
      $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:

      Comment

      • khalidbaloch
        New Member
        • Oct 2006
        • 61

        #4
        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

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          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:

          Comment

          • khalidbaloch
            New Member
            • Oct 2006
            • 61

            #6
            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("SE LECT * FROM category")
            or die("Select errro: " . mysql_error());
            echo "<table">;
            $i = 0;
            while ($row = mysql_fetch_ass oc($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]

            Comment

            • khalidbaloch
              New Member
              • Oct 2006
              • 61

              #7
              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.whe n 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

              Comment

              • khalidbaloch
                New Member
                • Oct 2006
                • 61

                #8
                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.whe n 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

                Code:
                <?php
                $conn = mysql_connect("localhost", "root", "mypass") 
                    or die("Cannot connect to server: " . mysql_error());
                mysql_select_db("faq") 
                    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>"; 
                ?>

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  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:

                  Comment

                  • khalidbaloch
                    New Member
                    • Oct 2006
                    • 61

                    #10
                    Thanx You Very Much this is Done .Thanks a lote dear friend

                    Comment

                    • khalidbaloch
                      New Member
                      • Oct 2006
                      • 61

                      #11
                      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.ifas tnet.com in order to know php better i i would use friends like you and comunuty like this .thanx alot for you sincer corporation ..

                      Comment

                      • darkdirk1
                        New Member
                        • Oct 2006
                        • 50

                        #12
                        Modulus!!!

                        Comment

                        Working...