PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Finding next recored in a array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Finding next recored in a array

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 22h24   #1
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Finding next recored in a array

$Campaign_array| = array('0','1','3','5','8','15','25');|
I know that I can find the next recored in a array using next. What I do
not understand is if I know the last number was say 5 how do I tell the
script that that is the current number so I can select the next record
||
  Réponse avec citation
Vieux 17/09/2007, 00h09   #2
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:
> $Campaign_array| = array('0','1','3','5','8','15','25');|
> I know that I can find the next recored in a array using next. What I do
> not understand is if I know the last number was say 5 how do I tell the
> script that that is the current number so I can select the next record
> ||
>


I think you'll need your own function for this. Pass in the array and
loop through it until you find the key, increment that, ensure that
there is another value with that key, and return the key (or the value).

(untested)

function nextInArray($arr, $val)
{
$next_key = NULL;

for ($i = 0; $i < sizeof($arr);$i++)
{
if ($arr[$i] == $val)
{
$next_key = ++$i;
break;
}
}

// return the key:
return (array_key_exists($next_key) ? $next_key : NULL);

// or the value:
return (array_key_exists($next_key) ? $arr[$next_key] : NULL);

}

However, in your example, you're searching for the key that points to
the value '5'. What if the value '5' occurs more than once?

brian
  Réponse avec citation
Vieux 17/09/2007, 00h35   #3
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

brian wrote:
> Richard Kurth wrote:
>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>> I know that I can find the next recored in a array using next. What I
>> do not understand is if I know the last number was say 5 how do I
>> tell the script that that is the current number so I can select the
>> next record
>> ||
>>

>
> I think you'll need your own function for this. Pass in the array and
> loop through it until you find the key, increment that, ensure that
> there is another value with that key, and return the key (or the value).
>
> (untested)
>
> function nextInArray($arr, $val)
> {
> $next_key = NULL;
>
> for ($i = 0; $i < sizeof($arr);$i++)
> {
> if ($arr[$i] == $val)
> {
> $next_key = ++$i;
> break;
> }
> }
>
> // return the key:
> return (array_key_exists($next_key) ? $next_key : NULL);
>
> // or the value:
> return (array_key_exists($next_key) ? $arr[$next_key] : NULL);
>
> }
>
> However, in your example, you're searching for the key that points to
> the value '5'. What if the value '5' occurs more than once?
>
> brian
>

In my script the value of 5 will not reoccur the numbers are number of
days from 0 up to 30 days.

Thanks for the function
  Réponse avec citation
Vieux 17/09/2007, 01h21   #4
Rick Pasotto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
> Richard Kurth wrote:
>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>> I know that I can find the next recored in a array using next. What I do
>> not understand is if I know the last number was say 5 how do I tell the
>> script that that is the current number so I can select the next record
>> ||

>
> I think you'll need your own function for this.


Nope. Just use array_search().

$k = array_search('5',$Campaign_array);
if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }

> Pass in the array and loop
> through it until you find the key, increment that, ensure that there is
> another value with that key, and return the key (or the value).
>
> (untested)
>
> function nextInArray($arr, $val)
> {
> $next_key = NULL;
>
> for ($i = 0; $i < sizeof($arr);$i++)
> {
> if ($arr[$i] == $val)
> {
> $next_key = ++$i;
> break;
> }
> }
>
> // return the key:
> return (array_key_exists($next_key) ? $next_key : NULL);
>
> // or the value:
> return (array_key_exists($next_key) ? $arr[$next_key] : NULL);
>
> }
>
> However, in your example, you're searching for the key that points to the
> value '5'. What if the value '5' occurs more than once?


>From the docs:


"If needle is found in haystack more than once, the first matching key
is returned. To return the keys for all matching values, use
array_keys() with the optional search_value parameter instead."

--
"Now what liberty can there be where property is taken without consent??"
-- Samuel Adams
Rick Pasotto rick@niof.net http://www.niof.net
  Réponse avec citation
Vieux 17/09/2007, 01h50   #5
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Rick Pasotto wrote:
> On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
>
>> Richard Kurth wrote:
>>
>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>> I know that I can find the next recored in a array using next. What I do
>>> not understand is if I know the last number was say 5 how do I tell the
>>> script that that is the current number so I can select the next record
>>> ||
>>>

>> I think you'll need your own function for this.
>>

>
> Nope. Just use array_search().
>
> $k = array_search('5',$Campaign_array);
> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>
>

I tried this and it gives me nothing back. It should give me a 8

$Campaign_array= array('0','1','3','5','8','15','25');
$val="5";

$k = array_search($val,$Campaign_array);
if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
  Réponse avec citation
Vieux 17/09/2007, 02h04   #6
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:
> Rick Pasotto wrote:
>> On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
>>
>>> Richard Kurth wrote:
>>>
>>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>>> I know that I can find the next recored in a array using next. What
>>>> I do not understand is if I know the last number was say 5 how do I
>>>> tell the script that that is the current number so I can select the
>>>> next record
>>>> ||
>>>>
>>> I think you'll need your own function for this.
>>>

>>
>> Nope. Just use array_search().
>>
>> $k = array_search('5',$Campaign_array);
>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>
>>

> I tried this and it gives me nothing back. It should give me a 8
>
> $Campaign_array= array('0','1','3','5','8','15','25');
> $val="5";
>
> $k = array_search($val,$Campaign_array);
> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>

I figured out way it was not working $k + 1 > count needed to be $k +
1 < count
But now it works perfect
  Réponse avec citation
Vieux 17/09/2007, 02h30   #7
Rick Pasotto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote:
> Richard Kurth wrote:
>> Rick Pasotto wrote:
>>> On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
>>>
>>>> Richard Kurth wrote:
>>>>
>>>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>>>> I know that I can find the next recored in a array using next. What I
>>>>> do not understand is if I know the last number was say 5 how do I tell
>>>>> the script that that is the current number so I can select the next
>>>>> record
>>>>> ||
>>>>>
>>>> I think you'll need your own function for this.
>>>>
>>>
>>> Nope. Just use array_search().
>>>
>>> $k = array_search('5',$Campaign_array);
>>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>>
>>>

>> I tried this and it gives me nothing back. It should give me a 8
>>
>> $Campaign_array= array('0','1','3','5','8','15','25');
>> $val="5";
>>
>> $k = array_search($val,$Campaign_array);
>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>

> I figured out way it was not working $k + 1 > count needed to be $k + 1 <
> count


Yup. Sorry 'bout that.

> But now it works perfect


--
"Our fatigue is often caused not by work, but by worry, frustration and
resentment." -- Dale Carnegie
Rick Pasotto rick@niof.net http://www.niof.net
  Réponse avec citation
Vieux 17/09/2007, 09h24   #8
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Rick Pasotto wrote:
> On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote:
>> Richard Kurth wrote:
>>> Rick Pasotto wrote:
>>>> On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
>>>>
>>>>> Richard Kurth wrote:
>>>>>
>>>>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>>>>> I know that I can find the next recored in a array using next. What I
>>>>>> do not understand is if I know the last number was say 5 how do I tell
>>>>>> the script that that is the current number so I can select the next
>>>>>> record
>>>>>> ||
>>>>>>
>>>>> I think you'll need your own function for this.
>>>>>
>>>> Nope. Just use array_search().
>>>>
>>>> $k = array_search('5',$Campaign_array);
>>>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>>>
>>>>
>>> I tried this and it gives me nothing back. It should give me a 8
>>>
>>> $Campaign_array= array('0','1','3','5','8','15','25');
>>> $val="5";
>>>
>>> $k = array_search($val,$Campaign_array);
>>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>>

>> I figured out way it was not working $k + 1 > count needed to be $k + 1 <
>> count

>
> Yup. Sorry 'bout that.
>
>> But now it works perfect

>


if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }

is very over-the-top. What's wrong with
if (isset($Campaign_array[$k+1]) { echo $Campaign_array[$k + 1]; }

Not to mention I don't really like the whole way this has been handled,
I'm sure there are better ways, but for this we'd need to know more
about what you're doing and what you're trying to achieve, which we
obviously don't.

- Tul
  Réponse avec citation
Vieux 17/09/2007, 14h33   #9
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:

>$Campaign_array| = array('0','1','3','5','8','15','25');|
>I know that I can find the next recored in a array using next. What
>I do not understand is if I know the last number was say 5 how do I
>tell the script that that is the current number so I can select the
>next record


What the next record?

Try:

$array = array('0','1','3','5','8','15','25');
$val = "5";

echo($array[array_search($val, $array)+1]);

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 17/09/2007, 16h52   #10
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

tedd wrote:
> Richard Kurth wrote:
>
>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>> I know that I can find the next recored in a array using next. What I
>> do not understand is if I know the last number was say 5 how do I tell
>> the script that that is the current number so I can select the next
>> record

>
>
> What the next record?
>
> Try:
>
> $array = array('0','1','3','5','8','15','25');
> $val = "5";
>
> echo($array[array_search($val, $array)+1]);
>
> Cheers,
>
> tedd


Not quite:

$array = array('0','1','3','5','8','15','25');
$val = "25";

echo($array[array_search($val, $array)+1]);

Notice: Undefined offset: 7 ...

brian
  Réponse avec citation
Vieux 17/09/2007, 19h50   #11
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

M. Sokolewicz wrote:
> Rick Pasotto wrote:
>> On Sun, Sep 16, 2007 at 06:04:45PM -0700, Richard Kurth wrote:
>>> Richard Kurth wrote:
>>>> Rick Pasotto wrote:
>>>>> On Sun, Sep 16, 2007 at 07:09:02PM -0400, brian wrote:
>>>>>
>>>>>> Richard Kurth wrote:
>>>>>>
>>>>>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>>>>>> I know that I can find the next recored in a array using next.
>>>>>>> What I do not understand is if I know the last number was say 5
>>>>>>> how do I tell the script that that is the current number so I
>>>>>>> can select the next record
>>>>>>> ||
>>>>>>>
>>>>>> I think you'll need your own function for this.
>>>>>>
>>>>> Nope. Just use array_search().
>>>>>
>>>>> $k = array_search('5',$Campaign_array);
>>>>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k +
>>>>> 1]; }
>>>>>
>>>>>
>>>> I tried this and it gives me nothing back. It should give me a 8
>>>>
>>>> $Campaign_array= array('0','1','3','5','8','15','25');
>>>> $val="5";
>>>>
>>>> $k = array_search($val,$Campaign_array);
>>>> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>>>>
>>> I figured out way it was not working $k + 1 > count needed to be
>>> $k + 1 < count

>>
>> Yup. Sorry 'bout that.
>>
>>> But now it works perfect

>>

>
> if ($k + 1 > count($Campaign_array)) { echo $Campaign_array[$k + 1]; }
>
> is very over-the-top. What's wrong with
> if (isset($Campaign_array[$k+1]) { echo $Campaign_array[$k + 1]; }
>
> Not to mention I don't really like the whole way this has been
> handled, I'm sure there are better ways, but for this we'd need to
> know more about what you're doing and what you're trying to achieve,
> which we obviously don't.

What I am trying to is get all the days from a table where email
campaign = number. and then look at the last day that was sent and find
it in the list and get the next day that follows that day. At this point
the script below is not working. So if you have any Ideas that would be
a better way of doing this please let me know.


$query = "SELECT day FROM emailcampaign where campaign_id =
'$emailcampaign' AND member_id = '$members_id'";
$DB_Change_Campaign_Results = safe_query($query);
for ($i=0; $i < mysql_num_rows($DB_Change_Campaign_Results); $i++) {
$Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results);
$Campaign = $Change_Campaign_row['day'];
$Campaign_array[$Campaign] = $Change_Campaign_row;
}
$k = array_search($val,$Campaign_array);
if ($k + 1 < count($Campaign_array)){ echo $Campaign_array[$k + 1]; }
  Réponse avec citation
