get value of CHtml::activeDropDownList in yii - yii

I have this code
<?php echo CHtml::activeDropDownList(
$semaineModel,
'libelleSemaine',
CHtml::listData(Semaine::findBySql('SELECT * FROM Semaine')->all(), 'idSemine', 'libelleSemaine')
); ?>
but why that displays just the last element of the table, and me I have 6 items in this table 'Semaine 1' to 'Semaine 6'
and that code display just 'Semaine 6'.
an idea please ?

Then you don't nedd CHtml but active dropDownList
Assiming your Semain model is named Semain
use app\models\Semaine;
use yii\helpers\ArrayHelper;
$semaines=Semaine::find()->all();
$listSemaines = $listData=ArrayHelper::map($semaines,'idSemine', 'libelleSemaine');
echo $form->field($model, 'idSemaine')->dropDownList( $listSemaines,
['prompt'=>'Select Semaine...']);

Related

Yii2: How to show SQL results?

I want to show the result of this SQL query execution in a view:
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
$people = print_r($results);
<?= $people ?>
But it always shows: 1
The results should be a table with 3 columns and 10 rows. So I don't know why it shows 1. I tried with different tables but always shows the same.
depend of your need you
QueryALL return All rows of the query result.
Each array element is an array representing a row of data
so you could simple iterate over you $result
foreach($result as $key => $row){
echo $row['column1'] . '<br />' ;
echo $row['column2'] . '<br />' ;
....
}
otherwise you can use the queru for build a sqlDataProvider and the manage the models show with widget (eg: gridView)
wrong usage of print_r. see this link. did you mean:
$people = print_r($results, true);
or just use var_dump to avoid confusion.
$results already contains your rows.
This way you'll see this array $results.
$results = Yii::$app->getDb()->createCommand('SELECT * FROM colors')->queryAll();
echo "<pre>";
print_r($results);
exit;

Separate woocommerce variable products on archive page

Is there a way to show woocommerce product variations separately on the product category, search or another archive page? For example, if there are red t-shirt and yellow t-shirt variations it should display 2 products on 't-shirt' search result page.
Thanks.
Try this code..
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
with this:
<?php
/**
* ADD PRODUCT VARIABLE WITH ADD TO CART ON CATALOG PAGE
ADDED BY GW
**/
?>
<div class="catbox">
<?php
//ADD SHORT DESCRIPTION TO PRODUCT
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5); ?>
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
<?php
//ADD VARIATIONS BEFORE ADD TO CART
if($product->product_type == "variable"){
woocommerce_variable_add_to_cart();
} else {
woocommerce_template_loop_add_to_cart();
}
?>

DropDownList won't POST value by using ajax

I created DropDownList and I'm trying to fill div with content and I know how to do it. But problem is that my dropdown list won't POST value.
This is code from view\index.php
<?php
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
?>
<div id="legla"></div>
And this is from controller
public function actionAjaxIzlistajLegla()
{
echo $_POST['parovi'];
}
this AjaxIzlistaijLegla action is inside accessRules(). And it's working cause When I replace echo $_POST['parovi']; with echo "Hello"; it works, it updates my div. But I don't know why it won't POST that value from drop down list.
jquery is included in head.
Answer is I should wrap my dropDownList with CActiveForm like this
$this->beginWidget('CActiveForm', array(
'id'=>'par-uzgojni-ispis-legla-form',
));
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
$this->endWidget();
Because I didn't wrap it, it couldn't POST any data to my controller

How to append a parameter to current page using CHtml::link?

