SIGN UP MEMBER LOGIN:    
ARTICLE

Display Related Records in PHP

Posted by Satyapriya Nayak Articles | PHP December 09, 2011
In this article we will learn how to display related records from the database when we click a show all link using PHP.
Reader Level:
Download Files:
 

Here we produce two PHP pages, in first (showDetails.php) it contains some data with a show all link. When the user clicks on that link all related data of that link will be displayed in the second page (showdetails1.php).
 
Table creation
 
 
Server: localhost   Database: home_work
 -- phpMyAdmin SQL Dump
 -- version 2.10.1
 -- http://www.phpmyadmin.net
 --
 -- Host: localhost
 -- Generation Time: Nov 17, 2011 at 04:25 PM
 -- Server version: 5.0.45
 -- PHP Version: 5.2.5
  
 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  
 --
 -- Database: `home_work`
 --
 -- --------------------------------------------------------
 --
 -- Table structure for table `employee`
 --
  
 CREATE TABLE `employee` (
   `id` int(11) NOT NULL auto_increment,
   `empid` varchar(255) NOT NULL,
   `empname` varchar(255) NOT NULL,
   `empaddress` varchar(255) NOT NULL,
   `designation` varchar(255) NOT NULL,
   `salary` int(11) NOT NULL,
   PRIMARY KEY  (`id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
  
 --
 -- Dumping data for table `employee`
 --
  
 INSERT INTO `employee` (`id`, `empid`, `empname`, `empaddress`, `designation`, `salary`) VALUES
 (1, 'E001', 'Raj', 'Delhi', 'Clerk', 10000),
 (2, 'E002', 'Harry', 'Mumbai', 'Player', 20000),
 (3, 'E003', 'Sunil', 'Noida', 'Producer', 30000),
 (4, 'E004', 'Pollock', 'Durban', 'Manager', 40000),
 (5, 'E005', 'Jonty', 'Perth', 'Director', 50000),
 (6, 'E006', 'Kallis', 'Oslo', 'Dean', 60000),
 (7, 'E007', 'Imran', 'Dubai', 'Admin', 70000);
  
 
showDetails.php
 
 
 <?php
 $link=mysql_connect("localhost","root","");
 mysql_select_db("home_work",$link);
 $sql="SELECT * FROM employee ";
 $aResult=mysql_query($sql);
  
 ?>
 <html>
 <head>
 </head>
 <body bgcolor="Yellow">
 <form name="frmForm" id="frmForm" >
 <table border="1" width="30%" height="30%">
 <tr>
 <th><font color='Red'>EMP ID</font></th>
 <th><font color='Red'>EMP NAME</font></th>
 <th><font color='Red'>SALARY</font></th>
 <th><font color='Red'>SHOW ALL</font></th>
 </tr>
  
 <?php 
 while($rows = mysql_fetch_array($aResult,MYSQL_ASSOC))
 {
 $id                    = $rows['id'];
 $empid             = $rows['empid'];
 $empname        = $rows['empname'];
 $empaddress    = $rows['empaddress'];
 $designation     = $rows['designation'];
 $salary              = $rows['salary'];
  
 ?>
 <tr> 
 <td><b><font color='#663300'><?php echo $empid;?></font></b></td>
 <td><b><font color='#663300'><?php echo $empname;?></font></b></td>
 <td><b><font color='#663300'><?php echo $salary;?></font></b></td>
 <td><b><font color='#663300'><a href="showDetails1.php?frm_action=3&cust_id=<?php echo $id;?>" target="_blank">Show
 All</a></font></b></td>
 </td>
 </tr> 
 <?php } ?>
  
 <?php if($_REQUEST['frm_action'] == 3) { ?>
 <tr>
 <td colspan="2">
 <table  style="border:1px solid #003366;" cellspacing="2" cellpadding="2" width="100%" bgcolor="#003366">
 <tr bgcolor="#EFEFEF">
 <td>Empid</td>
 </tr>
 <?php
 while($row1= mysql_fetch_array($aCustomer,MYSQL_ASSOC))
 {
 $empid=$row1['empid']; 
 ?>
  
 <tr bgcolor="#FFFFFF">
 <td><?php echo $empid;?></td>
 </tr>
 <?php } ?>
 </table>
 </td>
 </tr>
 <?php } ?>
  
 </table>
 </form>
 </body>
 </html> 
  
 
showdetails1.php
 
 
 <?php
 $link = mysql_connect("localhost","root","");
 mysql_select_db("home_work",$link);
 if($_REQUEST['frm_action'] == 3)
 {
 $id          = $_REQUEST['cust_id'];
 $sqlCustomer = "SELECT * FROM employee WHERE id='$id'";
 $aCustomer   = mysql_query($sqlCustomer);
 }
 ?>
 <html>
 <head>
 </head>
 <body bgcolor="Yellow">
 <form name="frmForm" id="frmForm" >
 <table border="1" width="30%" height="30%">
 <tr>
 <th><font color='Red'>ID</font></th>
 <th><font color='Red'>NAME</font></th>
 <th><font color='Red'>ADDRESS</font></th>
 <th><font color='Red'>DESIGNATION</font></th>
 <th><font color='Red'>SALARY</font></th>
 </tr>
 <?php if($_REQUEST['frm_action'] == 3) { ?>
  
 <?php
 while($row1 = mysql_fetch_array($aCustomer,MYSQL_ASSOC))
 {
  
 $empid             = $row1['empid'];
 $empname        = $row1['empname'];
 $empaddress  = $row1['empaddress'];
 $designation = $row1['designation'];
 $salary      = $row1['salary'];
 ?>
  
 <tr>
 <td><b><font color='#663300'><?php echo $empid;?></font></b></td>
 <td><b><font color='#663300'><?php echo $empname;?></font></b></td>
 <td><b><font color='#663300'><?php echo $empaddress;?></font></b></td>
 <td><b><font color='#663300'><?php echo $designation;?></font></b></td>
 <td><b><font color='#663300'><?php echo $salary;?></font></b></td>
 </tr>
 </table>
 <?php } ?>
 <?php } ?>
 </form>
 </body>
 </html>
 

Running the application
 
Run the WampServer then write the below line in the Url
 
http://localhost/Display Related records/

php.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks

Posted by Satyapriya Nayak Dec 10, 2011

Very good article for PHP beginner.........

Posted by Vineet Kumar Saini Dec 10, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor