Jump to content

How to disinclude a value from a loop?


Solarpitch

Recommended Posts

Hey guys,

 

I have a little bit more of a complex problem below. I basically have this script that will take two time values, a start time and an end time and will then loop 10min intervals between both times and output the result...

 


<?php

$start = "7:00";
$finish = "12:00";

$st = strtotime(date('Y-m-d') . $start);
$en = strtotime(date('Y-m-d') . $finish);
$int = 10 * 60;

$myinfo[0] = "<select name=\"time\" id='listboxes'>";
for ($i = $st; $i <= $en; $i += $int) {

    $time = date('h:i a',$i) . "<br>\n";
	$myinfo[0] .= "<option value=\"".$time."\">".$time."</option>";
}

?>

this will display a list box with 7:00, 7:10, 7:20, 7:30 and so on. What I want to do it to disinclude certain times from the loop. So if the value to disinclude is "7:10" I dont want that time to be in the list box. 

<?php

        $disinclude = "7:10"; --> needs to be a value like this.

        ........

?>


Link to comment
https://forums.phpfreaks.com/topic/96687-how-to-disinclude-a-value-from-a-loop/
Share on other sites

Basically just have an if statement to control it..

 

*Note code below was written online and is untested

 

here is an example for a range of dates


<?php

$start = "7:00";
$finish = "12:00";

$st = strtotime(date('Y-m-d') . $start);
$en = strtotime(date('Y-m-d') . $finish);

//create a exclude range
$excludeStart = "7:10"
$excludeEnd = "7:40"

$eStart = strtotime(date('Y-m-d') . $excludeStart;
$eEnd = strtotime(date('Y-m-d') . $excludeEnd;

$int = 10 * 60;

$myinfo[0] = "<select name=\"time\" id='listboxes'>";
for ($i = $st; $i <= $en; $i += $int) {

    $time = date('h:i a',$i) . "<br>\n";
    //if NOT in the exclude range then add a value
    if( !($time > $eStart && $time < $eEnd) ) 
    {
	$myinfo[0] .= "<option value=\"".$time."\">".$time."</option>";
    }
}

?>

 

 

another example for singles


<?php

$start = "7:00";
$finish = "12:00";

$st = strtotime(date('Y-m-d') . $start);
$en = strtotime(date('Y-m-d') . $finish);

//create a exclude single/s
$exclude[] = "7:10"
$exclude[] = "7:40"

$int = 10 * 60;

$myinfo[0] = "<select name=\"time\" id='listboxes'>";
for ($i = $st; $i <= $en; $i += $int) {

    $time = date('h:i a',$i) . "<br>\n";
    //if NOT in the exclude array then add a value
    if( !in_array($time, $exclude) ) 
    {
	$myinfo[0] .= "<option value=\"".$time."\">".$time."</option>";
    }
}

?>

Hi MadTechie,

 

I've tried both of the ways you suggested for this but the listbox just appears with the all the times in it and none excluded. Here's the full function...

 

<?php

function check_teetimes($teedate, $players, $membertype)
{

dbconnect();

$myinfo = array();

$sql = "select * from golfpro_time_sheet where realdate= '".$teedate."' and slots < ".$players."";
$result = mysql_query($sql) or die(mysql_error());

	while(($row = mysql_fetch_row($result)) != false) {
	$myinfo[1] = $row[1];
}


$start = "7:00";
$finish = "12:00";

$st = strtotime(date('Y-m-d') . $start);
$en = strtotime(date('Y-m-d') . $finish);


$exclude[] = "7:10";
$exclude[] = "7:40";

$int = 10 * 60;

$myinfo[0] = "<select name=\"time\" id='listboxes'>";
for ($i = $st; $i <= $en; $i += $int) {

    $time = date('h:i a',$i) . "<br>\n";

    //if NOT in the exclude array then add a value
    if( !in_array($time, $exclude) ) 
    {
	$myinfo[0] .= "<option value=\"".$time."\">".$time."</option>";
    }
}

$myinfo[0] .= "</select>";

return $myinfo;
}

?>

change

$time = date('h:i a',$i) . "<br>\n";

to

$time = date('h:i a',$i);

 

as that will messup the compare

you can add it back in after the compare

    if( !in_array($time, $exclude) ) 
    {
	$time = date('h:i a',$i) . "<br>\n";
	$myinfo[0] .= "<option value=\"".$time."\">".$time."</option>";
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.