Selected option in dropdownlist in yii - php

How can I have a default selected option in a dropdownlist in Yii? And then when the user enter my view the data would be displayed automatically?For example I pass some months via an action January, February etc and I want to have preselected the current month and when the user enters my view he should see the data from the current month? I think I have to play with javascript but I'm having trouble with the syntax because I'm fairly new to this.
This is my view:
<?php
?>
<div class="panel panel-primary">
<div class="panel-heading">
<?php
echo $schoolclass->description;
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<?php
$presentsData = CHtml::listData($presentmonths, 'presentmonth','presentmonth');
echo CHtml::dropDownList('presentmonthlist', 'presentmonth', $presentsData,
array(
'ajax'=>array('type'=>'POST',
'url'=>Yii::app()->createUrl('present/viewmonthlypresents',array('id'=>$schoolclass->schoolclassid)),
'update'=>'#mypresents',
'data'=>array('presentmonth'=>'js:this.value')
)
));
?>
</div>
</div>
<div id="mypresents">
</div>
<div class="panel panel-default">
<div class="panel-body">
<?php echo CHtml::button(Yii::t('default' ,'Submit Presents'), array(
'submit'=>array('create', 'id'=>$schoolclass->schoolclassid)
)
);
?>
</div>
</div>

you can set default selected option like this, for example i used 'January'
echo CHtml::dropDownList('presentmonthlist', 'January', $presentsData,
array(
'ajax'=>array('type'=>'POST',
'url'=>Yii::app()->createUrl('present/viewmonthlypresents',array('id'=>$schoolclass->schoolclassid)),
'update'=>'#mypresents',
'data'=>array('presentmonth'=>'js:this.value')
)
));

Related

How to make TbModal work properly in PhP (Yii)

I am looking to make a simple Pop-Up window using TbModal (as requested by my superiors). However, I have no clue how to do this. I have never written a modal before and I am not certain if I am doing it completely wrong.
All I need to do, is to create a Pop-up window once a button is clicked. In this window I need to simply add a name, a description and something that is called carousel. However, whenever I try to make it work I get the following error:
Alias "bootstrap.widgets.TbModal" is invalid. Make sure it points to an existing directory or file.
Below is what the button in my view looks like (using a TbButtonColumn):
'buttons'=>array(
'update'=>array(
),
'delete'=>array(
),
'clone'=>array(
'id'=>'clone-dashboard-button',
'label'=>'Clone',
'title'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'option'=>array(
'rel'=>'tooltip',
'data-toggle'=>'tooltip',
'title'=>'Clone'
),
),
),
As well as the modal:
<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
'id'=>'clone-form',
// 'focus'=>'#Dashboard_clone_title',
'htmlOptions'=>array(
'tabindex'=>'-1',
))); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Test</h4>
</div>
<div class="model-body">
<div class="row">
<div class ="col-md-2">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textArea($model,'title', array('rows'=>1, 'cols'=>20,'class'=>'resize-non form-control')); // Title input?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description', array('rows'=>1, 'cols'=>30,'class'=>'resize-non form-control')); //Description ?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'carousel'); ?>
<?php echo $form->checkbox($model,'carousel'); ?>
</div>
</div>
</div>
<div class="modal-footer">
<!--Buttons-->
<div class="pull-right">
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'htmlOptions'=>array(
'onClick'=>"
$(this).attr(\"disabled\", \"disabled\");"
),
)); ?>
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'button',
'label'=>'Cancel',
'htmlOptions'=>array(
'data-dismiss'=>'modal',
),
)); ?>
</div>
</div>
<?php $this->endWidget(); ?>

Pass array to view in Codeigniter

I have a breadcrumb that i send to my view pages, but the part that have an array not load, just static code.
I think the problem it's in variable inside array, because i have another breadcrumbs with same problem, the static is loaded.
Example breadcrumb that I want: Home -> Product -> "Tshirt name"
Controller:
$itensBreadCrumb["itensBreadCrumb"] = array(
"Home" => "home",
"Produtos" => "produtos", // after this don't load in view.
strtolower($produto->nome) => ""
);
$dataProdutos['produto'] = $produto;
$dataProdutos['estoques'] = $this->produtos_model->retornaEstoqueProduto($produto[0]->id);
$dataProdutos['breadcrumb'] = $this->load->view("breadcrumb", $itensBreadCrumb, TRUE);
Breadcrumb structure View:
<div class="breadcrumb">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php foreach ($itensBreadCrumb as $item => $link) {
if(!empty($link)){
?>
<?=$item?>
<?php }else{ ?>
<span class="item-bread"><?=$item?></span>
<?php
}
} ?>
</div>
</div>
</div>
How i'm calling breadcrumb in pages.
<?= $breadcrumb ?>

array looped through foreach not displaying any data

I was working with an array which I have to list in a view page. so I iterated the array using a foreach loop. but it's not displaying any data and throwing an error like
trying to get property of non-object
I have tried the normal way but didn't get it
<div class="scrollbar1 table-content">
<?php
foreach($items_list as $row)
{
?>
<div class="table-row">
<div class="row list_row_style color_black common_rows">
<div class="col-md-12 no_padding">
<a href="add_ledger_sub_type?ledger_name=<?php echo $row->ledger_name; ?
>">
<div class="row">
<div class="col-md-2 list_common_text_style table-data">
<?php echo $i; ?></div>
<div class="col-md-3 list_common_text_style table-data" >
<?php if(!empty($row->sub_ledger_name)) echo $row->sub_ledger_name; ?>
</div>
<div class="col-md-3 list_common_text_style table-data" >
<?php if(!empty($row->ledger_name)) echo $row->ledger_name; ?>
</div>
<div class="col-md-4 list_common_text_style table-data" >
<?php if(!empty($row->description)) echo $row->description; ?>
</div>
</div></a>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
My expected result is this code lists all items in '$items_list' array. But it's not
Please help me.
can you put $row["ledger_name"] instead of $row->ledger_name
Yes, I got the answer. It was a small mistake. I took the items to an array in database_models and again stored to another array in 'controller'.

Sort array alphabetically in PHP

I'm creating a website for a book publishing company. They've books in multiple different languages and want to display some details for each language. To retrieve all the necessary information for each language, I do the following:
<div class="col-lg-3" align="center">
<?php
$fields = get_field_objects();
if ($fields):
foreach ($fields as $name => $field):
if (stripos($name, 'isbn') !== false) : ?>
<?php $lang = substr($field['name'], strpos($field['name'], "_")); ?>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
</h4>
</div>
<div id="collapse1<?php echo $lang ?>" class="panel-collapse collapse">
<div class="panel-body">
// ... get all neccessary information ...
</div>
</div>
</div>
</div>
<?php endif;
endforeach;
endif; ?>
</div>
Now my problem is, that if somebody creates a book, forgets to add a language and wants to add it after the book has been saved to the database, the alphabetical order is not correct anymore.
Therefore I'd like to add a sorting function for the $fields array.
sort($fields) doesn't work since after doing this, everything is blank.
Here's a screenshot of how the output looks like.
And sometimes, the order is wrong (e.g. Deutsch/English appears on the bottom). So I'll have to sort the part where I add the second language (next to German). The part where I add this is here:
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
</h4>
Does anyone have any ideas? Please let me know if code is missing!
P.S. the plugin I use is "Advanced-Custom-Fields" and the functions like e.g. "get_field_objects()" come with that plugin!
get_field_objects() returns an array of custom field objects. You can sort it alphabetically using uasort like this:
$fields = get_field_objects();
uasort($fields,"sort_alphabetically");
function sort_alphabetically($a,$b) {
return $a['name'] > $b['name'];
}
So what solve my problem is #LeoTahk 's suggestion of adding ksort().
Final code:
<div class="col-lg-3" align="center">
<?php
$fields = get_field_objects();
//working solution
ksort($fields);
if($fields):
....
endif; ?>
</div>

Tile Thumbnails in a Grid

I want to tile thumbnails into 3 columns using bootstrap's grid classes like this (this entry only has 3 images):
The 4th image would go to the next row <div class="row"></div> within a <div class="col-sm-4"></div>, 5th and 6th images in the same row but separate <div class="col-sm-4"></div>. Then the 7th image goes to the 3rd row etc...
The details of the images including urls are taken from DB using php.
<div class="panel-body">
<?php foreach($screenshots as $key=>$screenshot): ?>
<div class="col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?=$screenshot["ss_name"]?></h3>
</div>
<div class="panel-body">
<img class="img-rounded" style="display: block; text-align:center;" src="<?=UPLOADS_FOLDER.$screenshot['ss_url']?>" alt="<?=$screenshot['ss_name']?>">
<p><?=$screenshot["ss_description"]?></p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
I've managed to figure out the algorithim:
<?php
$total_entries = count($screenshots);
$total_cols = 3;
$total_rows = ceil($total_entries / $total_cols);
for($col = 0; $col < $total_cols; ++$col):
?>
<div class="row" style="margin: 1% 0.5%;">
<?php for($row = 0; $row < $total_rows; ++$row): ?>
<div class="col-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Panel Title</h3>
</div>
<div class="panel-body">
row: <?=$row?> | col: <?=$col?>
</div>
</div>
</div>
<?php endfor; ?>
</div>
<?php endfor; ?>
But I'm stuck trying to figure out how to find of the index of the screenshot to show.
You have row and column mixed up in the output above.
Once that is fixed, if you need an integer index you should be able to calculate it from the row and column values. For a zero based array of images, something like (row*3)+column
That said, in Bootstrap, you should not need to create the individual rows in the way you have. If you put all the col-sm-3 divs one after the other, without breaking out new rows, this will sort itself out anyway.
Doing it this way, you can use col-Xxx to specify different numbers of columns for different screen widths without changing your code.
The trick is to send every new data in a single column define by <div class="column-sm-4"> and in order to have three items in a row, calling <div class="row"> after every three items. Which can be done by using a counter initialized at 0 and then incrementing its value by 1 every time and inserting new row when dividing it by 3 results an integer:
$count = 0;
if (is_int($count/3)){
echo '<div class="row">';
}
We need to insert a div before first element so we check if its the starting:
if ($count==0 OR is_int($count/3)){
echo '<div class="row">';
}
Then in order to close the div we need to check again if the division results an integer:
if(is_int($count/3)){
echo '</div>' ;
}
We also need to close the div after the last element so we check whether the element is last one or not by:
if($count==$total_entries OR is_int($count/3)){
echo '</div>' ;
}
The full code is:
<?php
$count = 0;
$total_entries = count($screenshots);
foreach($screenshots as $key=>$screenshot):
if ($count==0 OR is_int($count/3)){
echo '<div class="row">';
}
?>
<div class="column-sm-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?=$screenshot["ss_name"]?></h3>
</div>
<div class="panel-body">
<img class="img-rounded" style="display: block; text-
align:center;" src="<?=UPLOADS_FOLDER.$screenshot['ss_url']?>"
alt="<?=$screenshot['ss_name']?>">
<p><?=$screenshot["ss_description"]?></p>
</div>
</div>
</div>
<?php
$count++;
if($count==$total_entries OR is_int($count/3)){
echo '</div>' ;
}
endforeach;
?>

Categories