How do I display SQL query results in a repeating table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DigitalWallfare
    New Member
    • Nov 2009
    • 2

    How do I display SQL query results in a repeating table?

    Hi all,

    This is my first post here, but i've lurked for a while.

    I'm working on a website but have come across a major stumbling block in the code:

    I've managed to structure the search query but have 2 problems:

    I dont know how to code the results into the table, and make the table repeat itself 5 times per page (and recognise it needs a new page)

    And I cant test the search query because i cant get the results to display.

    If any one could answer 1 or more of these problems and/or correct any error's i've made I'd jump for joy, as i feel i've been fumbling in the dark for the last week or so.

    Here's my code so far:
    Code:
    $connection = mysql_connect($hostname, $username, $password);
        if (!$connection) {
           die("A connection to the server could not be established");
        }
        //remember to select the database!
        mysql_select_db($database) or die("Database could not be selected!");
    
        //get the values from the form
        $MerchantType = $_POST["MerchantName"];
        $County = $_POST["County"];
    	
    	// rows to return
    $limit=10; 
    
    
    // Build SQL Query  
    $query = "SELECT * FROM Testtable where County = '$county'  
      ORDER BY MerchantType"; // EDIT HERE and specify your table and field names for the SQL query
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    ?>
    	
    <?php
    echo "
    <html><body><table>
    <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFFF" width="800" cellpadding="3" cellspacing="3">
    <tr>
    <td width="200" align="left" rowspan=5>Image</td>
    </tr>
    <tr>
    <td width="573" align="left">Name:</td>
    </tr>
    <tr>
    <td height="27" align="left">Telephone:</td>
    </tr>
    <tr>
    <td height="27" align="left">Website:</td>
    </tr>
    <tr>
    <td height="54" align="left">Description:</td>
    </tr>
    </table>
    
    </html></body></table>;
    ?>
    Thanks,
    Sam
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Why don't you first have a look on the table pagination tutorials here.

    Comment

    • Alien
      New Member
      • Sep 2007
      • 61

      #3
      Why not do a while statement with mysql_fetch_arr ay( ) ?

      Code:
      $counter = 1;
      $maxRows = 5;
      $query1 = "# SELECT * FROM Testtable where County = '$county'  
      #   ORDER BY MerchantType";
      <table>
      while($counter < $maxRows)
      {
          $row_details = mysql_fetch_array($query1)
          
      // each $row_details will have a row from the table, just output it onto a table.
           // $row_details['col1'] will output the which you need to put in <td></td>. 
      }	
      </table>
      So you get first 5 rows this way. Do a bit of more programmiing using sessions to alter value of $counter and $maxRows to get your remaining tables.

      Comment

      • DigitalWallfare
        New Member
        • Nov 2009
        • 2

        #4
        Thank you both so, so much for your help. I apologise for asking such a basic question.

        I went to bed dreading this morning and another day of coding, now i'm able hit it head on.

        Thanks again,

        Sam

        Comment

        Working...