Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetching data from databse in cakePHP.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 247
    Comment on it

    Fetching a data in the form of table from the database can is explained here in this blog, User have to define a function in the controller with the same file name in which user are displaying table using HTML code, for example here it is 'fetch' so user need to used find('all') method in the controller for accessing all details from the database table. It is often convenient to load all details of the user from table. User can set more than one variable in controller by using only $this->set method. In this example we use $table for accessing values from the table in tabular form, below are some line of code that will help user to display all fields from the database

    PHP code:

    
    <?php?>
    App::uses('Controller', 'Controller'); 
    function fetch()
    	  {
    	  	$result = $this->User->find('all');
    	  	$this->set("table",$result);
    	  }
    ?>


    HTML code:

    
    <table>
    	<tr>
    		<th>Name</th>
    		<th>email</th>
    		<th>Gender</th>
    		<th>education</th>
    		<th>Update</th>
    		<th>Delete</th>
    	</tr>
    	<?php foreach ($table as $row) {?>
    		<tr>
    			<td><?php echo $row['User']['username'] ?></td>
    			<td><?php echo $row['User']['email'] ?></td>
    			<td><?php echo $row['User']['gender'] ?></td>
    			<td><?php echo $row['User']['education'] ?></td>
    			<td><button type="button">edit</button></td>
    			<td><button type="button">delete</button></td>
    		</tr>
    
    	<?php	}
    	?>
    </table>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: