I want to display all records from database to my view page. Having a table customers, how can I display all records of this table?
Here is my customer_model.php file..
function all_customss()
{
$query=$this->db->query("select * from customers");
return $query->result();
}
Here are my controller.php file...
function viewpage()
{
$result['data'] = $this->customer_model->all_customss();
$this->load->view('models_view', $result);
}
And here is the link which will display all records after click...
<div class="col-md-12 col-sm-12 text-center"><a class="btn btn-default hvr-bounce-to-right" href="<?php echo site_url('/viewpage/');?>" role="button"><?php echo $this->lang->line('see_more'); ?></a>
and here is my view file...page_view.php
tr><td><?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
?></td></tr>
You must put the name of your contoller before the name of your function like bellow:
<?php echo site_url('name_of_controoler/viewpage/');?>
function all_customss()
{
$query=$this->db->query("select * from customers");
return $query;
}
tr><td><?php
$i=1;
foreach($data->result () as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
I have modified your code. You should retrieve an array from a model. But it in your case you have already extracted the result in the model. And take a look in the view. I have modified your foreach parameter
I have edited Comment module from a tutorial to review module.
In this module i using CStarRating widget. When view, submit, delete rate post it OK. but when i click edit/update, edit old post and click update to edit rate post then star don't show, instead this radio group ???
Here is code:
CommentList.php
<?php
/** #var CArrayDataProvider $comments */
$comments = $model->getCommentDataProvider();
$comments->setPagination(false);
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$comments,
'itemView'=>'comment.views.comment._view'
));
$this->renderPartial('comment.views.comment._form', array(
'comment'=>$model->commentInstance
));
_view.php
<?php
?>
<nav id = "atext" >
<div>
<span class="ext-comment-head">
wrote on
<span class="ext-comment-date">
<?php echo Yii::app()->format->formatDateTime(
is_numeric($data->createDate) ? $data->createDate : strtotime($data->createDate)
);
?>
</span>
</span>
<span class="ext-comment-options">
<?php if (!Yii::app()->user->isGuest && (Yii::app()->user->id == $data->userId)) {
echo CHtml::ajaxLink('delete', array('/comment/comment/delete', 'id'=>$data->id), array(
'success'=>'function(){ $("#ext-comment-'.$data->id.'").remove(); }',
'type'=>'POST',
), array(
'id'=>'delete-comment-'.$data->id,
'confirm'=>'Are you sure you want to delete this item?',
));
echo " | ";
echo CHtml::ajaxLink('edit', array('/comment/comment/update', 'id'=>$data->id), array(
'replace'=>'#ext-comment-'.$data->id,
'type'=>'GET',
), array(
'id'=>'ext-comment-edit-'.$data->id,
));
} ?>
</span>
<p id = "apa">
<br>
</p>
<table id = "atable">
<td id = "test123">
<?php
$model=new Comment;
$a=$data->id;
$colArr=$model->attributes;
for ($i = 0; $i < count($colArr); $i++) {
$key=key($colArr);
$val=$colArr[$key];
next($colArr);
if (($key<> 'average') and($key<>'id') and ($key<>'message')
and ($key<>'userId') and ($key<>'createDate') ) {
$a=$a+100;
echo $data->getAttributeLabel($key).' ('.$data->$key.')';
$this->widget('CStarRating',array(
'name'=>'Comment['.$a.']',
'value'=>$data->$key,
'minRating'=>1, //minimal value
'maxRating'=>5,//max value
'starCount'=>5, //number of stars
'readOnly'=>true,
));
}
echo "<br>";
}
?>
</td>
<td id = "average-teacher">
<p>
<?php echo nl2br(CHtml::encode($data->message)); ?>
</p>
</td>
</table>
<br style="clear: both;"/>
</div>
</nav>
</div>
_form.php
<br>
<br>
<?php if (Yii::app()->user->isGuest) {
?><div class="ext-comment-not-loggedin">
Sorry, you have to login to leave a comment.
</div><?php } else { ?>
<div id="ext-comment-form-<?php echo $comment->isNewRecord ? 'new' : 'edit-'.$comment->id; ?>" class="form" >
<nav id = "comment-a">
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'ext-comment-form',
'action'=>array('/comment/comment/create'),
'enableAjaxValidation'=>false
)); ?>
<?php /** #var CActiveForm $form */
echo $form->errorSummary($comment); ?>
<div class="row">
<?php echo $form->labelEx($comment,'message'); ?>
<?php echo $form->textArea($comment,'message',array('rows'=>3, 'cols'=>76)); ?>
<?php echo $form->error($comment,'message'); ?>
</div>
<?php
$model=new Comment;
$colArr=$model->attributes;
for ($i = 0; $i < count($colArr); $i++) {
$key=key($colArr);
if (($key<> 'average') and ($key<>'id') and ($key<>'message') and ($key<>'userId')
and ($key<>'createDate') ){
$this->widget('CStarRating',array(
'name'=>'Comment['.$key.']',
'value'=>$model->$key,
'minRating'=>1, //minimal value
'maxRating'=>5,//max value
'starCount'=>5, //number of stars
));
echo " ".$model->getAttributeLabel($key)."<br>";
echo "<br>";}
next($colArr);
}
?>
<div class="row buttons">
<?php if ($comment->isNewRecord) {
echo "<br>";
echo $form->hiddenField($comment, 'type');
echo $form->hiddenField($comment, 'key');
echo CHtml::ajaxSubmitButton('Submit',
array('/comment/comment/create'),
array(
'replace'=>'#ext-comment-form-new',
'error'=>"function(){
$('#Comment_message').css('border-color', 'red');
$('#Comment_message').css('background-color', '#fcc');
}"
),
array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
);
} else {
echo CHtml::ajaxSubmitButton('Update',
array('/comment/comment/update', 'id'=>$comment->id),
array(
'replace'=>'#ext-comment-form-edit-'.$comment->id,
'error'=>"function(){
$('#Comment_message').css('border-color', 'red');
$('#Comment_message').css('background-color', '#fcc');
}"
),
array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
);
}
echo "<br>";
?>
</div>
<?php $this->endWidget() ?>
</nav>
</div><!-- form -->
<?php } ?>
Hope someone can help me :( And sorry about my english :(
i m creating a single page WP theme, actually theme is created BY some one else i am converting it to WP. all works fine but when a post from blog section is viewed and closed all the carousels breaks on the site. post opens in magnific popup.
Links:
Magnific:
$('.ajax-popup-link').magnificPopup({ type: 'ajax', closeOnBgClick: false });
Full code where I am using the popup:
<?php
$bl_args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DEC',
);
$counter = 0;
$bl_query = new WP_Query( $bl_args );
if ( $bl_query->have_posts() ) {
while ( $bl_query->have_posts() ) {
$bl_query->the_post();
if ( $counter%4 == 0 ) {
echo '<div class="blog">';
echo '<div class="row">';
} // open before every fourth item
echo '<div class="col-md-6">';
?>
<a href="<?php the_permalink(); ?>" class="ajax-popup-link">
<?php the_post_thumbnail('blog-thumb'); ?>
</a>
<?php
echo '<div class="row">';
echo '<div class="col-sm-2"><span><span>';
the_time('j');
echo '</span>';
the_time('F Y');
echo '</span>';
echo '</div><!-- .col-sm-2 -->';
echo '<div class="col-sm-10">';
echo '<h4>';
?>
<a href="<?php the_permalink(); ?>" class="ajax-popup-link">
<?php echo get_the_title(); ?>
</a>
<?php
echo '</h4>';
the_excerpt();
echo '</div><!-- .col-sm-10 -->';
echo '</div><!-- .row -->';
echo '</div><!-- .col-md-6 -->';
if ( $counter%4 == 3 ) {
echo '</div><!-- .row -->';
echo '</div><!-- .blog -->';
} // close after each block of four or after last item
$counter++;
}
}
/* Restore original Post Data */
wp_reset_postdata();
The problem was because with popup, the styles and scripts were loaded again. I removed wp_head() and wp_footer() from the popup and everything works great now.
I have this script to show the dynamic sku on select option, but i cannot get working.
Is loading the correct sku, but on select nothing happen.
This code get list of sku on Javascript and update a div on select option for product on configurable product view.
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<dl>
<?php foreach($_attributes as $_attribute): ?>
<dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
<dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
<div class="input-box">
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select" onchange="return changeSku(this);">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
<?php endforeach; ?>
</dl>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>
<?php endif;?>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
echo '<script type="text/javascript">';
echo '
document.observe("dom:loaded", function() {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
});
';
echo ' function changeSku(sel){';
$itemId = array();
foreach($col as $simple_product){
$itemId[] = array($simple_product->getSelectLabel() => $simple_product->getSku());
}
//echo "<pre>";
//print_r($itemId);
//echo "</pre>";
foreach($itemId as $val){
foreach($val as $k => $v){
echo "\n".'if(sel.options[sel.selectedIndex].value == "'.$k.'"){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong>'.$v.'");'. "\n";
echo '}';
}
}
echo "\n".'if(sel.options[sel.selectedIndex].value == ""){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");'. "\n";
echo '}';
echo "}";
echo "\n</script>";
?>
I appreciate any help.
Thanks
The script is almost correct except for $simple_product->getSelectLabel() being a wrong key. No such method/property exists in a simple product model. In order to make the script work, this key should be replaced with a right one - a Product Id. Utilizing product id, it is possible to find the sku of the product being selected.
So, first of all you need to reorganize itemId array to make it a "productId => productSku" map:
$productMap = array();
foreach($col as $simpleProduct){
$productMap[$simpleProduct->getId()] = $simpleProduct->getSku();
}
Then you need to change the "onchange" function call to pass Configurable's attribute id to the changeSku() function. Thus the underlying logic is able to search appropriate simple product's attributes.
onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
And after that you need utilize configurable's config in order to map selected simple product's attribute id to the product id selected:
function changeSku(confAttributeId, sel) {
var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
var selectedAttributeId = sel.options[sel.selectedIndex].value;
if (selectedAttributeId) {
var options = spConfig.config.attributes[confAttributeId].options;
var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
$("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]);
} else {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
}
}
For your reference, below is the summary of how the whole template looks like (I've beautified it a little):
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<dl>
<?php foreach($_attributes as $_attribute): ?>
<dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
<dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
<div class="input-box">
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select"
onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
<?php endforeach; ?>
</dl>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>
<?php endif;?>
<div id="sku-container"></div>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$productMap = array();
foreach($col as $simpleProduct){
$productMap[$simpleProduct->getId()] = $simpleProduct->getSku();
}
?>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
});
function changeSku(confAttributeId, sel) {
var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
var selectedAttributeId = sel.options[sel.selectedIndex].value;
if (selectedAttributeId) {
var options = spConfig.config.attributes[confAttributeId].options;
var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
$("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]);
} else {
$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
}
}
</script>
This will do the task you originally needed.
Also note the following
Your approach won't work for a configurable product with two or more configurable attributes. For that product a final simple product is not known until a user selects values for all the select inputs. So an approach should be changed to check all the selects before outputting SKU.
The code doesn't consider a case, when user edits product configuration, rather than specifying configuration for a new product. You can go to editing mode from the Shopping Cart clicking the Edit link. In such a case all the select inputs will be pre-filled with previously chosen values. But the text will say "Select an option to display Product Id". The script might also produce other Javascript errors in Edit mode. The code should be slightly modified in order to support Edit mode as well.
The template is overfilled with logic. A Magento template should have only simple prints and foreach-iterations. All the methods like $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions() are better to be moved to block. This reduces code complexity.
Hope it helps.
How can i add custom class="featured" into html\com_content\category\blog_item.php if that rendered article is featured too?
Is there a line of code that can determines if the article is featured?
Thanks!
Ok i`ve got it!
Into html\com_content\category\blog.php
Paste this lines on line 79
<?php
$featuredclass = '';
$item = &$item;
if ($item->featured == 1) {
$featuredclass = 'featured';
}
?>
And add
<?php echo $featuredclass;?>
on line 86
<div class="items-row cols-<?php echo (int) $this->columns;?> <?php echo 'row-'.$row ; ?>">
So it looks like this:
<?php
$featuredclass = '';
$item = &$item;
if ($item->featured == 1) {
$featuredclass = 'featured';
}
?>
<div class=" <?php echo $featuredclass;?> items-row cols-<?php echo (int) $this->columns;?>
<?php echo 'row-'.$row ; ?>">
Special thanks to Bojcheski