Vieux 17/09/2007, 20h04   #12
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:


$query = " SELECT day
FROM emailcampaign
where campaign_id = '$emailcampaign'
AND member_id = '$members_id'
";
$DB_Change_Campaign_Results = safe_query($query);

while ( $row = mysql_fetch_array($DB_Change_Campaign_Results) ) {
$Campaign_array[$row['day']] = $row;
}

# At this point you have arrays as values for your $Campaign_array
# So, unless $k is an array that matches a sub array of $Campaign_array
# you're never going to get a match

$k = array_search($val,$Campaign_array);

# What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...)
# Before I go any further I will need to know the above information.

if ( ($k + 1) < count($Campaign_array) ) {
echo $Campaign_array[$k + 1];
}


--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Réponse avec citation
Vieux 17/09/2007, 20h24   #13
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Jim Lucas wrote:
> Richard Kurth wrote:
>
>
> $query = " SELECT day
> FROM emailcampaign
> where campaign_id = '$emailcampaign'
> AND member_id = '$members_id'
> ";
> $DB_Change_Campaign_Results = safe_query($query);
>
> while ( $row = mysql_fetch_array($DB_Change_Campaign_Results) ) {
> $Campaign_array[$row['day']] = $row;
> }
>
> # At this point you have arrays as values for your $Campaign_array
> # So, unless $k is an array that matches a sub array of $Campaign_array
> # you're never going to get a match
>
> $k = array_search($val,$Campaign_array);
>
> # What is $k at this point? An int (1, 2, 3, etc...) , string
> (Sunday, Monday, etc...)
> # Before I go any further I will need to know the above information.
>
> if ( ($k + 1) < count($Campaign_array) ) {
> echo $Campaign_array[$k + 1];
> }
>
>

This is what I get if I run the above script.
From a var_dump($Campaign_array); I get

*array*
0 =>
*array*
0 => string '0' /(length=1)/
'day' => string '0' /(length=1)/
1 =>
*array*
0 => string '1' /(length=1)/
'day' => string '1' /(length=1)/
3 =>
*array*
0 => string '3' /(length=1)/
'day' => string '3' /(length=1)/
6 =>
*array*
0 => string '6' /(length=1)/
'day' => string '6' /(length=1)/
9 =>
*array*
0 => string '9' /(length=1)/
'day' => string '9' /(length=1)/
12 =>
*array*
0 => string '12' /(length=2)/
'day' => string '12' /(length=2)/
15 =>
*array*
0 => string '15' /(length=2)/
'day' => string '15' /(length=2)/
20 =>
*array*
0 => string '20' /(length=2)/
'day' => string '20' /(length=2)/
25 =>
*array*
0 => string '25' /(length=2)/
'day' => string '25' /(length=2)/
30 =>


*array*
0 => string '30' /(length=2)/
'day' => string '30' /(length=2)/

From a $val="5";
$k = array_search($val,$Campaign_array);
var_dump($k); I get

boolean false
  Réponse avec citation
Vieux 17/09/2007, 20h51   #14
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

REVISED

Try this

<?php

$query = " SELECT day
FROM emailcampaign
WHERE campaign_id = '$emailcampaign'
AND member_id = '$members_id'
";

$DB_Change_Campaign_Results = safe_query($query);

##
## NOTICE: changed from array to assoc
##
$Campaign_array = array();
while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) {
## Switched to an indexed array with the day as the value instead of the key
$Campaign_array[] = $row['day'];
}

# At this point you have arrays as values for your $Campaign_array
# So, unless $k is an array that matches a sub array of $Campaign_array
# you're never going to get a match

$k = array_search($val,$Campaign_array);

# What is $k at this point? An int (1, 2, 3, etc...) , string (Sunday, Monday, etc...)
# Before I go any further I will need to know the above information.
if ( isset($Campaign_array[($k + 1)])) {
echo $Campaign_array[($k + 1)];
} else {
echo 'Not found';
}


