Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.76 KB | None | 0 0
  1. <?php
  2. class PictureModel extends CI_Model {
  3.    
  4.     //stdClass Object ( [user_id] => 0 [comment] => Canceriuo [created] => 1447321029 )
  5.     function getComments($id){
  6.                 $this->db->select("user_id,comment,created");
  7.                 $this->db->from('comments');
  8.                 $this->db->where('picture_id', $id);
  9.                 $query = $this->db->get();
  10.                 $result = $query->result();
  11.                 $comments = array();
  12.                 //print_r($result[2]->user_id);
  13.                 //print_r($result[1]->user_id."on tühi?");
  14.                 //print_r(count($result));
  15.  
  16.                 for($i = 0; $i < count($result); ++$i){
  17.                     if($result[$i]->user_id == ''){
  18.                         $username = 'anonüümne';
  19.                     } else {
  20.                         $this->db->select('username');
  21.                         $this->db->from('users');
  22.                         $this->db->where('id', $result[$i]->user_id);
  23.                         $query = $this->db->get()->result();
  24.                         $username = $query[0]->username;
  25.                     }
  26.                 array_push($comments, array($username, $result[$i]->comment,$result[$i]->created));
  27.                 //print_r($query->result()[0]->comment);
  28.                 }
  29.                 return $comments;
  30.  }
  31.     function enterComment($id, $comment){
  32.  
  33.                 if (! $comment == ''){
  34.                     //id, picture_id, user_id, comment
  35.                     $data = array(
  36.                                'user_id' => $this->ion_auth->get_user_id(),
  37.                                'comment' => $comment,
  38.                                'picture_id' => $id,
  39.                                'created' => time()
  40.                 );
  41.      
  42.                             $this->db->insert('comments', $data);
  43.             }
  44.             }
  45.  
  46.     function getPicture($id){
  47.         $this->db->select("id,title,description,location,comments_enabled");
  48.         $this->db->from('pictures');
  49.         $this->db->where('id', $id);
  50.         $query = $this->db->get();
  51.         return $query->result()[0];
  52.  }
  53.     function getTags($id){
  54.         $tags = array();
  55.        
  56.        
  57.         for($i = 0; $i < $this->db->get_where('pictures_tags', array('picture_id' => $id))-> num_rows(); ++$i){
  58.             array_push($tags, ($this->db->get_where('tags', array('id' => $this->db->get_where('pictures_tags',
  59.             array('picture_id' => $id))->result()[$i]->tag_id))->result()[0]->tag));
  60.         }
  61.        
  62.         return $tags;
  63.         }
  64. }
  65.  
  66. ?>
  67.  
  68. /// TEINE FAIL
  69.  
  70.  
  71. <?php
  72. defined('BASEPATH') OR exit('No direct script access allowed');
  73.  
  74. class Picture extends CI_Controller {
  75.     function __Construct(){
  76.     parent::__Construct ();
  77.        $this->load->model('PictureModel'); // load model
  78.        $this->load->helper('form');
  79.      }
  80.  
  81.     public function photo() {
  82.         $this->data['comments'] = $this->PictureModel->getComments($this->uri->segment(2, 1));
  83.         $this->data['picture'] = $this->PictureModel->getPicture($this->uri->segment(2, 1));
  84.         $this->data['tags'] = $this->PictureModel->getTags($this->uri->segment(2, 1));
  85.         $this->load->view('templates/header');
  86.         $this->load->view('picture', $this->data);
  87.         $this->load->view('templates/footer');
  88.     }
  89.  
  90.     public function comment($id){
  91.                 $this->PictureModel->enterComment($id, $this->input->post('comment'));
  92.                 redirect('/picture/'.$id);
  93.         }
  94.  
  95. }
  96. ?>
  97.  
  98.  
  99. /// KOLMAS FAIL
  100.  
  101. <div class="col-md-2 col-sm-4"></div>
  102. <div class="container">
  103.     <div class="panel panel-default">
  104.         <div class="panel-heading">
  105.             <?php echo "<h3>".$picture->title."</h3><br>";?>
  106.             <a class="btn btn-info" href="<?php echo site_url("edit/".$picture->id);?>">Muuda Andmeid</a>
  107.             <a class="btn btn-warning" href="<?php echo $picture->location;?>">Täissuuruses</a>
  108.         </div>
  109.         <div class="panel-body">
  110.                 <div class="col-lg-6 col-md-6 col-xs-12"><?php echo $picture->description;?></div>
  111.                     <div class="row">
  112.                         <div class="col-lg-6 col-md-6 col-xs-12">
  113.                             <br>
  114.                             <?php echo '<img src="'.$picture->location.'" class="img-responsive">';?>
  115.                         </div>
  116.                     </div>
  117.         </div>
  118.         <div class="panel-footer"><?php echo implode(', ',$tags);?><br></div>
  119.     </div>
  120.     <?php if ($picture->comments_enabled == 't'){?>
  121.     <div class="panel-group">
  122.         <div class="panel panel-default">
  123.             <div class="panel-heading">
  124.                 <h4 class="panel-title">
  125.                     <a data-toggle="collapse" href="#collapse1">Kommentaarid<?php echo '('.count($comments).')'; ?></a>
  126.                 </h4>
  127.             </div>
  128.             <div id="collapse1" class="panel-collapse collapse">
  129.                 <ul class="list-group">
  130.                     <li class="list-group-item">
  131.                         <?php for($i = 0; $i < count($comments); ++$i){?>
  132.                             <div class="panel panel-default">
  133.                                 <div class="panel-heading">
  134.                                 <h3 class="panel-title">
  135.                                 <?php if ($comments[$i][0] == 'anonüümne'){?>
  136.                                     <?php echo $comments[$i][0];?></h3>
  137.                                 <?php } else { ?>
  138.                                     <a href="<?php echo site_url('/profile/'.urlencode($comments[$i][0]));?>">
  139.                                     <?php echo $comments[$i][0];?></a></h3>
  140.                             <?php }?>
  141.                                 </div>
  142.                                 <div class="panel-body" style="text:word-wrap"><?php echo $comments[$i][1]; ?></div>
  143.                                 <div class="panel-footer"><?php echo date("H:i:s d.m.Y", $comments[$i][2]); ?></div>
  144.                             </div> 
  145.                             <?php }?>
  146.                     <?php } else { ?>
  147.                         <div class="panel-group">
  148.                             <div class="panel panel-default">
  149.                                 <div class="panel-heading">
  150.                                     <h4 class="panel-title">
  151.                                         Kommenteerimine on selle pildi jaoks väljalülitatud.
  152.                                     </h4>
  153.                         </div>                 
  154.                         <?php }?>
  155.                     </li>
  156.                 </ul>
  157.             </div>
  158.         </div>
  159.     </div>
  160.     <?php if($this->ion_auth->logged_in() && $picture->comments_enabled == 't') {?>
  161.         <?php echo form_open('picture/comment/'.$this->uri->segment(2, 1));?>
  162.         <div class="form-group">
  163.             <div class="col-lg-5 col-cs-offset-5">
  164.                 <form>
  165.                     <label for="comment">Kommenteeri:</label>
  166.                     <textarea class="form-control" type="text" rows="5" id="comment" name="comment"></textarea>
  167.                     <br>
  168.                     <input type="submit" class="btn btn-primary" value="kommenteeri">
  169.                 </form>
  170.             </div>
  171.         </div>
  172.     <?php } ?>
  173. </div>
  174. <div class="col-md-2 col-sm-4"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement