Display comment regarding posts - ajax

I am developing a blog using codeigniter, every thing works good like posts are being stored comments are being made likes also works the only problem is fetching the comments using codeigniter against each post. What i want to do is that i want to send id to controller of each id when page loads. Here is the screenshot to make it more clear how i am working.
Front end is like this
Now the code section:
1. Code on the blog page where all messages and posts are being shown
<div class="col-lg-11 col-md-10 col-sm-10 col-xs-9 post_message" id="getComments<?php echo $blog -> blog_id; ?>">
<p>
<?php echo $blog -> message; ?>
</p>
<span class="pull-left">
<small> Posted By: <?php echo $blog -> name; ?></small>
</span>
<!-- onclick="get_comments(this.id, '<?php //echo base_url('users/get_comments_total'); ?>')" id="<?php// echo $blog -> blog_id; ?>-->
<a>
<i class='fa fa-comment' aria-hidden='true'></i> Comments (<small> <?php echo $rows; ?> </small>)
</a>
<a onclick="update_like('<?php echo $blog -> blog_id; ?>', '<?php echo base_url('users/update_like'); ?>')">
<i class='fa fa-thumbs-up' aria-hidden='true'></i> Likes (<small><?php echo $blog -> likes; ?></small>)
</a>
<div class="result_id<?php echo $blog -> blog_id; ?>" style="display: block;float: left;width: 100%;"></div>
</div> <!-- End of post message -->
In the above code there is a line written in comments where on-click function is defined i want to pass each id on page load to controller instead of click and all this code is written in foreach loop.
Help me with this thing or tell if there is any better idea to achieve this. Please do see the image attached with this so that you can know easily how i want to display the comments. one hint is like FB comments.

What is $rows?
If the likes are working OK then you can use the same idea for the comments. Probably you have joined the likes table and that is why you have $blog -> likes;. You should join the comments table to the query and then use Comments (<small> <?php echo $blog->comments; ?> </small>).
If this does not work you should provide your query in the model and your database structure.

Related

How do I call the function to check in a view?

How do I check if my database has more than 9 entries, and if it has, I can advance to the next page.
Currently I have this code, but I don't know how to call that in my view:
Here in the controller I have already called the checklogin and see if it has more than 9 entries.
Controller:
function quizzes(){
$this->load->model('Quiz_Model');
$data['categorys'] = $this->Quiz_Model->SearchAllCategorys();
$this->load->view('headerLogged');
$this->load->view('quizzes', $data);
$this->load->view('footer');
}
function proses_quiz($id_category){
$this->load->view('headerLogged');
$checkquiz=$this->Quiz_Model->checkquiz($id_category);
if($checkquiz > 9){
redirect('User_Ctr/quiz/').$id_category;
}else{
$data['error_quiz']="MEH";
$this->load->view('quizzes', $data);
}
$this->load->view('footer');
}
And here it's just the code to make the count:
Model:
public function checkquiz($id_category){
$this->db->select('id_question, COUNT(id_question) as total');
$this->db->from('questions');
$this->db->where('id_category', $id_category);
$result = $this->db->get();
return $result->result();
}
View:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="container">
<h1 class="my-4 text-center text-lg-left">Quizzes</h1>
<div class="row text-center text-lg-left">
<?php foreach($categorys as $cat): ?>
<div class="col-lg-3 col-md-4 col-xs-6" >
<a href="<?php echo 'quiz/'.$cat->id_category ?>" class="d-block mb-4 h-100" >
<img class="img-fluid img-thumbnail" src="<?php echo $cat->img_category ?>" alt="" style="height: 125px; width: 400px">
<?php
if(isset($error_quiz)){
echo $error_quiz;
}
?>
</a>
</div>
<?php endforeach ?>
</div>
</div>
I will answer you question generally.
If you want to call something in the view you have many options like you have the variable already calculated in advance and already passed it to view waiting for a call, or you can click on a link that maybe reload the current view with the new variable done, or you can use ajax to go get the variable from the controller without reloading.
So if the variable has fixed value that doesn't depend on your action, go with the first option or else chose one of the others.

How to use Microdata for URLs in Joomla?

I read here about Microdata in Joomla. However, I'm not able to produce a link with Microdata.
I want a Microdata output like:
<a itemprop="url" href="http://someurl">
<span itemprop="name">Some Url</span>
</a>
I want a link to an organisation from my client's website. My code is:
$microdata = new JMicrodata('Organization');
<div <?php echo $microdata->displayScope();?>>
echo $microdata->content( $this->item->url )->property( 'sameAs' )->display();
// outputs <span itemprop="sameAs">www.url_of_company.com</span>
</div>
Both url and sameAs Schema.org properties are of type URL, as per my understanding.
I also tried to wrap it with an anchor tag but Joomla is not producing itemprop along with href attribute in anchor tag.
I must be missing something.
Ok I analysed in libraries/joomla/microdata/microdata.php the method display();
and it doesn't exist any way to produce a <a href="#" > tag, only div, span and meta.
Btw there is a static method called htmlProperty(), with that you can add the property you need.
<?php $microdata = new JMicrodata('Organization'); ?>
<div <?php echo $microdata->displayScope(); ?> >
<a <?php echo JMicrodata::htmlProperty('url'); ?> href="http://google.com">
<?php echo $microdata->content('Google')->property('name')->display(); ?>
</a>
</div>

I have a script, in yii. That almost does the job, need just a little push

I am not a programmer by any means. I purchased the script and have spent two weeks and many variations trying to get the code to do what I need. It is 'almost' there.
<div id="step-2" style="position:absolute; center:overflow-y:;">
<?php if($images): ?>
<?php foreach ($images as $img => $data): ?>
<a href="javascript:;" class="pin-img">
<img src="<?php echo $img ?>" />
</div>
</a>
<?php endforeach; ?>
<?php else: ?>
<h2><?php echo Yii::t('yii', 'No relevant found!') ?></h2>
<?php endif ?>
</div>
It returns the images from a site. It only lets me select the first image. Even though all images are displayed. Only one is actually clickable. If I hover over each of the images I see the javascript href. I understand that to be some sort of place holder. that just makes the browser show the hand.
I just need the script to allow each item to be a choice for reaction.
http://silenceisnotawkward.com/Sinapin/Script/# is the live code.
I have made a test user
Test=user
Password =password in case anyone wants to see what the site does.
http://www.ew.com/ew/ is a good link to pin from website and see the behavior.
I didn't look at your site, but looks like you are confusing your nesting of HTML tags, because you introduced a end-of-dev tag in your link.
<div id="step-2" style="position:absolute; center:overflow-y:;">
<?php if($images): ?>
<?php foreach ($images as $img => $data): ?>
<a href="javascript:;" class="pin-img">
<img src="<?php echo $img ?>" />
<!-- I removed the < / div > from here -->
</a>
:
<?php endforeach; ?>
</div
>
HINT: I indented the code to make it more readable, Try doing the same with your code you posted and see where it leads you.

Updating Values Using Ajax in Yii