--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Réponse avec citation
Vieux 17/09/2007, 20h57   #15
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:
>
> What I am trying to is get all the days from a table where email
> campaign = number. and then look at the last day that was sent and find
> it in the list and get the next day that follows that day. At this point
> the script below is not working. So if you have any Ideas that would be
> a better way of doing this please let me know.
>
>
> $query = "SELECT day FROM emailcampaign where campaign_id =
> '$emailcampaign' AND member_id = '$members_id'";
> $DB_Change_Campaign_Results = safe_query($query);
> for ($i=0; $i < mysql_num_rows($DB_Change_Campaign_Results); $i++) {
> $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results);
> $Campaign = $Change_Campaign_row['day'];
> $Campaign_array[$Campaign] = $Change_Campaign_row;
> }
> $k = array_search($val,$Campaign_array);
> if ($k + 1 < count($Campaign_array)){ echo $Campaign_array[$k + 1]; }
>



You can simplify this greatly:

$Campaign_array = Array();

$DB_Change_Campaign_Results = safe_query($query);

while ($row = mysql_fetch_array($DB_Change_Campaign_Results))
{
array_push($Campaign_array, $row['day']);
}

Because you're only selecting day from the table, your row data consists
of only one column. You're repeating stuff unnecessarily by doing this:

$Campaign = $Change_Campaign_row['day'];
$Campaign_array[$Campaign] = $Change_Campaign_row;

IOW, for the row that contains '5', you'd end up with $Campaign_array[5]
== an array that essentially just points to the value '5'.

I also noticed that your dump shows that the days begin with 0. If these
are calendar days you're adding a further layer of complexity.

brian
  Réponse avec citation
Vieux 17/09/2007, 21h08   #16
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Jim Lucas wrote:
> REVISED
>
> Try this
>
> <?php
>
> $query = " SELECT day
> FROM emailcampaign
> WHERE campaign_id = '$emailcampaign'
> AND member_id = '$members_id'
> ";
>
> $DB_Change_Campaign_Results = safe_query($query);
>
> ##
> ## NOTICE: changed from array to assoc
> ##
> $Campaign_array = array();
> while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) {
> ## Switched to an indexed array with the day as the value instead of
> the key
> $Campaign_array[] = $row['day'];
> }
>
> # At this point you have arrays as values for your $Campaign_array
> # So, unless $k is an array that matches a sub array of $Campaign_array
> # you're never going to get a match
>
> $k = array_search($val,$Campaign_array);
>
> # What is $k at this point? An int (1, 2, 3, etc...) , string
> (Sunday, Monday, etc...)
> # Before I go any further I will need to know the above information.
> if ( isset($Campaign_array[($k + 1)])) {
> echo $Campaign_array[($k + 1)];
> } else {
> echo 'Not found';
> }
>
>

include ("includes/location.php");
$query = " SELECT day
FROM emailcampaign
where campaign_id = '1'
AND member_id = '8'
";
$DB_Change_Campaign_Results = safe_query($query);
$Campaign_array = array();
while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) {
$Campaign_array[] = $row['day'];
}
if ( isset($Campaign_array[($k + 1)])) {
echo $Campaign_array[($k + 1)];
} else {
echo 'Not found';
}
var_dump($Campaign_array);


This is what I get now when I run this

1

*array*
0 => string '0' /(length=1)/
1 => string '1' /(length=1)/
2 => string '3' /(length=1)/
3 => string '6' /(length=1)/
4 => string '9' /(length=1)/
5 => string '12' /(length=2)/
6 => string '15' /(length=2)/
7 => string '20' /(length=2)/
8 => string '25' /(length=2)/
9 => string '30' /(length=2)/
  Réponse avec citation
Vieux 17/09/2007, 21h13   #17
Richard Kurth
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

brian wrote:
> Richard Kurth wrote:
>>
>> What I am trying to is get all the days from a table where email
>> campaign = number. and then look at the last day that was sent and
>> find it in the list and get the next day that follows that day. At
>> this point the script below is not working. So if you have any Ideas
>> that would be a better way of doing this please let me know.
>>
>>
>> $query = "SELECT day FROM emailcampaign where campaign_id =
>> '$emailcampaign' AND member_id = '$members_id'";
>> $DB_Change_Campaign_Results = safe_query($query);
>> for ($i=0; $i < mysql_num_rows($DB_Change_Campaign_Results); $i++) {
>> $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results);
>> $Campaign = $Change_Campaign_row['day'];
>> $Campaign_array[$Campaign] = $Change_Campaign_row;
>> }
>> $k = array_search($val,$Campaign_array);
>> if ($k + 1 < count($Campaign_array)){ echo $Campaign_array[$k + 1]; }
>>

>
>
> You can simplify this greatly:
>
> $Campaign_array = Array();
>
> $DB_Change_Campaign_Results = safe_query($query);
>
> while ($row = mysql_fetch_array($DB_Change_Campaign_Results))
> {
> array_push($Campaign_array, $row['day']);
> }
>
> Because you're only selecting day from the table, your row data
> consists of only one column. You're repeating stuff unnecessarily by
> doing this:
>
> $Campaign = $Change_Campaign_row['day'];
> $Campaign_array[$Campaign] = $Change_Campaign_row;
>
> IOW, for the row that contains '5', you'd end up with
> $Campaign_array[5] == an array that essentially just points to the
> value '5'.
>
> I also noticed that your dump shows that the days begin with 0. If
> these are calendar days you're adding a further layer of complexity.
>


0 represents the email campain to send 0 mens send the one that gois out
the day the campain starts. the next number in the list is the next day
a email is sent out would represent 5 day after the starting day
  Réponse avec citation
Vieux 17/09/2007, 21h32   #18
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:
> include ("includes/location.php");
> $query = " SELECT day
> FROM emailcampaign
> where campaign_id = '1'
> AND member_id = '8'
> ";
> $DB_Change_Campaign_Results = safe_query($query);
> $Campaign_array = array();
> while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) {
> $Campaign_array[] = $row['day'];
> }
> if ( isset($Campaign_array[($k + 1)])) {
> echo $Campaign_array[($k + 1)];
> } else {
> echo 'Not found';
> }
> var_dump($Campaign_array);
>
>
> This is what I get now when I run this
>
> 1
>
> *array*
> 0 => string '0' /(length=1)/
> 1 => string '1' /(length=1)/
> 2 => string '3' /(length=1)/
> 3 => string '6' /(length=1)/
> 4 => string '9' /(length=1)/
> 5 => string '12' /(length=2)/
> 6 => string '15' /(length=2)/
> 7 => string '20' /(length=2)/
> 8 => string '25' /(length=2)/
> 9 => string '30' /(length=2)/
>
>
>


Are there going to be wholes in the date range?

if so, you will have to do that last bit like this.


<?php

include ("includes/location.php");

#
# Setting $k
# Make sure that $k is an integer, not a string.
# hence, no quotes
$k = 5;

$query = " SELECT day
FROM emailcampaign
WHERE campaign_id = '1'
AND member_id = '8'
";
$DB_Change_Campaign_Results = safe_query($query);
$Campaign_array = array();
while ( $row = mysql_fetch_assoc($DB_Change_Campaign_Results) ) {
$Campaign_array[] = $row['day'];
}

sort($Campaign_array);

foreach ( $Campaign_array AS $day ) {
if ( $day <= $k ) {
$day = next($Campaign_array);
break;
}
}

echo $day;

var_dump($Campaign_array);

?>



--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Réponse avec citation
Vieux 18/09/2007, 00h08   #19
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

