9 Replies - 579 Views - Last Post: 03 September 2012 - 09:53 AM Rate Topic: -----

#1 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

[QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 05:01 AM

Hi, if I have the next data in a table (mysql). With this appearance.

 |         C
 |         █ D   F     ... █
 |---------█-█---█-----█---█----------- <- $limit
 |         █ █ E █     █   █
 | A     B █ █ █ █     █   █
 | █ █   █ █ █ █ █ G H █ █ █
 | █ █ █ █ █ █ █ █ █ █ █ █ █ ...
 --------------------------------------


Intervals: A-B, C-D, E, F, G-H, etc.

I have the next query.

$result = mysql_query('SELECT starttime, endtime, kw FROM importkw_model1 WHERE (starttime BETWEEN ' . $sttimestamp . ' AND ' . $ettimestamp . ') AND kw > ' . $limit . ' ORDER BY kw', $link);


How can I make this intervals? Each bar is one minute, $ sttimestamp is stored in the database as seconds. As each bar is 60 seconds longer than the previous one. What I thought is to create a multidimensional array and go save them. But I do not do well...

$periodInterval = array();
            $number = 0;
            $element = 0;
            
            for($i = $sttimestamp; $i <= $ettimestamp; $i+60){
                $row = mysql_fetch_assoc($result);
                if($row['starttime'] == $i){
                    $periodInterval[$number][$element] = $row;
                    $element++;
                    }
                else if ($row['starttime'] != $i){
                    $element = 0;
                    $number++;
                    $periodInterval[$number][$element] = $row;
                }
            }



Any help will be important. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: [QUESTION] How to make Intervals from a query?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • Posts: 10,788
  • Joined: 08-August 08

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 05:50 AM

What is an interval?

Some things you need to study:
Prepared statements.
for loops
Was This Post Helpful? 0
  • +
  • -

#3 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 05:54 AM

An interval is, for example:

If I have A,B,C,D,E,F,G

An interval of this group of letters is [A-D], which contain A,B,C and D.
Was This Post Helpful? 0
  • +
  • -

#4 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 106
  • Joined: 31-July 12

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 05:56 AM

Firstly, I got to admit that you lost me a bit when I tried to read your question. Actually, I'm not sure if indeed I managed to understand what are you trying to do, but since you say you have an issue, I'll start by warning you about the way you increment - or not - that $i var.

View Postrumbitas, on 02 September 2012 - 02:01 PM, said:

for($i = $sttimestamp; $i <= $ettimestamp; $i+60){

If you want to add + 60 on each loop, you should use something like: $i += 60, not $i+60

This post has been edited by StefanOnRails: 02 September 2012 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • Posts: 10,788
  • Joined: 08-August 08

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 06:10 AM

Is this what you're trying to do?
<?php
$intervals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for($i = 0; $i < 26; $i++) {
	$letter = substr($intervals,$i,1); // Just picking each possible letter to test the code below.
	
	$intervalnumber = (int)(strpos($intervals,$letter)/2);
	$interval = substr($intervals,2*$intervalnumber,1)."-".substr($intervals,2*$intervalnumber+1,1);
	echo $letter." is in interval: ".$interval."<br>";
}

Was This Post Helpful? 0
  • +
  • -

#6 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 06:27 AM

Yes, it is the way. I have a table with a start time (in seconds) and a unit. I want to choose a period, a start row and end row of the table.

If I have the next serie of numbers:

1, 2, 4, 1, 0, 5, 6, 2, 8, 1, 0, 1

I want to do as many intervals as possible. If I put a limit = 4 now. I will get the next intervals.

IntervalA = [1, 2]

As exceeds the limit, we change the interval

IntervalB = [4]

As back down below the limit, we change interval again.

IntervalC = [1, 0]
IntervalD = [5, 6]
IntervalE = [2]
IntervalF = [8]
IntervalG =[1, 0, 1]

I do not know if I'm explaining well. Sorry.
Was This Post Helpful? 0
  • +
  • -

#7 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 106
  • Joined: 31-July 12

Re: [QUESTION] How to make Intervals from a query?

Posted 02 September 2012 - 09:59 AM

Hope this will help you:
$numbers = array(1, 2, 4, 1, 0, 5, 6, 2, 8, 1, 0, 1);
$intervals = array();

$count = count($numbers);
$limit = 4;
$letter = 'A';
$toggle = ($numbers[0] >= $limit) ? true : false;
$intervals['A'] = array($numbers[0]);

for($i = 1; $i < $count; $i++){
   if($numbers[$i] >= $limit && !$toggle){
	$letter++;
	$toggle = true;
	$intervals[$letter] = array();
   }else if($numbers[$i] < $limit && $toggle){
	$letter++;
	$toggle = false;
	$intervals[$letter] = array();
   }
	$intervals[$letter][] = $numbers[$i];
}
echo '<pre>',print_r($intervals,true),'</pre>';
Output:
(
    [A] => Array
        (
            [0] => 1
            [1] => 2
        )

    [B] => Array
        (
            [0] => 4
        )

    [C] => Array
        (
            [0] => 1
            [1] => 0
        )

    [D] => Array
        (
            [0] => 5
            [1] => 6
        )

    [E] => Array
        (
            [0] => 2
        )

    [F] => Array
        (
            [0] => 8
        )

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

)
Fun fact: I was planning to post this earlier, but my internet connection played a prank on me :D
Was This Post Helpful? 1
  • +
  • -

#8 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Re: [QUESTION] How to make Intervals from a query?

Posted 03 September 2012 - 03:36 AM

View PostStefanOnRails, on 02 September 2012 - 05:59 PM, said:

Hope this will help you:
$numbers = array(1, 2, 4, 1, 0, 5, 6, 2, 8, 1, 0, 1);
$intervals = array();

$count = count($numbers);
$limit = 4;
$letter = 'A';
$toggle = ($numbers[0] >= $limit) ? true : false;
$intervals['A'] = array($numbers[0]);

for($i = 1; $i < $count; $i++){
   if($numbers[$i] >= $limit && !$toggle){
	$letter++;
	$toggle = true;
	$intervals[$letter] = array();
   }else if($numbers[$i] < $limit && $toggle){
	$letter++;
	$toggle = false;
	$intervals[$letter] = array();
   }
	$intervals[$letter][] = $numbers[$i];
}
echo '<pre>',print_r($intervals,true),'</pre>';
Output:
(
    [A] => Array
        (
            [0] => 1
            [1] => 2
        )

    [B] => Array
        (
            [0] => 4
        )

    [C] => Array
        (
            [0] => 1
            [1] => 0
        )

    [D] => Array
        (
            [0] => 5
            [1] => 6
        )

    [E] => Array
        (
            [0] => 2
        )

    [F] => Array
        (
            [0] => 8
        )

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

)
Fun fact: I was planning to post this earlier, but my internet connection played a prank on me :D


This will help me so much. Thanks StefanOnRails.
Was This Post Helpful? 0
  • +
  • -

#9 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Re: [QUESTION] How to make Intervals from a query?

Posted 03 September 2012 - 07:55 AM

View PostStefanOnRails, on 02 September 2012 - 05:59 PM, said:



StefanOnRails, if I have a query instead of an array?
$resultIntervals = mysql_query('SELECT starttime, value FROM minimo WHERE starttime BETWEEN ' . $sttimestamp . ' AND ' . $ettimestamp, $link);
        

I have to do this with the value variable. But I have not entirely clear how to manage this instead of number array, because I get the values with mysql_fetch_assoc.

Thanks soo much

This post has been edited by rumbitas: 03 September 2012 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#10 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 106
  • Joined: 31-July 12

Re: [QUESTION] How to make Intervals from a query?

Posted 03 September 2012 - 09:53 AM

View Postrumbitas, on 03 September 2012 - 04:55 PM, said:

I have to do this with the value variable.

So what are you saying is you need to find a way to pass all the value(s) inside your query to an array?
Try this:
$query = mysql_query("...");
$numbers = array();
while($row = mysql_fetch_assoc($query)) $numbers[] = $row['value'];
This will pass all the values from your query into your $numbers array. Then you can use the algorithm I posted yesterday to get your intervals.
By the way, I suggest checking CTphpnwb's link on prepared statements because those functions you use to deal with your database are pretty obsolete (I only used them in order to fit your example).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1