This is my first ever post....
I'm working on a shopping website using Yii-Booster but I'm currently stumped! Basically what I'm trying to achieve is that when a user adds a product to the cart, a modal displays showing an acknowledgement message and then the div with the shopping cart is updated to reflect the new total.
Really simple, innit?? I've spent the entire day trying to get it to work. Any help you can offer would be greatly appreciated.
Oh yeah... one more thing, the dropdown menu in the navbar stops working after almost any sort of AJAX action(loading modals, etc). I'm guessing its caused by a conflict somewhere but I'm completely lost in Jquery/Ajax stuff.
Thanks for the replies. Here's my code:
StoreController:
// action allowing a product to be added to the shopping cart
public function actionAddToCart($id)
{
//fetch item
$item = Item::model()->findByPk($id);
//if item is in cart
if(Yii::app()->shoppingCart->contains($id))
{
//Yii::app()->clientScript->scriptMap['jquery.js'] = true;
//$this->renderPartial('_addToCart',array('data'=>$data),false,true);
echo CJSON::encode(
array(
//'total'=>'Your Cart (N'.number_format(Yii::app()->shoppingCart->getCost()).')',
'addCart'=>$this->renderPartial('_inCart',array('item'=>$item),true),
)
);
//CHtml::ajax();
}
else
{
Yii::app()->shoppingCart->put($item);
//Yii::app()->clientScript->scriptMap['jquery.js'] = true;
//$this->renderPartial('_addToCart',array('data'=>$data),false,true);
//$this->renderPartial('_addToCart',array('item'=>$item),false,true);
echo CJSON::encode(
array(
'total'=>'Your Cart (N'.number_format(Yii::app()->shoppingCart->getCost()).')',
'addCart'=>$this->renderPartial('_addToCart',array('item'=>$item),true),
)
);
}
}
_display (partialview showing the product)
<div style="float: left; display: inline; margin-right: 30px; margin-top: 20px;">
<img style="border: 1px solid black;" src="<?php echo Yii::app()->baseUrl; ?>/images/products/thumbs/<?php echo $data->image; ?>" />
<div style="font-size: 14px; margin-top: 3px;">
Name: <?php echo CHtml::ajaxLink($data->name, array("view", "id"=>$data->id), array('update'=>'#viewdetailsDiv',)); //array(), array('data-toggle'=>'modal','data-target'=>"#".$data->id)); ?><br/>
Category: <?=$data->category->category?><br/>
Gender: <?=$data->gender?><br/>
Price: <?php echo "N".number_format($data->price);?><br/>
<?php //echo CHtml::ajaxLink("Add to Cart", array("addtocart", "id"=>$data->id), array( 'update'=>'#addtocartDiv',)); ?>
<?php echo CHtml::ajaxLink("Add to Cart", array("addtocart", "id"=>$data->id), array( 'type'=>'POST', 'dataType'=>'json',
'success'=>'function(data){
$("#Cart").html(data.total);
}'
)
); ?>
</div>
Here's the code for the view containing the modal
<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal', 'autoOpen'=>true, 'htmlOptions'=>array('style'=>'margin-top: 2px;'))); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Your Cart has been updated</h4>
</div>
<div class="modal-body">
<p>The item, <strong><?php echo $item->name; ?></strong> (<?php echo $item->category->category; ?>), has been added to your shopping cart.</p>
</div>
<div class="modal-footer">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'type'=>'primary',
'label'=>'Close',
'url'=>'#',
'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
</div>
<?php $this->endWidget(); ?>
I can't tell you the perfect solution but can give you a suggestion regarding Yii that can solve your problem.
Always load all the jquery or js files used in first request. Never load again any js file in any ajax request, It will a lot of problems.
You can achieve it in simple way
if(Yii::app()->request->isAjaxRequest){
Yii::app()->clientScript->scriptMap['jquery.js'] = true;
}

Codeigniter link issue

Hello all first sorry for the stupid question.
I am making a small job board with codeigniter, and i ran into a problem.
On the main page it lists the jobs and when i click on them it redirect to the details page with the infos about the job.
My problem is i made a category filter where they can browse the jobs by clicking on a category, and on that page when i click the link for the details its giving me this url: localhost/jobboard/lists/category/lists/details/test-job insted of this localhost/jobboard/lists/details/test-job <-- this is the right one.
<li>
<a href="<?php base_url(); ?>lists/details/<?php echo $res->job_link ?>">
<span><?php echo $res->job_title ?></span>
<p><?php echo $job_type ?> | <?php echo $res->job_location ?> | <?php echo $res->company_name ?></p>
</a>
</li>
and the above code should not show this: *localhost/jobboard/lists/category/lists/details/test-job
Could please soeone give me a hint? because i tried a few things and im totally clueles
Thank you
Darn i got it, i forgot to echo the base url....

Resources