1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to Display each record on a row of a table

Discussion in 'PHP & Perl' started by back2black, Jul 18, 2009.

Tags:
  1. back2black

    back2black Junior Member

    Joined:
    Dec 20, 2008
    Messages:
    115
    Likes Received:
    28
    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. drdankmendez

    drdankmendez Junior Member

    Joined:
    May 30, 2008
    Messages:
    194
    Likes Received:
    316
    Location:
    In front of my computer
    If i understand you correctly you are not asking about the actual retrevial of data from the database, but instead the display of the data once you have made the query.

    What it sounds like you want to happen is for each result from the database to be given its own line in the display like :

    ItemTitle1 ItemSubCat1 ItemPrice1
    ItemTitle2 ItemSubCat2 ItemPrice2
    ItemTitle3 ItemSubCat3 ItemPrice3

    If this is what you are looking for try the following 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>
    	<?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>
     
    • Thanks Thanks x 1
  3. back2black

    back2black Junior Member

    Joined:
    Dec 20, 2008
    Messages:
    115
    Likes Received:
    28
    thanks drdankmendez - code works great