Using Yii, and trying to append a Lang=xx to the end of the current page url and present it on the page.
I put the below code in the protected/views/layout/main.php
<?php echo CHtml::link('English', array('','lang'=>'en'), array('class'=>'en')) ?>
<?php echo CHtml::link('中文', array('','lang'=>'tw'), array('class'=>'tw')) ?>
<?php echo CHtml::link('日本語', array('','lang'=>'jp'), array('class'=>'jp')) ?>
With standard pages like "/site/index", or controller action pages like "/site/contact", they work fine. But with the standard static pages like "site/page?view=about", it's not working. The url expected should be something like "site/page?view=about&lang=tw", but instead, it gives me "site/page?lang=tw".
How can I fix that?
I ended up doing it with langhandeler extension and url rules and map [site]/[path]?lang=[language code] to [site]/[language code]/[path]
And then I coded the links like below:
<?php
$request = $_SERVER['REQUEST_URI'];
$path_a = explode("/",$request);
$haveLang = isSet($_GET["lang"]);
$uri = ($haveLang?
substr($request, strlen($path_a[1])+1) //strip language prefix and the slash
:$request); //don't process if the page is in default language
echo CHtml::link(CHtml::encode(Yii::app()->name), CHtml::normalizeUrl(($haveLang?'/'.$_GET["lang"].'/':'/')), array('id'=>'logo'));
?>
<div id="lang_switch">
<?php
echo CHtml::link('English', CHtml::normalizeUrl($uri), array('class'=>'en')); //no need to add default language prefix
echo CHtml::link('中文', CHtml::normalizeUrl('/tw'.$uri), array('class'=>'tw'));
echo CHtml::link('日本語', CHtml::normalizeUrl('/jp'.$uri), array('class'=>'jp'));
?>
</div>
that pretty much solved my problem. I hope this could help out someone else in the future.
you can give chtml link like this
$language = 'en';
CHtml::link("English", array('site/about/lang/' . $language));
site/about/lang/en = controller/action/lang/en
i hope this will help you.

symfony get data from array

I'm trying to use an SQL query to get data from my database into the template of a symfony project.
my query:
SQL:
SELECT l.loc_id AS l__loc_id, l.naam AS l__naam, l.straat AS l__straat,
l.huisnummer AS l__huisnummer, l.plaats AS l__plaats, l.postcode AS l__postcode,
l.telefoon AS l__telefoon, l.opmerking AS l__opmerking, o.org_id AS o__org_id, o.naam AS o__naam
FROM locatie l
LEFT JOIN organisatie o
ON l.org_id = o.org_id
This is generated by this DQL:
DQL:
$this->q = Doctrine_Query::create()
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->from('Locatie l')
->leftJoin('l.Organisatie o')
->execute();
But now when i try to acces this data in the template by either doing:
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o.naam'] ?>
or
<?php foreach ($q as $locatie): ?>
<?php echo $locatie['o__naam'] ?>
i get the error from symfony:
500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "o__naam" on "Locatie"
Does anyone know what is going wrong here? i dont know how to call the value from the array if the names in both query's dont work.
Doctrine will have hydrated your results into objects corresponding to the models in your query. In your case these will be Locatie and Organisatie. You should therefore be able to access the data as follows:
<?php foreach ($q as $obj): ?>
<?php echo $obj->Locatie->naam; ?>
<?php echo $obj->Organisatie->naam; ?>
<?php endforeach; ?>
If you have the above method in eg the Locatie table class and use self::create("l") to create your method, the object you use in the view won't need the ->Locatie part.
Edit: table method example:
class LocatieTable extends Doctrine_Table
{
public function getLocaties()
{
$q = self::createQuery("l")
->select('l.naam, o.naam, l.straat, l.huisnummer, l.plaats, l.postcode, l.telefoon, l.opmerking')
->leftJoin('l.Organisatie o')
->execute();
return $q;
}
}
You should be able to find this class (probably empty) already auto-generated in lib/model/doctrine/LocatieTable.class.php. Now call it with:
$this->q = Doctrine::getTable("Locatie")->getLocaties();
If you want to know how to get some value from the result DoctrineRecord object I advise to use var_dump($obj->toArray()) method to get clear view of the object structure. After that you can use several types of getters to retrive what you want (e.g. $obj->A->b, $obj->getA()->getB() etc..)