I try to create loop for select box
for select time start 8.00 and increasing it continusly
for this matter i found solution from
creating a loop for time incremented by 15 minutes
but i put it into my code under array it shows only one time
but i use
var_dump($timeinoption)
it shows correctly
as
array (size=1)
'8 . 0' => string '8 . 0' (length=5)
array (size=1)
'8 . 15' => string '8 . 15' (length=6)
array (size=1)
'8 . 30' => string '8 . 30' (length=6)
array (size=1)
'8 . 45' => string '8 . 45' (length=6)
array (size=1)
'9 . 0' => string '9 . 0' (length=5
but codeignaiter select box not work;
form_dropdown('timein',$timeinoption,'8.30');
it shows only one time on select box
echo form_label('Time Start','timestart');
for ($i = 8; $i <= 17; $i++)
{
for ($j = 0; $j <= 45; $j+=15)
{
//inside the inner loop
$opti= $i.' . '. $j;
$timeinoption=array($opti=>$opti) ;
}
//inside the outer loop
}
echo form_dropdown('timein',$timeinoption,'8.30');
?>
You are overwriting your array on each loop
$timeinoption=array($opti=>$opti);
So there will only be 1 value in your array.
Try changing to
$timeinoption[$opti]= $opti;
You have to array inside the loop :
$timeinoption = array();
for ($i = 8; $i <= 17; $i++)
{
for ($j = 0; $j <= 45; $j+=15)
{
//inside the inner loop
$opti= $i.' . '. $j;
$timeinoption[$opti] = $opti;
}
//inside the outer loop
}
Easy Way to Put loop Dropdown in CI
$d_opt = array(0 => 'DD');
for($i=1;$i<=31;$i++)
{
if(strlen($i)<2)
{
$i = 0 . $i;
}
$d_opt[$i]=$i;
}
echo form_dropdown('date',$d_opt);
Related
Can someone help me, where should I put my $days[] array so that holidays are not included in this array.
My code is as follows
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie = array($row->start);
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . 'weekdays', $timestamp));
if (in_array($date_tmp , $ferie)){
$jourOuvre++;
$date_tmp = date("Y-m-d", strtotime('+1days', $date_tmp));
}
$days[] = $date_tmp;
$i++;
}
}
var_dump($days);
this code makes :
699:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-21' (length=10)
4 => string '2020-05-22' (length=10)
while 2020-05-21 is a holiday.
NB: $data['ferie'] is an array of holidays.
Thanks, guys. I did it.
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$ferie = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie[] = $row->start;
}
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . ' weekdays' , $timestamp));
if(in_array($date_tmp , $ferie)) {
$jourOuvre++;
} else {
$days[] = $date_tmp;
}
$i++;
}
var_dump($days);
Holidays are out of my array :
C:\wamp\www\HELPJUR\application\controllers\Ticket.php:816:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-22' (length=10)
4 => string '2020-05-25' (length=10)
I have an array of string values called $genderAge that looks like this when echoed:-
F, 0-4, 327607378
M, 0-4, 392700793
F, 15-24, 887438943
M, 15-24, 525132614
M, 25-34, 621410857
So for the above array, $ageRange[0] is "F". $ageRange[1] is "0-4", and so forth.
I want to separate it out into two arrays, $male_array and $female_array with key value pairs. The key should be age range and the value should be cost.
$male_array = [];
$female_array = [];
for ($i = 0; $i < count($genderAge); $i++) {
if ($genderAge[i] == 'M') {
$male_array[$genderAge[i+1]] = $genderAge[i+2];
}
elseif ($genderAge[i] == 'F') {
$female_array[$genderAge[i+1]] = $genderAge[i+2];
}
}
foreach($male_array as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
The above doesn't echo anything. I want it to echo this:
Key=0-4, Value=392700793
Key=15-25, Value=525132614
Key=25-34, Value=621410857
Your $genderAge should be an array of this kind so that you can perform storing of values in the separate variables and you can use it for further purpose.
Proposed Array:
$genderAge = array("F", "0-4", "327607378", "M", "0-4", "392700793", "F", "15-24", "887438943","M", "15-24", "525132614","M","25-34", "621410857");
For Loop Manipulation over the array in order to save the value.
Here you must use $i for increment operator with +1. You must not use the i along separately as you have used.
PHP Code:
<?php
$genderAge = array("F", "0-4", "327607378", "M", "0-4", "392700793", "F", "15-24", "887438943","M", "15-24", "525132614","M","25-34", "621410857");
$male_array = [];
$female_array = [];
for ($i = 0; $i < count($genderAge); $i++) {
if ($genderAge[$i] == 'M') {
$male_array[$genderAge[$i+1]] = $genderAge[$i+2];
}
elseif ($genderAge[$i] == 'F') {
$female_array[$genderAge[$i+1]] = $genderAge[$i+2];
}
}
echo 'M Values'.'<br>';
foreach($male_array as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
echo '<br>';
echo 'F Values'.'<br>';
foreach($female_array as $y=> $y_value) {
echo "Key=" . $y . ", Value=" . $y_value;
echo "<br>";
}
echo '<br>';
?>
Output:
M Values
Key=0-4, Value=392700793
Key=15-24, Value=525132614
Key=25-34, Value=621410857
F Values
Key=0-4, Value=327607378
Key=15-24, Value=887438943
If your array is constantly a combination of 3 pairs : Gender, Age range, Cost, you may want to increment of each 3 pairs in your loop. It will save you from errors relative to out of array indexes :
<?php
$GENDER_MALE = 'M';
$GENDER_FEMALE = 'F';
$genderAge = ['F', '0-4', '327607378', 'M', '0-4', '392700793', 'F', '15-24', '887438943', 'M', '15-24', '525132614', 'M', '25-34', '621410857'];
$male_array = [];
$female_array = [];
$size = count($genderAge);
# Format the male/female array
for( $i = 0; $i < $size; $i += 3 ) {
$gender = $genderAge[$i]; // M or F
$ageRange = $genderAge[$i + 1]; // 0-4, 14-18, ...
$cost = $genderAge[$i + 2]; // 897726, 10927, ...
if( $gender == $GENDER_MALE ) {
$male_array[$ageRange] = $cost;
}
else if( $gender == $GENDER_FEMALE ) {
$female_array[$ageRange] = $cost;
}
}
# Print the male/female formatted array
foreach( $male_array as $key => $value ) {
echo "Age range : $key - cost = $value <br />";
}
?>
Notice in the first for loop the use of $i += 3 which can be read as :
Loop every 3 items on my array
This way, you can apply your index + n relative to the pair you need ($i + 1 for the age range, and so on).
First, You may want to get all those key-value pairs including the Sex in an associative array, instead. Here's how that could be done using for(){} Loop:
<?php
$arr = array("F"," 0-4", "327607378",
"M", "0-4", "392700793",
"F", "15-24", "887438943",
"M", "15-24", "525132614",
"M", "25-34", "621410857");
$all = array();
for($a=0, $b=1, $c=2; $a <= (count($arr)-3); $a+=3, $b+=3, $c+=3){
$temp = array(
"sex" => $arr[$a],
"key" => $arr[$b],
"value" => $arr[$c]
);
$all[] = $temp;
}
var_dump($all);
The call to var_dump($all); yields:
array (size=5)
0 =>
array (size=3)
'sex' => string 'F' (length=1)
'key' => string ' 0-4' (length=4)
'value' => string '327607378' (length=9)
1 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '0-4' (length=3)
'value' => string '392700793' (length=9)
2 =>
array (size=3)
'sex' => string 'F' (length=1)
'key' => string '15-24' (length=5)
'value' => string '887438943' (length=9)
3 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '15-24' (length=5)
'value' => string '525132614' (length=9)
4 =>
array (size=3)
'sex' => string 'M' (length=1)
'key' => string '25-34' (length=5)
'value' => string '621410857' (length=9)
Now, to echo out some data, you can just do something like this:
<?php
foreach($all as $iKey=>$arrKv){
echo "sex : " . $arrKv["sex"] . "<br />" . PHP_EOL;
echo "Key : " . $arrKv["key"] . "<br />" . PHP_EOL;
echo "Value : " . $arrKv["value"] . "<br /><br />" . PHP_EOL;
}
The Compound Result of each of the echo() in the Loop above yields:
sex : F
Key : 0-4
Value : 327607378
sex : M
Key : 0-4
Value : 392700793
sex : F
Key : 15-24
Value : 887438943
sex : M
Key : 15-24
Value : 525132614
sex : M
Key : 25-34
Value : 621410857
I have this loop, which shows books based in a year:
foreach ($cases_result as $case) {
$i = $case->n_ano;
switch ($i) {
case $i >= 2013:
echo $case->book.'<br>';
break;
case $i <= 2012 && $i >= 2011:
echo $case->book.'<br>';
break;
case $i <= 2010 && $i >= 2009:
echo $case->book.'<br>';
break;
case $i <= 2008 && $i >= 2007:
echo $case->book.'<br>';
break;
}
}
Is possible to to add a non-repeatable line to separate each case?
I need to show the year, like so:
2013
- book 1
- book 2
2012
- book 3
- book 4
Here is an excerpt of the array:
array (size=22)
0 =>
object(stdClass)[14]
public 'book' => string 'book 1' (length=6)
public 'n_ano' => string '2013' (length=4)
1 =>
object(stdClass)[15]
public 'book' => string 'book 2' (length=6)
public 'n_ano' => string '2013' (length=4)
2 =>
object(stdClass)[16]
public 'book' => string 'book 3' (length=6)
public 'n_ano' => string '2012' (length=4)
This should work for you:
First I create an array, which uses the year as key and adds the book name to the array, so you then can simply loop over it, e.g.
<?php
foreach($arr as $v)
$data[$v->n_ano][] = $v->book;
foreach($data as $n_ano => $books) {
echo $n_ano . "<br>";
foreach($books as $book)
echo " - " . $book . "<br>";
}
?>
output:
2013
- book 1
- book 2
2012
- book 3
First, sort your array by n_ano. Then you can easily loop trough the sorted result and create a grouped output.
function sortCases($a, $b) {
return $a->n_ano < $b->n_ano;
}
usort($cases_result, "sortCases");
$out = '';
$i = 0;
foreach ($cases_result as $case) {
//If the year is different, print year and assign it so $i
if($i !== intval($case->n_ano)) {
$out .= '<h2>' . $case->n_ano . '</h2>';
$i = intval($case->n_ano);
}
$out .= $case->book . '<br>';
}
print $out;
Let's say I have an array like this :
$intarray = array("300","350","399","650","625","738","983","1200","1050");
how can I display this array to user like this :
Price
_________________
[] 300 - 699 (5)
[] 700 - 999 (2)
[] 1000 - 1500 (2)
Details :
as in the example I wanted to show the user not the whole elements but option to select between them by giving limits low and max limits. So if user select 300 - 699, page display the results between 300-699
That $intarray is generated dynamically so some code must handle the splitting. Array can
have more elements. What I want is divide the numbers like 5 range options to show the user.
Having in mind my comment, I assume you want to count particular ranges.
<?php
$intarray = array("300","350","399","650","625","738","983","1200","1050");
//echo "[] ". $intarray[0]. " - " .$intarray[4][0]."99"; # that was in my comment
$ranges = array(
0 => array(
'min' => 300,
'max' => 699
),
1 => array(
'min' => 700,
'max' => 999
),
2 => array(
'min' => 1000,
'max' => 1500
)
);
foreach ($intarray as $val) {
for ($i = 0; $i < count($ranges); $i++){
if ($val >= $ranges[$i]['min'] && $val <= $ranges[$i]['max']) {
$range_values[$i][] = $val;
}
}
}
var_dump($range_values);
array (size=3)
0 =>
array (size=5)
0 => string '300' (length=3)
1 => string '350' (length=3)
2 => string '399' (length=3)
3 => string '650' (length=3)
4 => string '625' (length=3)
1 =>
array (size=2)
0 => string '738' (length=3)
1 => string '983' (length=3)
2 =>
array (size=2)
0 => string '1200' (length=4)
1 => string '1050' (length=4)
You can use count() in order to display the thing in the brackets
for ($i = 0; $i < count($ranges); $i++) {
$max = max($range_values[$i]);
echo "[] " . min($range_values[$i]) . " - " . $max[0]."99" . " (". count($range_values[$i]).")" . "<br />";
}
[] 300 - 699 (5)
[] 738 - 999 (2)
[] 1050 - 199 (2)
Or just display the ranges (as it was the desired output?) and count($ranges_values) current iteration
for ($i = 0; $i < count($ranges); $i++) {
echo "[] " . $ranges[$i]['min'] . " - " . $ranges[$i]['max'] . " (" . count($range_values[$i]) . ")" . "<br />";
}
[] 300 - 699 (5)
[] 700 - 999 (2)
[] 1000 - 1500 (2)
Try
$intarray = array("300","350","399","650","625","738","983","1200","1050");
sort($intarray);
$result=array();
foreach($intarray as $key=>$val){
switch($val){
case ($val > 300 && $val < 699):
$result[0][] = $val;
break;
case ($val > 700 && $val < 999):
$result[1][] = $val;
break;
case ($val > 1000 && $val < 1500):
$result[2][] = $val;
break;
}
}
demo here
You can do a function that prints and counts the numbers in your range
public function printInRanges($startRange, $endRange)
{
$count = 0;
foreach($element in $array)
{
if($element>=$startRange && $element<=$endRange)
count++;
}
echo $startRange."-".$endRange."(".$count.")";
}
And than you can call this function with whatever ranges you want
Last Edit
if you want to do this for all your array, get your first element value (array[0]) and last element value, and call the function from a loop
$startValue = array[0];
while($startValue + 500 < $endValue)// 500 being the value between ranges like 0-500,500-1000,1000-1500
{
printInRanges($startValue ,$startValue +500);
$startValue+=500;
}
I need help, example, i got array with 4 for team names there.
array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');
I want to make 3 dimensional array where first is a round, second is a match, and the third is teams who plays with eachother, there will be always 2 teams.
the logic must make that all teams must be played with all others and in one round, any team will play only one match, so if we got 5 teams then in some round one team must just wait to the next round.
it must produce something like this:
array
0 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Lust' (length=4)
1 =>
array
0 => string 'Marat and Friends' (length=17)
1 => string 'Pandas of Belgium' (length=17)
1 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Marat and Friends' (length=17)
1 =>
array
0 => string 'Lust' (length=4)
1 => string 'Pandas of Belgium' (length=17)
2 =>
array
0 =>
array
0 => string 'logiX.L4D2' (length=10)
1 => string 'Pandas of Belgium' (length=17)
1 =>
array
0 => string 'Lust' (length=4)
1 => string 'Marat and Friends' (length=17)
It must be work with 2,3,5 ...10 ...12 teams.
I hope that you can help me out, i already spent 1 and half days for that.
Some Googling on Round Robin algorithm PHP gives the following:
http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/
http://www.phpbuilder.com/board/showthread.php?t=10300945
I hope you'll find what you're looking for.
EDIT
Adding my attempt for this, following the round-robin algorithm described on Wikipedia.
If teams number is odd, it adds a team in the array (null value), so you can retrieve the "waiting team" for each round.
<?php
$teams = range('a', 'g');
function make_rounds($teams)
{
$nb_teams = count($teams);
if ($nb_teams % 2 != 0)
{
$teams[] = null;
$nb_teams++;
}
$nb_rounds = $nb_teams - 1;
$nb_matches = $nb_teams / 2;
$rounds = array();
for($round_index = 0; $round_index < $nb_rounds; $round_index++)
{
$matches = array();
for($match_index = 0; $match_index < $nb_matches; $match_index++)
{
if ($match_index == 0)
$first_team = $teams[0];
else
$first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];
$second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];
$matches[] = array($first_team, $second_team);
}
$rounds[] = $matches;
}
return $rounds;
}
print_r(make_rounds($teams));
My version of solution. I would call it brute force. However, it works somehow. (Or it looks like that.)
<?php
$a = array('a','b','c','d','e','f','g');
function do_array($a)
{
$lim = sizeof($a) - 1;
# Create an array of all matches to play.
$cross = array(array());
foreach (range(0,$lim) as $k_row):
foreach (range(0,$lim) as $k_col):
if ($k_row >= $k_col):
$toput = false;
else:
$toput = array($a[$k_row],$a[$k_col]);
endif;
$cross[$k_row][$k_col] = $toput;
endforeach;
endforeach;
$ret = array();
foreach (range(0,$lim) as $k_round):
$round = array();
# $tmp array holds all possible matches
# to play in current round.
$tmp = $cross;
$i = 0;
foreach (range(0,$lim) as $k_row):
foreach (range(0,$lim) as $k_col):
if ($math = $tmp[$k_row][$k_col]):
$cross[$k_row][$k_col] = false;
# These matches are not possible
# in the current round.
foreach (range(0,$lim) as $k):
$tmp[$k][$k_col] = false;
$tmp[$k][$k_row] = false;
$tmp[$k_col][$k] = false;
$tmp[$k_row][$k] = false;
endforeach;
$round[] = $math;
endif;
endforeach;
endforeach;
if ($round):
$ret[] = $round;
endif;
endforeach;
return $ret;
}
print_r (do_array($a));
?>