PHP change variable value after certain number of times

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

PHP change variable value after certain number of times

Post by bobbf1 »

Hi,

I am trying to modify some code on my website.

I have a variable $thumbnumber. Currently it is

Code: Select all

$thumbnumber=1;

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.
Last edited by bobbf1 on Wed Nov 23, 2011 3:43 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP change variable value after certain number of times

Post 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?
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

Re: PHP change variable value after certain number of times

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP change variable value after certain number of times

Post 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,

Code: Select all

if ($dug == "1,2,3")
checks to see if the variable $dug is set to the string "1,2,3".
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

Re: PHP change variable value after certain number of times

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP change variable value after certain number of times

Post 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;
  }
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP change variable value after certain number of times

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

Re: PHP change variable value after certain number of times

Post 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;
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP change variable value after certain number of times

Post 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 />';
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP change variable value after certain number of times

Post 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.
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

Re: PHP change variable value after certain number of times

Post 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>'
					);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP change variable value after certain number of times

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: PHP change variable value after certain number of times

Post 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?
Last edited by manohoo on Fri Nov 25, 2011 1:56 pm, edited 1 time in total.
bobbf1
Forum Newbie
Posts: 8
Joined: Wed Nov 23, 2011 3:30 pm

Re: PHP change variable value after certain number of times

Post 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.
Post Reply