Richard Kurth wrote:
> What I am trying to is get all the days from a table where email
> campaign = number. and then look at the last day that was sent and find
> it in the list and get the next day that follows that day. At this point
> the script below is not working. So if you have any Ideas that would be
> a better way of doing this please let me know.
>
>
> $query = "SELECT day FROM emailcampaign where campaign_id =
> '$emailcampaign' AND member_id = '$members_id'";
> $DB_Change_Campaign_Results = safe_query($query);
> for ($i=0; $i < mysql_num_rows($DB_Change_Campaign_Results); $i++) {
> $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results);
> $Campaign = $Change_Campaign_row['day'];
> $Campaign_array[$Campaign] = $Change_Campaign_row;
> }
> $k = array_search($val,$Campaign_array);
> if ($k + 1 < count($Campaign_array)){ echo $Campaign_array[$k + 1]; }


Ok, so just to recap (because I'm very confused now, even more after the
many tries on the list since):
You have table in your database called 'emailcampaign'. This table has a
column called 'day'.
Now, your table holds 1 row per "campaign" and the 'day' column
signifies when the last mail has been sent.

-- am I correct on this so far ? --

Next, you have an array which contains a FIXED collection of days on
which an email should be sent, which looks like so:
$days_to_send_email_on = array('0','1','3','5','8','15','25');

-- am I correct on this so far [2] ? --

You know the 'day' the last mail was sent, and you wish to check your
FIXED array to find out when the next email should be sent.

-- am I correct on this so far [3] ? --

If so, then there are a few things I'm wondering:
1. How did you come up with that fixed array? Is it the result of some
kind of calculation?
1.1 If so, it'd be easier to solve it the mathematical way.
1.2 If #1 is a "random" result (ie. user-input, or different), then
how about storing it in a table and doing:
SELECT day WHERE day > $last_day_it_was_sent_on ORDER BY day ASC LIMIT 1

2. Actually, that's all I'm wondering

- Tul
  Réponse avec citation
Vieux 19/09/2007, 18h47   #20
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

At 11:52 AM -0400 9/17/07, brian wrote:
>tedd wrote:
>>Richard Kurth wrote:
>>
>>>$Campaign_array| = array('0','1','3','5','8','15','25');|
>>>I know that I can find the next recored in a array using next.
>>>What I do not understand is if I know the last number was say 5
>>>how do I tell the script that that is the current number so I can
>>>select the next record

>>
>>
>>What the next record?
>>
>>Try:
>>
>> $array = array('0','1','3','5','8','15','25');
>> $val = "5";
>>
>> echo($array[array_search($val, $array)+1]);
>>
>>Cheers,
>>
>>tedd

>
>Not quite:
>
>$array = array('0','1','3','5','8','15','25');
>$val = "25";
>
>echo($array[array_search($val, $array)+1]);
>
>Notice: Undefined offset: 7 ...
>
>brian


Duh?

You program for that -- you want me to write the entire code?

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 19/09/2007, 21h14   #21
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Finding next recored in a array

tedd wrote:
> At 11:52 AM -0400 9/17/07, brian wrote:
>
>> tedd wrote:
>>
>>> Richard Kurth wrote:
>>>
>>>> $Campaign_array| = array('0','1','3','5','8','15','25');|
>>>> I know that I can find the next recored in a array using next. What
>>>> I do not understand is if I know the last number was say 5 how do I
>>>> tell the script that that is the current number so I can select the
>>>> next record
>>>
>>>
>>>
>>> What the next record?
>>>
>>> Try:
>>>
>>> $array = array('0','1','3','5','8','15','25');
>>> $val = "5";
>>>
>>> echo($array[array_search($val, $array)+1]);
>>>
>>> Cheers,
>>>
>>> tedd

>>
>>
>> Not quite:
>>
>> $array = array('0','1','3','5','8','15','25');
>> $val = "25";
>>
>> echo($array[array_search($val, $array)+1]);
>>
>> Notice: Undefined offset: 7 ...
>>
>> brian

>
>
> Duh?
>
> You program for that -- you want me to write the entire code?
>


~sigh~

Grasshopper, the point i was trying to make is that your example
displays what *not* to do with array_search(). Though a wonderful
teaching aid in itself, it falls somewhat short of being a reasonable
solution for the OP.

brian
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,36216 seconds with 29 queries