[CLOSED] Database results in columns

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

    #1

    [CLOSED] Database results in columns

    I'm a newbie (less than a week of php) and I'm curious about how results can lay out. Is it possible to have the results layed out in 4 columns?

    Usual

    RECORD 1 GOES HERE
    RECORD 2 GOES HERE
    RECORD 3 GOES HERE

    I need

    RECORD 1 RECORD 2 RECORD 3 RECORD 4
    RECORD 5 RECORD 6 ETC.

    If this is possible, please don't forget that I'm REALLY new at this. :)

    THANKS!!!
  • ldndude
    New Member
    • Oct 2006
    • 5

    #2
    Originally posted by TheBATManPhln
    I'm a newbie (less than a week of php) and I'm curious about how results can lay out. Is it possible to have the results layed out in 4 columns?

    Usual

    RECORD 1 GOES HERE
    RECORD 2 GOES HERE
    RECORD 3 GOES HERE

    I need

    RECORD 1 RECORD 2 RECORD 3 RECORD 4
    RECORD 5 RECORD 6 ETC.

    If this is possible, please don't forget that I'm REALLY new at this. :)

    THANKS!!!
    The answer is yes. It is possible. I am also a noob so hopefully someone might have a better/cleaner solution than I do. The only reason I am replying is because I *just* managed to accomplish the same effect that you are describing. How I did it may not be the best way available but my knowledge is limited so I work with what I have.

    Here's what I did

    I started off with setting up my query and getting the results and the numrows

    then..when it came to echoing the results
    I declared two variables: the standard $i and $columncountSTO RE YOUR CODE BETWEEN code or PHP TAGS!!!!!

    Code:
    $i = 0;
    $columncount = 0:
    
    //I started my table
    echo "<table><tr>";
    
    //I created a while loop for the recordsets
    
    while ($i < $num) {
    $field=mysql_result($result,$i,"field");
    
    echo "<td>".$field."</td>";
    
    //inside the loop, I check the column counter to see if I reached the 3rd column
    
    if ($columncount = 2) {
    //close that row and open a new one
    echo "</tr><tr>";
    }
    //increment my counts by 1
    $i++;
    $columncount++;
    }
    //then close my table
    echo "</tr></table>";
    STORE YOUR CODE BETWEEN code or PHP TAGS!!!!!
    Like I said before, there may be better ways to do this so keep looking for an answer, this just happened to be the only one I could think of under my time constraints with my knowledge
    Hope it helps.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      See my reply to this forum's thread http://www.thescripts.com/forum/thread545705.html
      and the code posted there is:
      [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

      • TheBATManPhln
        New Member
        • Oct 2006
        • 8

        #4
        Being a novice maybe I'm screwing this up but neither one work.


        echo "<table>";

        $i = 0;

        while ($rec = mysql_fetch_ass oc($result)) {

        $i++;
        // THE NEXT LINE IS LINE 27
        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>";

        }
        ?>


        Gives me this error

        Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/dir/public_html/phundays/testphp/column_test.php on line 27


        As for the other code



        $i = 0; // THE NEXT LINE IS LINE 19
        $columncount = 0:

        //I started my table
        echo "<table><tr >";

        //I created a while loop for the recordsets

        while ($i < $num) {
        $field=mysql_re sult($result,$i ,"carid");

        echo "<td>".$field." </td>";

        //inside the loop, I check the column counter to see if I reached the 3rd column

        if ($columncount = 4) {
        //close that row and open a new one
        echo "</tr><tr>";
        }
        //increment my counts by 1
        $i++;
        $columncount++;
        }
        //then close my table
        echo "</tr></table>";



        Parse error: parse error, unexpected ':' in /home/dir/public_html/phundays/testphp/columns_nov.php on line 19

        :(

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          You indeed are screwing it up. The code I sent you worked, but why did you add the right brace at the end?? [php]
          if ($i < 3)
          echo "</tr>";
          echo "</table>";

          }
          [/php]

          Why don't you show us all the code you have used to test this solution (the one I showed here).

          Ronald :cool:

          Comment

          • TheBATManPhln
            New Member
            • Oct 2006
            • 8

            #6
            Here's the code. Less my db info

            <html>
            <head>
            <title>Column Test</title>
            </head>
            <body>


            <?php
            $db_host = "localhost" ;
            $db_user = "XXXXXX";
            $db_pwd = "YYYYYY";
            $db_name = "ZZZZZZZ";
            mysql_connect($ db_host, $db_user, $db_pwd);
            mysql_select_db ($db_name);

            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>";
            ?>

            </body>
            </html>
            Here's the error

            Parse error: parse error, unexpected '.', expecting ')' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 20

            This is line 20
            while ($rec = mysql_fetch_ass oc(....)) {

            I did fix the html tag from
            echo "<table">;

            to
            echo "<table>";

            Were you testing me?? LOL
            Anyway, I appreciate the help!!!!

            If I replace the (....) with my db's name (cars)

            I then get
            Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 24

            Line 24 is
            switch $i {

            Thanks

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              But where is your database query statement (mysql_query) executed? It is not in your code! It will never work without it!

              Ronald :cool:

              Comment

              • TheBATManPhln
                New Member
                • Oct 2006
                • 8

                #8
                I tried it with this

                mysql_connect($ db_host, $db_user, $db_pwd);
                mysql_select_db ($db_name);
                $query="SELECT * FROM cars";
                $result=mysql_q uery($query);

                Same results
                Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 26

                Line 26
                switch $i {

                As I said, I've only been playing with PHP for about a week.

                THANK YOU!!

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  My mistake, switch operand should be within parentheses. Anyway, the following code is tested by me and it works. Notice that I have included as comments at the beginning the way I set up the table 'cars'. Here is the working sample (don't forget to fill in your db data):
                  [php]<?php
                  /*
                  create table cars (id int primary key, field varchar(20));

                  insert into cars values(1, "type 1");
                  insert into cars values(2, "line 2");
                  insert into cars values(3, "row 3");
                  insert into cars values(4, "row 4");
                  insert into cars values(5, "and 5");
                  insert into cars values(6, "line 6");
                  insert into cars values(7, "last 7");
                  */
                  $db_host = "localhost" ;
                  $db_user = "xxxxxx";
                  $db_pwd = "yyyyy";
                  $db_name = "zzzzz";
                  mysql_connect($ db_host, $db_user, $db_pwd);
                  mysql_select_db ($db_name);
                  $query="SELECT * FROM cars";
                  $result=mysql_q uery($query);
                  echo "<table>";
                  $i = 0;
                  while ($rec = mysql_fetch_ass oc($result)) {
                  $value = $rec['field'];
                  $i++;
                  switch ($i) {
                  case 1 : echo "<tr><td>$value </td>";
                  break;
                  case 2 : echo "<td>$value </td>";
                  break;
                  case 3 : echo "<td>$value </td></tr>";
                  $i=0;
                  break;
                  } // End switch
                  } // End while
                  if ($i < 3)
                  echo "</tr>";
                  echo "</table>";
                  ?>[/php]

                  Good luck! Ronald :cool:

                  Comment

                  • TheBATManPhln
                    New Member
                    • Oct 2006
                    • 8

                    #10
                    BINGO!!

                    THANK YOU!!!

                    One more question. Can I have one field in one row and another in the next?

                    IMAGE 1 | IMAGE 2 | IMAGE 3
                    DESCRIPTION 1 | DESCRIPTION 2 | DESCRIPTION 3


                    IMAGE 4 | IMAGE 5 | IMAGE 6
                    DESCRIPTION 4 | DESCRIPTION 5 | DESCRIPTION 6

                    I can play with it but I thought I'd ask just to save some frustration in case it's not possible.

                    Thanks

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Yes, if your description field is in db column 'description'th en you could setup the while loop as follows:
                      [php]while ($rec = mysql_fetch_ass oc($result)) {
                      $value = $rec['field'];
                      $descr = $rec['description'];
                      $i++;
                      switch ($i) {
                      case 1 : echo "<tr><td>$value <br>$descr</td>";
                      break;
                      case 2 : echo "<td>$value<br> $descr</td>";
                      break;
                      case 3 : echo "<td>$value<br> $descr</td></tr>";
                      $i=0;
                      break;
                      } // End switch
                      } // End while
                      [/php]

                      Ronald :cool:

                      Comment

                      • TheBATManPhln
                        New Member
                        • Oct 2006
                        • 8

                        #12
                        WORKS GREAT!!!!

                        You are AWESOME!!!

                        Thanks again!
                        The B.A.T.Man

                        Comment

                        • TheBATManPhln
                          New Member
                          • Oct 2006
                          • 8

                          #13
                          Thought you might like to see the results. (TEMPORARY results anyway.) I still need to populate the db.



                          Thanks again,
                          The B.A.T.Man

                          Comment

                          • ronverdonk
                            Recognized Expert Specialist
                            • Jul 2006
                            • 4259

                            #14
                            Looks good to me!

                            Comment

                            Working...