News:

Wondering if this will always be free?  See why free is better.

Main Menu

$message['alternate'] on a another function

Started by kenchix1, September 07, 2006, 01:52:53 AM

Previous topic - Next topic

kenchix1

I created a funtion that adds up a dummy post on every end of a thread and was able to do it except for one problem, the $message['alternate']. I used the condition if ($message['alternate'] == 0) but it seems to return the same value and doesn't change the color window background color. I also declared it as global $message.

what should I do determine what's the next window background color (windowbg/windowbg2) to use ?

Thanks in advance.

JayBachatero

What you can do is

$alternate = true;
foreach ($message as $m)
{
       echo '
               <td class="', $alternate ? 'windowbg' : 'windowbg2'" >...

       $alternate != $alternate;
}


That is just a quick and dirty example.
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

kenchix1

Quote from: Jay The Code Monkey on September 07, 2006, 01:58:32 AM
What you can do is

$alternate = true;
foreach ($message as $m)
{
       echo '
               <td class="', $alternate ? 'windowbg' : 'windowbg2'" >...

       $alternate != $alternate;
}


That is just a quick and dirty example.

should I also declare $alternate as global along with the $message ? I forgot to tell that I only add one dummy post on every thread. I wanted it to look real, so I need to alternate the background color of the window for the dummy post. something like if the last real post used windowbg, then the dummy message should use windowbg2 and vice versa.

thanks for the help.

JayBachatero

No no need to set that a global.  Can you post the whole code that you have now?
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

kenchix1



function dummy_post()
{
global $settings, $txt, $message, $context;
echo '<tr><td style="padding: 1px 1px 0 1px;">';

echo '<table width="100%" cellpadding="3" cellspacing="0" border="0"><tr><td class="', $message['alternate'] == 0 ? 'windowbg' : 'windowbg2', '">';

      // Show information about the poster of this message.
      echo '
.....
... Dummy post here



this is the code where I used the $message['alternate'] which I also got from the displaytemplate.php. I don't know where to insert the code that you suggest here.

thanks for the help.

JayBachatero

Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

kenchix1


JayBachatero

You can't really use this on this situation since you don't have it on a loop.  You have to manually enter windowbg or windowbg2.
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

jamesk

Silly beginner PHP question:

If using SSI.php and use the example on main site page:


$alternate = true;
foreach ($message as $m)
{
       echo '
               <td class="', $alternate ? 'windowbg' : 'windowbg2'" >...

       $alternate != $alternate;
}


Will I run into problems if we have many users at the same time viewing the page?  What I guess I am asking is if $alternate should be in a session variable or is it unique to each browser request?


JayBachatero

There shouldn't be any problems with this.  This wont put any load on the server.
Follow me on Twitter

"HELP!!! I've fallen and I can't get up"
This moment has been brought to you by LifeAlert

jamesk

Well, for my site, with very few users, it won't be an issue at all.  I guess I'm asking from more of a theortical (and programming correctness) standpoint.

If thousands of users hit the same page at the same time, will it be inevitable that the some will show not show alternating bkgrd colors on certain items?

Put another way, let's say the code is like this:

$alternate = true;
foreach ($message as $m)
{
       echo $alternate;

       $alternate != $alternate;
}


Under normal circumstances, it'll show "true false true false...." but if someone had a site with thousands of users hitting the same page at the same time, will it show "true false false true..." for some (if the variable is changed by someone else's browser)?  In those cases, is it "correct" to use something like $_SESSION['alternate']?

I'm just starting to learn PHP, and am really trying to grasp the idea of variable usage when many are accessing the same variable/server at the same time.  I've been more of non-thread Java person...


karlbenson

No, alternate is done for each person.

What someone else gets will NOT affect what the next person gets.

Using the code Jay posted every person should ALWAYS get
True, False, True, False.... no matter whether there is 1 person on your site or a million.

If your getting True True, or False False at any point then there is something wrong somewhere in your code.

jamesk

Thanks... I think I understand:

So there is a unique instance of the $alternate variable for each user (browser)?

And what one user does with his variable does not change another's instance of that same variable (name)...

So, if we had:


$count=1;
while ($count < 500000)
{
    echo $count;
    $count++;
}


If one user ran the code, it'll display from 1-499999 even if some other user opens up the same code (and thus setting his own $count to 1) because now there are two instances of $count...

Is my thinking correct?


karlbenson

Indeed.

It should be exactly the same for Every page load (whether the same person or different)

jamesk

Thank you -- clears it up and makes things a lot easier.

karlbenson

Unless the code is stored/saved somewhere and you're loading them each time that is.

But none of the examples in this topic are storing variables for the user in $_SESSION or for the user or other users by storing viarables in the db.

Advertisement: