Announcement

Collapse
No announcement yet.

How can I use a raw variable in an if statement?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mark.B
    replied
    Originally posted by Andy View Post
    Coding is always done by trial and error, so even tips that don't pan out are extremely helpful.
    In my case, mainly error.

    Leave a comment:


  • Andy
    replied
    Originally posted by Mark.B View Post
    That's ok - I got every answer wrong though! I have often said I'm no good at this coding lark, people shouldn't really listen to me. Disaster will ensue!
    Coding is always done by trial and error, so even tips that don't pan out are extremely helpful.

    Leave a comment:


  • Mark.B
    replied
    Originally posted by Andy View Post
    klh you're a genius! Your example works perfectly. I do need to make sure $my_preregistered_variable is always a variable and if the user doesn't have anyone on their ignore list I assign $my_preregistered_variable to be an array wih one value that is 0.

    I greatly appreciate your help on this.

    Also thank you Mark for your help too.
    That's ok - I got every answer wrong though! I have often said I'm no good at this coding lark, people shouldn't really listen to me. Disaster will ensue!

    Leave a comment:


  • Andy
    replied
    Originally posted by klh View Post
    in_array() is definitely allowed. I would have to guess that the problem is that $my_preregistered_variable is not an array. Just to check you could try this:

    Code:
    <vb:if condition="is_array($my_preregistered_variable) AND !in_array($thread[postuserid], $my_preregistered_variable)">
    It's strange because I don't remember seeing that error you're getting before yesterday, but on vb.org somebody else was getting that same error trying to do something similar to what you're doing.
    klh you're a genius! Your example works perfectly. I do need to make sure $my_preregistered_variable is always an array and if the user doesn't have anyone on their ignore list I assign $my_preregistered_variable to be an array with one value that is 0.

    I greatly appreciate your help on this.

    Also thank you Mark for your help too.
    Last edited by Andy; Sat 11th Feb '12, 2:53pm.

    Leave a comment:


  • kh99
    replied
    Sorry to disagree, but I don't think there's any reason it can't be a variable. If Andy was getting a syntax error saving it, then there probably really was an error somewhere (like maybe mismatched parens?).

    Leave a comment:


  • Mark.B
    replied
    Originally posted by Andy View Post
    Hi Mark,

    The example code you provided works, but if I try to replace the following:

    array(225,232,294,209,131,223,145,136,260,265,133,295,296,297,298)

    with a variable like $my_preregistered_variable it will not allow me to save the template giving me a syntax error.
    I don't think you can do it that way, the value within the brackets can't be a variable.

    Leave a comment:


  • Andy
    replied
    Hi Mark,

    The example code you provided works, but if I try to replace the following:

    array(225,232,294,209,131,223,145,136,260,265,133,295,296,297,298)

    with a variable like $my_preregistered_variable it will not allow me to save the template giving me a syntax error.

    Leave a comment:


  • Mark.B
    replied
    I've got in_array in conditionals in my templates and it works, not sure of the type of variable though.

    Eg I have this monster:

    Code:
    <vb:if condition="!in_array($thread['forumid'], array(225,232,294,209,131,223,145,136,260,265,133,295,296,297,298))">

    Leave a comment:


  • Andy
    replied
    Originally posted by klh View Post
    in_array() is definitely allowed.
    Hi klh,

    Are you sure it's allowed? There isn't a single occurrence of using an array in an if condition with the stock code in vBulletin. Do you have any such code in your modified templates, or have you seen it used in other examples?

    I'll be back later this afternoon and will give your example a try and report back. I sure appreciate the help.

    Leave a comment:


  • kh99
    replied
    in_array() is definitely allowed. I would have to guess that the problem is that $my_preregistered_variable is not an array. Just to check you could try this:

    Code:
    <vb:if condition="is_array($my_preregistered_variable) AND !in_array($thread[postuserid], $my_preregistered_variable)">
    It's strange because I don't remember seeing that error you're getting before yesterday, but on vb.org somebody else was getting that same error trying to do something similar to what you're doing.

    Leave a comment:


  • Andy
    replied
    Originally posted by klh View Post


    Then in the template,

    Code:
    <vb:if condition="!in_array($thread[postuserid], $my_preregistered_variable)">
    Unfortunately it looks like in_array is not allowed in the if condition statement. I get the following error whe I try to save the template:

    Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3459

    Leave a comment:


  • Andy
    replied
    I'll give that a try. Thank you kindly.

    Leave a comment:


  • kh99
    replied
    You could do something like, in script.php, don't print or echo anything, just make it create an array called $my_preregistered_variable and fill it with ignored userids, if any.

    Then in the plugin code,

    Code:
    include('/path/to/my/script.php');
    vB_Template::preRegister('threadbit',array('my_preregistered_variable' => $my_preregistered_variable));

    No need for ob_start/end if no output. Also, you could actually put the preRegister() call in the script if you wanted.


    Then in the template,

    Code:
    <vb:if condition="!in_array($thread[postuserid], $my_preregistered_variable)">

    ETA: I see now, you're working with userids.

    Leave a comment:


  • Andy
    replied
    Just to make it a bit more clear, here is the if condition I used in the threadbit template.

    Code:
    <vb:if condition="$thread['postuserid'] <> $my_preregistered_variable">

    Leave a comment:


  • Andy
    replied
    Originally posted by klh View Post
    How about this: Just have your script output the thread id, preregister it, then make the condition:

    Code:
    <vb:if condition="$thread[threadid] <> $my_preregistered_variable">
    Awesome klh, that works.

    One big issue is that most members have have more than one member on their Ignore list.

    Leave a comment:

widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Working...
X