Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch data from two table in cake php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.91k
    Comment on it

    Suppose we have a table name player having field id, name of player, images, etc having some data .Now we have another table name playergallery having field like id, player_id, images, etc here player_id is foreign key and it is referencing to table player id and we have to get data from both table so we use join for this query.
     
     In the following function we have to load database  player and playergallery.In the next line we are getting data from two table by the query.

    	public function playerimagegallery($id = null) {
    		$this->loadModel('Player');
    		$this->loadModel('playergallerys');
            if (!$id) {
                throw new NotFoundException(__('Invalid post'));
            }
            $data = $this->Player->findById($id);
            if (!$data) {
                throw new NotFoundException(__('Invalid post'));
    					}
    		$this->Player->bindModel(array('hasMany'=>array('Playergallerys'=>array('foreignKey'=>'playerid'))));
    		
    		
    		$this->set('gall',$this->Player->find('first'));
    		$this->set('data', $data);	
    		$this->set('id',$id);
        }

    HTML:

    <section>
    <div class="container" >
    	<div class="col-md-8" style="border:1px ;">
    		<div class="col-md-12 col-md-offset-0" style="margin-top:40px;margin-bottom:10px;" >
    			<div style="float:left;">
    			<?php echo $this->Html->image('player/'.$data['Player']['profile_pic'],array('class'=>'img-responsive','style'=>'height:220px;width:231px;border:4px solid silver;'));?>
    			</div>
    			<div style="float:left;width:64%;border:1px solid silver">
    				<table class="table table table-striped">
    				<tr> 
    				<th>Player-name:<?php echo $data['Player']['player_name'];  ?></th>
    				</tr>
    				<tr>
    				<td>Date Of Birth:<?php echo $data['Player']['dob'];  ?></td></tr>
    				<tr>
    				<td>Country:<?php echo $data['Player']['country'];  ?></td>
    				</tr>
    				<tr>
    				<td>Batting Hand:<?php echo $data['Player']['batting'];  ?></td></tr>
    				<tr>
    				<td>Bolwing Arm:<?php echo $data['Player']['bowling'];  ?></td>
    				</tr>
    				</table>
    			</div>
    			
    		</div>
    
    
    
    			<div class="col-md-12 col-md-offset-0" style="background:#222;text-align:center;margin-bottom:10px;color:#fff;border-radius:5px;;">
    			<span style="font-size:30px;"><?php echo $data['Player']['player_name']?> Gallery</span>
    		
    		<div class="col-md-12 col-md-offset-0">
    		<?php echo $this->Html->Link('Add Pictures',array('controller'=>'admin','action'=>'playergallery/'.$id),array('class'=>'add_pic_button','style'=>"text-decoration:none;"))?>
    		</div>
    		
    		<table class="table table-striped">
    			<tr>
    				<th>Image</th>
    				<th>Action</th>
    			</tr>
    
    		<div class="col-md-3 col-md-offset-3" style="margin-bottom:20px;" >
    		
    			        <?php foreach($data['Playergallerys'] as $data) { ?>
    				<tr>
    				<td><?php echo $this->Html->image('playergallery/'.$data['image'],array('class'=>'img-responsive','style'=>'height:120px;width:160px;'));?></td>
    					   <td> <?php echo $this->Form->postLink('Delete',array('action' => 'playerimage_del', $data['id'],$data['image'],$data['playerid']),array('confirm' => 'Are you sure?'),array('class' => 'del_msg')	
                    );
                ?>
    			<?php echo $this->Html->link('Edit',array('action'=>'playergalleryedit',$data['id'],$data['playerid']));?>
    			</td>
    			</tr>
    			</div>
    			<?php } ?>
    	</div>
    	</table>
    </div>
    </div>
    
    </section>
    
    
    

    This is how you can fetch data from multiple tables.

 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: