Closed Thread
Results 1 to 6 of 6

Thread: How to Display each record on a row of a table

  1. #1
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Question How to Display each record on a row of a table

    Hi All,

    I have a mysql table with three fields:

    1) Category
    2) Title
    3) Sub Category
    4) Price

    Basically, I have a php page called categories.php with a table on it, and a URL field called cat

    So to get there, for example you would go via:

    categories.php?cat=Mixers
    categories.php?cat=Headphone
    categories.php?cat=Racks etc. etc....

    On this page I want to display all the records in the category URL cat. So if for example someone came to the page from ?cat=Mixers I want to display all the records in the table where Category is Mixers.

    So how would I actually do this?

    So far I have this code:

    Code:
          <table width="700" border="1">
            <tr>
              <td width="193">Title</td>
              <td width="397">Sub Category</td>
              <td width="88">Price</td>
            </tr>
            <tr>
              <td>
                    <?php 
    				$catresult=$_GET['cat'];
    				$result = mysql_query("SELECT * FROM tbprofo WHERE category='$catresult'");
    		
    					while($row = mysql_fetch_array($result))
    					  {
    					  echo $row['title'];
    					  }
           			?>
              </td>
              <td>
                    <?php 
    				$catresult=$_GET['cat'];
    				$result = mysql_query("SELECT * FROM tbprofo WHERE category='$catresult'");
    		
    					while($row = mysql_fetch_array($result))
    					  {
    					  echo $row['sub'];
    					  }
           			?>
              </td>
              <td>
                    <?php 
    				$catresult=$_GET['cat'];
    				$result = mysql_query("SELECT * FROM tbprofo WHERE category='$catresult'");
    		
    					while($row = mysql_fetch_array($result))
    					  {
    					  echo $row['price'];
    					  }
           			?>
              </td>
            </tr>
          </table>
    But this displays all the fields in each cell, but doesnt add rows.

    Anyone know how to add rows for each record?

    Thanks very much for all your help

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: How to Display each record on a row of a table

    The general logic you want to use is:
    Code:
    query(select * from table)
    <table>
    <thead>
      <td>title</td><td>subcat</td><td>price</td>
    </thead>
    while not at end of query
      <tr><td>$title</td><td>$subcat</td><td>$price</td></tr>
    end while
    </table>
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    dvelop is offline Newbie
    Join Date
    Mar 2009
    Posts
    12
    Rep Power
    0

    Re: How to Display each record on a row of a table

    If you're talking about storing variables in the uri, you can check them via the $_GET global.

    $_GET['cat'] == 'Mixers';
    etc..

    This is not your answer but hopefully it points you in the correct direction.

  5. #4
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: How to Display each record on a row of a table

    he already has that working Dvelop. The only thing wrong with his code was as WP suggest he needed to lay it out how he wants it with HTML. ^_^

  6. #5
    tradingjamie is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Re: How to Display each record on a row of a table

    Thanks guys.

    WingedPanther - you sorted me out there matey... here's the code which works:

    Code:
    <table width="700" border="1">
    	<tr>
    	  <td width="193">Title</td>
    	  <td width="397">Sub Category</td>
    	  <td width="88">Price</td>
    	</tr>
    	<?php 
    	$catresult=$_GET['cat'];
    	$result = mysql_query("SELECT * FROM tbprofo WHERE category='$catresult'");
    	while($row = mysql_fetch_array($result)){
    	?>
    	<tr>
    		<td><?=$row['title']?></td>
    		<td><?=$row['sub']?></td>
    		<td><?=$row['price']?></td>
    	</tr>
    	<?php
    	}
    	?>		
    </table>
    Once again - thanks very much... this forum is great

  7. #6
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: How to Display each record on a row of a table

    Glad we could help
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 11-01-2011, 04:01 PM
  2. use php to display record!!!
    By noprobz09 in forum PHP Development
    Replies: 0
    Last Post: 10-16-2010, 04:13 PM
  3. Replies: 9
    Last Post: 08-24-2010, 12:53 AM
  4. Display table View and Form View - SQL/PHP
    By christh in forum Database & Database Programming
    Replies: 4
    Last Post: 04-18-2010, 08:48 AM
  5. Replies: 7
    Last Post: 08-27-2008, 07:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts