I am having a blog page where the blogs are inserted from admin panel and blog title will be inserted into new column as separated by " - " if there are spaces between the page titles.For example if the page title is " welcome to something " then in the database it is will be inserted into two columns. In once column it will be inserted as same and in other column it will be inserted as welcome-to-something.
when clicking on readmore button i need to display in url as (www.example.com/blob/article/welcome-to-something) in this format i need to display the url.
Here is the code:
Controller:
public function index()
{
$this->load->model('blogs_model');
$data["records2"] = $this->blogs_model->get_all_blogs($config["per_page"], $page);
$data['mainpage'] = "blog";
$this->load->view('templates/template',$data);
}
public function article()
{
$this->load->model('blogs_model');
$data['records2']= $this->blogs_model->getblogsdata($this->uri->segment(3));
$data['mainpage']='blogs';
$this->load->view('templates/templatess',$data);
}
Model:
function get_all_blogs()
{
$this->db->select('B.*');
$this->db->from('blogs AS B');
$this->db->where(array('B.status'=>1));
$this->db->order_by("position", "asc");
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function getblogsdata($id)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blog_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div class="col-md-9 blogs">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="blog1">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" class="testimonials1"/>
<h3 class="heading1"><?php echo $r->blog_title;?></h3>
<div class="blogtext1 read">
<?php echo $r->description;?>
</div>
<a href="<?php echo base_url()?>blog/article/<?php echo $r ->blog_id ;?>" class="read_more7" target="_blank" >Read More</a>
</div>
<?php endforeach ;endif;?>
<div class="pagination"><?php echo $links; ?></div>
</div>
blogs table
blog_id | blog_title | blogtitle
1 welcome to something welcome-to-something
Model:
function getblogsdata($id,$slug)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blogtitle'=>$id));
$this->db->where(array('blogs.blogtitle' => $slug));
$this->db->order_by("ne_views", "asc");
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<a href="<?php echo base_url()?>blog/article/<?php echo $r->blogtitle;?>" class="read_more7" target="_blank" >Read More</a>
Related
So I currently have three tables like this given below:
AI = auto increment.
ci_users: user_id(AI), username, slug, email, biography.
ci_terms: term_id(AI), title, slug, body.
ci_relationship: id(AI), term_id, user_id, type.
I'm trying to display all the users from a specific term_id(where type is skill),
for example, let's say term_id 3(where term_id has the title of 'CSS' from ci_terms).
To make it more clear,the above paragraph basically says show me all users with skill of CSS but I want the displayed users to show all of their other skills which are stored in the ci_relationship table.
PARAGRAPHS BELOW ARE AFTER CLICKING A SPECIFIC TERM_ID(SKILL).
This is the function which shows me all of the users that have the term_id as their skill.(from my previous Stackoverflow question. Thanks to pradeep who helped me solve that query:
// All users with specific skill
public function get_user_skill($id)
{
$this->db->select('*, count(ci_relationship.term_id)');
$this->db->from($this->table);
$this->db->join('ci_relationship', 'ci_relationship.user_id = ci_users.id', 'INNER');
$this->db->order_by('ci_relationship.user_id', 'DESC');
$this->db->group_by('ci_relationship.user_id');
$this->db->where('ci_relationship.term_id', $id);
$this->db->where('ci_relationship.type', 'skill');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
As I said before the code above just display the users with the specific term_id. Now this is the method in the controller which shows the expected data(users with specific term_id):
This is my controller :
public function skill($id){
// Get data
$data['users'] = $this->User_model->get_user_skill($id);
$term = $this->Terms_model->get($id);
// Get skill name
$data['skill'] = $this->Terms_model->get($id);
// Get skills per foreach --- HERE IS WHERE I NEED HELP
$data['skills'] = $this->User_model->skills($id);
// Meta
$data['title'] = $this->settings->title.' | '. ucfirst($term->title);
// Load template
$this->template->load('public', 'default', 'users/skill', $data);
}
This is the method that I'm trying to create to displayed the term_id per user_id:
// Skill for user foreach
public function skills($id){
$this->db->select('*');
$this->db->from($this->relationship);
$this->db->where('term_id', $id);
$this->db->where('type', 'skill');
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
if($query->num_rows() >= 1){
return $query->result();
} else {
return false;
}
}
The view is the following code:
<?php if($users) : ?>
<div class="row">
<?php foreach($users as $user) : ?>
<div class="col-lg-3 col-sm-6">
<div class="card text-center panel">
<div class="cardheader panel-heading" style="background: url(<?php if($user->user_id) : echo base_url('assets/img/users/covers/'.get_username($user->user_id).'/'.get_username_cover($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>)no-repeat center center;"></div>
<div class="avatar">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><img class="img-circle" alt="<?= get_username($user->user_id); ?> profile picture" title="<?= get_username($user->user_id); ?> profile picture" src="<?php if($user->user_id) : echo base_url('assets/img/users/avatars/'.get_username($user->user_id).'/'.get_username_avatar($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>"></a>
</div>
<div class="info panel-body">
<div class="title">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><?= get_username($user->user_id); ?></a>
</div>
<div class="desc"><?php if(get_occupation($user->user_id)) : ?><?= get_occupation($user->user_id); ?><?php else : echo 'No Job'; endif ?> <?php if(get_company($user->user_id)) : ?> - <b><?= get_company($user->user_id); ?></b><?php else : echo '- <b>No Company</b>'; endif ?></div>
<!-- BEGINNING OF HERE IS WHERE I NEED HELP -->
<?php if($skills) : ?>
<?php foreach($skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>
<!-- END OF HERE IS WHERE I NEED HELP -->
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<div class="alert alert-danger">No user with <?= $skill->title ?> skill found</div>
<?php endif; ?>
This is the actual output:
And this is the expected output:
I hope I could explain myself well enough to make it easy to understand,thanks in advance.
Hope this will help you :
Create a helper method get_skill_by_user just like get_username or get_occupation and use it in your view
function get_skill_by_user($user_id, $id)
{
$ci = & get_instance();
$ci->db->select('*');
$ci->db->from('ci_relationship');
$ci->db->where('term_id', $id);
$ci->db->where('user_id', $user_id);
$ci->db->where('type', 'skill');
$ci->db->order_by('id', 'DESC');
$query = $ci->db->get();
if($query->num_rows() > 0)
{
return $query->result();
} else
{
return false;
}
}
In your view your skills loop should be like this :
<?php $user_skills = get_skill_by_user($user->user_id, $user->term_id); if($user_skills) : ?>
<?php foreach($user_skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>
I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load the view page I do not see the username and no link to the users profile.
Here is my controller (User.php):
<?php
class User extends CI_Controller
{
public function index()
{
$this->load->view('profile', $data);
}
public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}
public function profile()
{
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
full model file (User_model.php)
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
}
?>
Problably because of this line:
<?=$row['username']; ?>
Maybe your server does not support short hand tags?
try this:
<?php echo $row['username']; ?>
You also missed a semicolon here at the end:
echo base_url() . 'User/profiel_user?id='.$row['user_id']
and also here:
<a href="<?php echo base_url() ?>/Cadeauaanvragen">
You also use table row and table data tags without a surrounding table tag. Or remove all tr and td tags.
See: How do browsers analyze <tr> <td> without <table>?
I want to retrive count of datas from one table and some datas from another table , also i want to know how to display these in a single foreach loop.
Model
public function counter(){
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$cnt = $this->db->count_all_results();
}
Controller
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
}
View part :
<?php foreach ($records as $r) { ?>
<div class="col-sm-6 col-md-3 col-xs-12">
<div class="bg-purple">
<h4 class="text-white">
<?php echo $r->category_name; ?>
</h4>
<span>200</span> Packages
<br>
<a style="margin-top: 10px;" href="<?php echo site_url() . "/gt_package/listing/" . $r->category_id ?>" class="btn btn-default">View Packages</a>
</div>
</div>
<?php } ?>
As per your question to pass the data model to controller code bellow
Model:
public function counter()
{
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$query = $this->db->get('gt_package');
$cnt = $query->num_rows();
return $cnt;
}
Controller:
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
if($res){
$data['result'] = $res;
$this->load->view('manage_accounts_view', $data);
} else {
echo "Fail";
}
print_r($data['result']);
}
I create just a simple blog and I can't get my desired output from the post, all I wanted is I want to get the firstname and lastname from the second table by using the userID from the first table.
controllers/posts.php:
<?php
class Posts extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('post');
}
function index() {
$data['posts'] = $this->post->get_posts();
$data['users'] = $this->post->get_users();
$this->load->view('post_index', $data);
}
function post($postID) {
$data['post'] = $this->post->get_post($postID);
$this->load->view('post', $data);
}
function correct_permissions($required) {
$user_type = $this->session->userdata('user_type');
if ($required == "User") {
if ($user_type) {
return true;
}
} elseif ($required == "Blogger") {
if ($user_type == "Blogger") {
return true;
}
}
}
function deletepost($postID) {
$user_type = $this->session->userdata('user_type');
if ($user_type != 'Blogger') {
echo "<script>alert:('Please log in to continue.');</script>";
redirect(base_url());
}
$this->post->delete_post($postID);
redirect(base_url() . 'posts');
}
}
models/post.php
<?php
class Post extends CI_Model{
function get_posts($num=50,$start=0){
$this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
$query = $this->db->get();
return $query->result_array();
}
function get_post($postID){
$this->db->select()->from('posts')->where(array('active'=>1,'postID'=>$postID))->order_by('date_added','desc');
$query=$this->db->get();
return $query->first_row('array');
}
function get_user($userID){
$this->db->select()->from('users')->where(array('userID'=>$userID));
$query=$this->db->get();
return $query->first_row('array');
}
function get_users(){
$userID = $this->session->userdata('userID');
$this->db->select('firstname','lastname')->from('users')->where('userID',$userID);
$query = $this->db->get();
return $query->result_array();
}
function insert_post($data){
$this->db->insert('posts',$data);
return $this->db->insert_id();
}
function update_post($postID,$data){
$this->db->where('postID',$postID);
$this->db->update('posts',$data);
}
function delete_post($postID){
$this->db->where('postID',$postID);
$this->db->delete('posts');
}
}
and my code from views/post_index.php
<div class="panel">
<?php if (!isset($posts)) { ?>
<p>There are currently no posts on my blog.</p>
<?php
} else {
foreach ($posts as $row) {
?>
<div class="panel-heading">
<h2 href="<?= base_url() ?>posts/post/<?= $row['postID'] ?>"><?= $row['title'] ?></h2>
</div>
<div class="panel-body">
<p><?= substr(strip_tags($row['post']), 0, 200) . "..." ?></p>
<br><br>
<div class="panelver">
<h6 href="<?= base_url() ?>posts/post/<?= $row['userID'] ?>">Posted by:
<?php
if($row['userID'] == 0) {
echo "Someone";
} else {
echo $this->db->select('firstname','lastname')->from('users')->where('userID',$row['userID']);
}
?></h6>
<p href="<?= base_url() ?>posts/post/<?= $row['postID'] ?>">Added last: <?= $row['date_added'] ?></p>
</div>
<p>Read More - - - Edit | Delete</p>
<hr/>
</div>
<?php
}
}
?>
</div>
The output:
Blog Posts
this is a post blah, blah, blah..... the "posted by:" must output the
author name getting userID from the second table value using the
userID from the first table as the output below, help me! T_T...
Posted by: 2
Added last: 2014-09-20 11:30:00
Read More - - - Edit | Delete
but when I do this code:
<div class="panel">
<?php if (!isset($posts)) { ?>
<p>There are currently no posts on my blog.</p>
<?php
} else {
foreach ($posts as $row) {
?>
<div class="panel-heading">
<h2 href="<?= base_url() ?>posts/post/<?= $row['postID'] ?>"><?= $row['title'] ?></h2>
</div>
<div class="panel-body">
<p><?= substr(strip_tags($row['post']), 0, 200) . "..." ?></p>
<br><br>
<div class="panelver">
<h6 href="<?= base_url() ?>posts/post/<?= $row['userID'] ?>">Posted by:
<?php
if($row['userID'] == 0) {
echo "Someone";
} else {
echo $this->db->select('firstname','lastname')->from('users')->where('userID',$row['userID']);
}
?></h6>
<p href="<?= base_url() ?>posts/post/<?= $row['postID'] ?>">Added last: <?= $row['date_added'] ?></p>
</div>
<p>Read More - - - Edit | Delete</p>
<hr/>
</div>
<?php
}
}
?>
</div>
this will happen:
Blog Posts
this is a post blah, blah, blah..... the "posted by:" must output the author name getting userID from the second table value using the userID from the first table as the output below, help me! T_T...
The "posted by:" must output the author name getting userID from the second table value using the userID from the first table as the output below.
Posted by:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_driver could not be converted to string
Filename: views/post_index.php
Line Number: 42
Added last: 2014-09-20 11:30:00
Read More - - - Edit | Delete
P.S: I'm a beginner programmer and new to CodeIgniter.
Ok.So what i get from your question is you have two tables with userId stored in one and user details in another.So try this as you need to get the row before echoing it.
echo $this->db->select('firstname','lastname')->from('users')->where('userID',$row['userID'])->get()->row();
the above code can also be written like this :
$this->db->select('firstname','lastname')->where('userID',$row['userID'])->get('users')->row();
Let me know if you have any doubts.
I want to edit some data after a POST in CodeIgniter.
The problem is when I make an edit I want to show a message that tells me the edit is done. I tried to do the following, but no message is displayed. Why?
Controller
function edit($id = 0) {
$this->load->model('Event_Model');
if ($id > 0) {
$this->data['eventinfo'] = $this->Event_Model->getInfo($id);
}
$this->data['pagetitle'] = "Edit";
$this->data['formID'] = $this->uri->segment(2)."-".$this->uri->segment(3);
$this->template->build('admin/events/add',$this->data);
}
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$this->Event_Model->update($id, $data);
$this->data['success'] = "Done";
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
}
View (where I want the message)
<?php
if (isset($success)) { ?>
<div class="alert alert-success normal-alert" style="display: block;">
<p><span class="ico-text ico-alert-success" ></span><?= $success; ?></p>
</div>
<?php
}
if (isset($errors)) { ?>
<div class="alert alert-error normal-alert" style="display: block;">
<div>
<span class="ico-text ico-alert-error"></span>
<?php if (count($errors) > 0) { ?>
<ul>
<?php
foreach ($errors as $error) {
echo "<li>$error</li>";
}
?>
</ul>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
and the second error, update data with picture not work, its get me error in dubg that call me the $_FILES['choose-file']['name'] not define
Try This:
Controller
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$result = $this->Event_Model->update($id, $data);
if($result) {
$this->data['success'] = "Done";
$this->template->build('admin/events/add',$this->data);
}
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model:
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
return $this->db->affected_rows();
}