Posts Tagged with array

Displaying 1-10 of 20 results.
Resolved: Yii2 NavBar check isGuest leave empty or fill with multiple items
posted by admin on February 21, 2017

<?php
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use app\helpers\App;
use yii\helpers\Html;

NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => App::t('Home'), 'url' => ['/site/index']],
Yii::$app->user->isGuest ? (
['label' => App::t('Login'), 'url' => ['/site/login']]
) : (
'<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
App::t('Logout').' (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>'
),
],
]);
NavBar::end();
?>

I want to add functionality like these:
1. I want to leave else empty
example

Yii::$app->user->isGuest ? (
['label' => App::t('Sign up', 'app2'), 'url' => ['/site/registration']]
) : (
[]//want to leave empty
),

2. I want multiple array
example

Yii::$app->user->isGuest ? (
['label' => App::t('Sign up0', 'app2'), 'url' => ['/site/zczczcxz']],
['label' => App::t('Sign up1', 'app2'), 'url' => ['/site/qweqweqe']],
['label' => App::t('Sign up2', 'app2'), 'url' => ['/site/asdasd']]
) : (
['label' => App::t('Sign up4', 'app2'), 'url' => ['/site/oipoiopi']],
['label' => App::t('Sign up4', 'app2'), 'url' => ['/site/oipoiopi']]
),

Both example not working and throwing errors, I think there is one decision for both situation with errorsRead more
Resolved: Yii 1.1 Cgridview filter listdata without findAll from db, with php array
posted by admin on November 7, 2016
I have CGridView with select filter, and I want to insert select box info manually with php array.
Now I have result with findAll():

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'application-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
...
array(
'name'=>'status_id',
'value'=>'$data->status->name',
'type'=>'html',
'filter' => CHtml::listData(Status::model()->findAll(), 'id', 'name'),
),
...
),
)
);
Read more
Resolved: PHP datetime to russian user friendly format converting. Дата и время на русский на дружественный вид
posted by admin on October 12, 2016
I have datetime from databse MySQL, in format
2016-10-10 12:34:33

By PHP datetime formats I can have string like this
07 October 2016, 04:11, Friday

But I want the3 same output, but in russian
07 Октября 2016, 04:11, ПятницаRead more
Resolved: Yii 1.1 Cmenu item url array working by wrong logic
posted by admin on October 11, 2016
Cmenu item url working not correct.
It take my current url and the compose new url, and it is wrong.
For example it looks like like this

http://bukivedi.store/index.php?r=stores/stores/user

But I have only module Stores and want to view result:

http://bukivedi.store/index.php?r=stores/user

My Cmenu array is here:

array('label'=>'Таблицы <span class="caret"></span>', 'url'=>array('#'),
'itemOptions'=>array('class'=>'dropdown'),
'linkOptions'=> array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
),
'items' => array(
array('label' => 'Пользователи', 'url' => array('stores/user')),
array('label' => '', 'url' => array('#'), 'itemOptions'=>array('role'=>'separator', 'class'=>'divider')),
),
),
Read more
Resolved: Get data from table from a list of strings on MySQL
posted by admin on May 6, 2016
How can I query data from a table available on a list of strings?

I only want to get the data from the list of strings.

Example:
Table
ID Name
1 Big
2 Small
3 Extra
4 Orange
5 Drink



List of Strings:
Big
Small
Extra
Read more
Resolved: PHP display an Array under alphabetical letters without key sensitive
posted by admin on April 27, 2016
I have an associative array of tags like

array(11) {
[0]=>
array(4) {
["tag_id"]=>
string(2) "81"
["slug"]=>
string(4) "mag1"
["name"]=>
string(4) "mag1"
["TagsCount"]=>
string(1) "2"
}
[1]=>
array(4) {
["tag_id"]=>
string(2) "85"
["slug"]=>
string(4) "mag2"
["name"]=>
string(4) "mag2"
["TagsCount"]=>
string(1) "1"
}
[2]=>
array(4) {
["tag_id"]=>
string(2) "83"
["slug"]=>
string(4) "mag3"
["name"]=>
string(4) "mag3"
["TagsCount"]=>
string(1) "2"
}
[3]=>
array(4) {
["tag_id"]=>
string(2) "84"
["slug"]=>
string(4) "mag4"
["name"]=>
string(4) "mag4"
["TagsCount"]=>
string(1) "2"
}
. . .
[9]=>
array(4) {
["tag_id"]=>
string(2) "79"
["slug"]=>
string(4) "tag5"
["name"]=>
string(4) "tag5"
["TagsCount"]=>
string(1) "1"
}
[10]=>
array(4) {
["tag_id"]=>
string(2) "80"
["slug"]=>
string(4) "tag6"
["name"]=>
string(4) "tag6"
["TagsCount"]=>
string(1) "1"
}
}

In output I want to see result like

<h3>A</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Animal</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Aqua</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Arthur</a>
</li>
</ol>
<!-- if B not EXIST not show B -->
<h3>C</h3>
<ol>
<li>
<em>tag count</em>
<a href="link to tag">Camel</a>
</li>
<li>
<em>tag count</em>
<a href="link to tag">Crazy</a>
</li>
</ol>
<!-- etc -->
Read more
Wiki: PHP 5 Sorting Arrayswiki
posted by admin on April 16, 2016
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key


Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>


The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>


Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>


The following example sorts the elements of the $numbers array in descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>


Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>


Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>


Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to
arsort($age);
?>
[/php]

Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Read more
Wiki: PHP Dominant Colour Function with GD Librarywiki
posted by admin on April 13, 2016
Someone asked if it was possible to find the dominant colour (the more frequent colour, in %) of an image.

So I read about the GD PHP Library, which enables image support. I found the interesting function imageColorAt() which the colour of a specific pixel. Hence, my function consists of resizing to a known size, 100 x 100, then looping into each pixel, storing it’s colour into an array, then sorting the array to find the more frequent key.

Here’s the result :

function getDominantColour($image, $type='jpeg') // $type can be gif, jpeg or png
{
// This function needs GD Library enabled

eval('$source = imagecreatefrom' . $type . '("' . $image . '");'); // we load the source image
$source_x = imagesx($source); // source's X
$source_y = imagesy($source); // source's Y
$resized_x = 100; // resized image's X
$resized_y = 100; // resized image's Y
$resized= imagecreatetruecolor($resized_x, $resized_y); // we create a blank 100 x 100 image
imagecopyresampled($resized, $source, 0, 0, 0, 0, $resized_x, $resized_y, $source_x, $source_y); // We then copy and resized the source image into the blank image

// Required vars declaration
$colours = array();
$rgb = '';
$index = array();
for ($x=0; $x<100; $x++) // We loop into the pixels X
{
for ($y=0; $y<100; $y++) // We loop into the pixels Y
{
$rgb = imagecolorat($resized, $x, $y); // get the RGB of the pixel
$index = imagecolorsforindex($resized, $rgb); // Get the RGB Index
$key = 'R' . $index['red'] . 'G' . $index['green'] . 'B' . $index['blue']; // Put it into a string
if (empty($colours[$key])) // If it's the first time this colour is encountered
{
$colours[$key] = 1; // We give it a value of 1
} else { // If it's not the first time it is encountered
$colours[$key]++; // We increment its value
}
}
}
arsort($colours, SORT_NUMERIC); // We then sort the colours array so that the first one is the most frequent one
return key($colours); // we return the most frequent value
}


Working with images sure is fun. Looking forward to have more functions with the GD libraryRead more
Wiki: PHP Convert Hex Color to RGB and RGB to Hexwiki
posted by admin on April 13, 2016
Converting certain values back and forth is often necessary when developing functions to help your website work the way you want it to. I needed a function that would convert a hex color to rgb and for some reason it took me a while to figure it out.

function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}


This function works with both shorthand hex codes such as #f00 and longhand hex codes such as #ff0000. It also accepts the number sign (#) just in case. You can see there are two return lines at the end of the function. The first, which is commented out, will return the rgb values separated by a comma. The second, which is the default, will return an array with the rgb values.

So now with this function in place we can use it like so:

$rgb = hex2rgb("#cc0");
print_r($rgb);


The above function would output:

Array ( [0] => 204 [1] => 204 [2] => 0 )


Since we are going one way with this, might as well go the other. Here is a function to convert rgb to a hex color:

function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

return $hex; // returns the hex value including the number sign (#)
}


This function works like so:

$rgb = array( 255, 255, 255 );
$hex = rgb2hex($rgb);
echo $hex;


The above function would output:

#ffffff
Read more
Wiki: Yii 1.1: Add id or class to CMenu itemswiki
posted by admin on April 12, 2016
CMenu comes with a lot of great customization options built in. One of the most used is the class 'active' being added to menu item. But, what if you want to add your own class or id to a menu item ?

It's very easy, you just need to use the 'itemOptions'.

If you want a menu like :

<ul id="myMenu">
<li id="first"><a href="#"><span>First</span></a></li>
<li id="second"><a href="#"><span>Second</span></a></li>
</ul>


Just do :

<?php $this->widget('application.components.MyMenu', array(
'id' => 'myMenu',
'items' => array(
array('label' => 'First', 'url' => array('#'), 'itemOptions'=>array('id' => 'first'), ),
array('label' => 'First', 'url' => array('#'), 'itemOptions'=>array('id' => 'second'), ),
),
));
?>
Read more