Page 1 of 1
PHP change variable value after certain number of times
Posted: Wed Nov 23, 2011 3:39 pm
by bobbf1
Hi,
I am trying to modify some code on my website.
I have a variable $thumbnumber. Currently it is
I am using wordpress and after the third time $thumbnumber is called by wordpress I need the value of $thumbnumber to change to 2.
After the sixth time $thumbnumber is called I need the value of $thumbnumber to change to 3.
Here is my current code, that is not working.
Code: Select all
$dug=1;
if ($dug == "1,2,3") {
$thumbnumber=1;
$dug++;
}
elseif ($dug == "4,5,6") {
$thumbnumber=2;
$dug++;
}
else {
$thumbnumber=3;
}
When I run the code, it just leaves the value as 1 even though $thumbnumber is called nine times.
I also tried using do while, but it was way too slow.
Re: PHP change variable value after certain number of times
Posted: Wed Nov 23, 2011 3:42 pm
by Celauran
Code: Select all
$dug=1;
if ($dug == "1,2,3") {
$thumbnumber=1;
$dug++;
}
elseif ($dug == "4,5,6") {
$thumbnumber = 2;
$dug++;
}
else {
$thumbnumber = 3;
}
This sets $dug to 1 and $thumbnumber to 3. What is $dug? What changes it?
Re: PHP change variable value after certain number of times
Posted: Wed Nov 23, 2011 3:46 pm
by bobbf1
Hi,
I am just using $dug as a variable to iterate through so that it will trigger the changing of $thumbnumber. I obviously am not thinking of it properly.
In my mind, the $dug is initially set to 1, then the if statement is run 3 times outputting $thumbnumber=1 and increasing $dug by 1 because of the $dug++
I know I am making an error here somewhere logically.
Re: PHP change variable value after certain number of times
Posted: Wed Nov 23, 2011 3:50 pm
by Celauran
bobbf1 wrote:then the if statement is run 3 times
What? There's no loop here, nothing is being run multiple times. Also,
checks to see if the variable $dug is set to the string "1,2,3".
Re: PHP change variable value after certain number of times
Posted: Thu Nov 24, 2011 9:23 am
by bobbf1
Haha, wow. Ok I was cobbling together code from a couple tutorials and am making some dumb mistakes here.
I basically need a loop that starts off with $thumbnumber=1 then iterates. During the first three iterations, it leaves $thumbnumber=1; For the the 4-6 iterations, it changes $thumbnumber=2, then any iterations after 6, it sets $thumbnumber=3
Any help on the code to do this? I have been searching php tutorials for awhile and am not getting it right so far.
Re: PHP change variable value after certain number of times
Posted: Thu Nov 24, 2011 10:20 am
by Celauran
Do you mean something like this?
Code: Select all
$thumbnumber = 1;
for ($i = 1; $i < some_value_you_specify; $i++)
{
if ($i > 3)
{
$thumbnumber = 2;
}
else if ($i > 6)
{
$thumbnumber = 3;
}
}
Re: PHP change variable value after certain number of times
Posted: Thu Nov 24, 2011 3:40 pm
by pickle
This would work:
Code: Select all
$thumbnumber = 1;
for($i = 1; $i <= $whatever_your_max_number_is; $i++)
{
$thumbnumber = ceil($i/3);
$thumbnumber = ($thumbnumber > 3) ? 3 : $thumbnumber;
}
Take out the 2nd line inside the loop to allow $thumbnumber to be greater than 3
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 12:32 pm
by bobbf1
Hi Celauran,
When I use the following code, it sets thumbnumber to 2 for all 9 iterations.
Code: Select all
$thumbnumber = 1;
for ($i = 1; $i < 9; $i++)
{
if ($i > 3)
{
$thumbnumber = 2;
}
else if ($i > 6)
{
$thumbnumber = 3;
}
}
@pickle When I use the following code, it sets $thumbnumber to 1 in all nine iterations.
Code: Select all
$thumbnumber = 1;
for($i = 1; $i <= 9; $i++)
{
$thumbnumber = ceil($i/3);
$thumbnumber = ($thumbnumber > 3) ? 3 : $thumbnumber;
}
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 12:43 pm
by pickle
It shouldn't. I'm running this exact code & it works as expected
Code: Select all
$thumbnumber = 1;
for($i = 1; $i <= 9; $i++)
{
$thumbnumber = ceil($i/3);
$thumbnumber = ($thumbnumber > 3) ? 3 : $thumbnumber;
echo $thumbnumber.'<br />';
}
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 12:51 pm
by Celauran
bobbf1 wrote:When I use the following code, it sets thumbnumber to 2 for all 9 iterations.
I must have been half asleep when I wrote that. I'm not even going to bother fixing it since pickle's is simpler and better.
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 1:01 pm
by bobbf1
Hi Pickle,
I see the code is working properly after adding the echo statement. When I look at it on my site, it displays the echoed numbers.
I remove the echo statement and when I call $thumbnumber below it still sets all 9 iterations of thumnumber within the $data array to three. Here is the whole code segment
Code: Select all
$thumbnumber = 1;
for($i = 1; $i <= 9; $i++)
{
$thumbnumber = ceil($i/3);
$thumbnumber = ($thumbnumber > 3) ? 3 : $thumbnumber;
echo $thumbnumber.'<br />';
}
$data = array(
'title' => '<a id="one" href="'.get_permalink($wppost->ID).'" title="'. $title_attr .'" onMouseOver="setpage(\'thumb' .$thumbnumber.'\', \''.get_permalink($wppost->ID).'\', \''.get_video_thumbnail($wppost->ID).'\')"><span class="wpp-post-title">'. $tit .'</span></a>',
'summary' => $post_content,
'stats' => $stats,
'img' => $thumb,
'id' => $wppost->ID,
'posttime' => ' <span class="wpp-post-time">( '.get_field('video_time').')</span>'
);
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 1:43 pm
by pickle
Well ya, it will. You're looping through 1-9, then outputing after the loop is done. If you want to access the value of thumbnumber in the loop, then you need to put your $data assignment in the loop.
The code you posted - how does it relate to $thumbnumber? Maybe you don't need another loop?
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 1:53 pm
by manohoo
Take a look at this:
Code: Select all
for ($i=0; $i<20; $i++ ) {
echo (int) ($i/3)+1;
}
The above code will output: 11122233344455566677
Every third iteration the output increments by 1, is that what you wanted?
Re: PHP change variable value after certain number of times
Posted: Fri Nov 25, 2011 1:56 pm
by bobbf1
The $data array is being employed by a wordpress plugin to output code. When it outputs the code, there is a javascript event called setpage. When it employs setpage, it is passing a value called thumb1 (an image id) originally. I realized for the first three posts the plugin outputs thumb1 and it works fine.
This is because of the first three posts it output are in a specific div (tabs-1) that contains an img with an id of thumb1.
But for 4-6 times it ouputs the code, the value passed needs to be thumb2 because it is targeting a different image id(thumb2).
This is because these posts show up on a different div (tabs-2) that contains an img with an id of thumb2.
For the for 7-9 times it ouputs the code, the value passed needs to be thumb3 because it is targeting a different image id(thumb3). This is because these posts show up on a different div (tabs-3) that contains an img with an id of thumb3.