[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP how to stop for statement after value is hit

Posted on 2013-01-09
8
Medium Priority
?
306 Views
Last Modified: 2013-01-10
Hello,
I have a calendar controls that correctly assigns a week (1-52) number to the date that is picked in a start date and end date control.

And in my database I have 52 price fields (one for each week).

I am trying to display the pricing for the weeks that are picked.
For example if the start week is 40 and the end week is 45 it displays the pricing for weeks 40, 41, 42, 43, 44 and 45.

I have that all working accurately through the code:
for($i=$startweek;$i<=$endingweek;$i++)
	{
 echo "<td>Week ", $i, ": $<b>", $row["price$i"], "</b></td>";
 }

Open in new window


The problem is when the start date is larger then the end date.
For example if the start week is 52 (last week of the year) and the end week is 1 (first week of the next year).

I have the code:
 for($i=$startweek;$i>=$endingweek;$i++)
	{
 echo "<td>Week ", $i, ": $<b>", $row["price$i"], "</b></td>";
 }

Open in new window


It will bring back the correct information for week 52 but then goes into a loop adding week 53, 54, 55, etc... with no values.

How do I get it to stop at week 52 and also bring back the values for weeks lower then 52 (ie week #1 in the example).

I hope this makes sense.

Thank you in advance for your help.
0
Comment
Question by:Razzmataz73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 38761517
There is an ISO standard for week numbers.  You can use a combination of date() and strtotime() to get the week number.  These functions are documented in the PHP.net web site, and also written about in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Try printing out the week number according to the PHP date() function.  You will find that a "homegrown" week number is not a useful way of dealing with this information.  If you still have trouble with this, please post back with some test data and I'll be glad to show you a solution.  It may take a day or two because I am teaching full time right now, but there is a good solution and I will be happy to show you how we would do it.

Best regards, ~Ray
0
 

Author Comment

by:Razzmataz73
ID: 38761538
The week number comes back with no problem.

My issue is how do I write a query if the start week number is less then the end date number.

For example 49 and 1.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38761546
You do not write a query that way.  You use the week number and the year to produce a lower DATETIME value and an upper DATETIME value. Then you write a query using the BETWEEN subclause of the WHERE clause.  The article either tells it explicitly or give strong suggestions about it.  You want to convert the year + week number to an ISO-8601 DATETIME string, and your query will use the resulting DATETIME strings to look into the data base table.
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:Razzmataz73
ID: 38761557
Duh!  Don't know why that didn't occur to me.
That sounds perfect!
I will try it out first thing tomorrow and let you know how it goes.
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 1400 total points
ID: 38761742
Not sure if you can apply this, but the Math of having a higher number first, is that you roll (for loop) the High to the Highest first and then roll from the lowest to the Lower number as this code which I ran , and it does what you describe-

$array52 = range(0, 53);
$startweek = 50;
$endingweek = 2;

if ($startweek == $endingweek)
{
	echo "Week ", $startweek, ': $<b>price', $array52[$startweek], "</b><br />";
} elseif ($startweek < $endingweek)
{
	for($i=$startweek; $i<=$endingweek; $i++) echo "Week ", $i, ': $<b>price', $array52[$i], "</b><br />";
} else
{
	for($i=$startweek; $i<=52; $i++) echo "Week ", $i, ': $<b>price', $array52[$i], "</b><br />";
	for($i=1; $i<=$endingweek; $i++) echo "Week ", $i, ': $<b>price', $array52[$i], "</b><br />";
}

Open in new window

0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38761804
simple

if($i==52) exit;
0
 
LVL 9

Expert Comment

by:rinfo
ID: 38762301
Firstly you should not allow  to select next week which is less then start week if you are taking into consideration weeks for your query and it is assumed that you need data for a
particular year only.
In case you intend to extend the query range spread over multiple and i assume consecutive
years then you should set a variable for currYear which should be year for the start week.
And if start week is more then end week it would be assumed that those weeks belong to
next year.
In such eventualities it would be advisable to do it in multiple steps. One each for each year.
Like one starting form 50th week and going upto 52nd week and then another query which starts at 1st week of the next year and going up to end week.
Naturally you would need to involve year in your query to get correct results.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38762523
Please see http://www.laprbass.com/RAY_week_number.php (and give yourself some time to read the code and output carefully -- it's subtle).

This program shows how to use the week number.  You can use it in combination with strtotime() and date() to come up with the appropriate DATETIME values to use in the query.

<?php // RAY_week_number.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED SINCE PHP VERSION 5.1+
date_default_timezone_set('America/Chicago');

// WHAT IS THE WEEK NUMBER?
echo "WE ARE NOW IN WEEK " . date('W');
echo PHP_EOL;

$now = date('c', strtotime( date('Y-01-01') . ' + ' . date('W') . ' WEEKS' ));
var_dump($now);

// SHOW SOME "INTERESTING" THINGS ABOUT WEEK NUMBER
$years = array
( 2006
, 2007
, 2008
, 2009
, 2010
, 2011
, 2012
, 2013
, 2014
, 2015
)
;

foreach ($years as $y)
{
    $c = date('c', strtotime("$y-01-01"));
    $w = date('W', strtotime("$y-01-01"));
    echo PHP_EOL . "ON $c IT IS WEEK NUMBER $w";
    if ($w > '01')
    {
        $str = "$c NEXT MONDAY";
        $tsp = strtotime($str);
        $nur = date('c', $tsp);
        $nuw = date('W', $tsp);
        echo " AND ON $nur IT IS WEEK NUMBER $nuw";
    }
}

foreach ($years as $y)
{
    $str = "$y" . '-01-01' . ' NEXT MONDAY';
    $tsp = strtotime($str);
    $nur = date('r', $tsp);
    echo PHP_EOL . "$str = $nur";
}

// NO DIFFERENCE BETWEEN "NEXT" AND "FIRST"
foreach ($years as $y)
{
    $str = "$y" . '-01-01' . ' FIRST MONDAY';
    $tsp = strtotime($str);
    $nur = date('r', $tsp);
    echo PHP_EOL . "$str = $nur";
}

Open in new window

Best regards, ~Ray
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question