Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Populating a Drop-Down Only with Items Starting with a Specified String

    Hello,
    I'm proficient in other languages but I'm still a novice learning PHP. I'm trying to figure out what appears to be a rather simple modification and I'm wondering if anyone here would be so kind as to assist me (thank you in advance)

    The following line of code populates a drop-down box with various categories (about 20-30 items in my case) In this particular instance I would like to modify the code to only display categories starting with '[ADV]' first in their category name. I've taken a cursory look at PHP string functions but I'm not seeing anything that would only look at the beginning of a text string and select it only if it has [ADV] as the first five characters.

    Here is the code I'm attempting to modify (as above)
    PHP Code:
    {~if is_array($categories) && count($categories)~}
      <
    tr>
        <
    td align="right"><b>Category</b></td>
        <
    td>
          <
    select name="category">
      {~foreach 
    item=cat from=$categories~}
            <
    option value="{~$cat~}"{~if $smarty.post.category==$cat~} selected="selected"{~/if~}>{~$cat~}</option>
      {~/foreach~}
          </
    select>
        </
    td
    I believe this would take the form of a very small change to the option value line.

  • #2
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post

    It looks like the function strpbrk might be my best bet here... now to find out how to properly use the function.

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Code:
    if( substr($myString04) == "[ADV]" )
    {
    echo 
    "<option>$myString</option>";

    substr() reference: http://php.net/manual/en/function.substr.php

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • Users who have thanked whizard for this post:

    FreeCashPro (07-08-2013)

  • #4
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    PHP Code:
    if( substr($myString04) == "[ADV]" )
    {
    echo 
    "<option>$myString</option>";

    substr() reference: http://php.net/manual/en/function.substr.php

    HTH
    Dan

    Thank you very much for the clue...

    In my code I tried this but it gave me a syntax error:
    PHP Code:
    {~if is_array($categories) && count($categories)~}
      <
    tr>
        <
    td align="right"><b>Category</b></td>
        <
    td>
          <
    select name="category">
      {~foreach 
    item=cat from=$categories~}
           {~if( 
    substr($cat04) == "[ADV]" )~} 
          <
    option value="{~$cat~}"{~if $smarty.post.category==$cat~} selected="selected"{~/if~}>{~$cat~}</option>
           {~/if~}
       {~/foreach~}
          </
    select>
        </
    td
    I'm still trying to figure this out... I used these statements wrapped around the option statement (I only want the option statement running if [ADV] is in the category string)
    PHP Code:
    {~if( substr($cat04) == "[ADV]" )~}

    {~/if~} 

  • #5
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    PHP Code:
    {~if is_array($categories) && count($categories)~}
      <
    tr>
        <
    td align="right"><b>Category</b></td>
        <
    td>
          <
    select name="category">
      {~foreach 
    item=cat from=$categories~}
             {~if( 
    substr($cat04) == "[ADV]" )~}
            <
    option value="{~$cat~}"{~if $smarty.post.category==$cat~} selected="selected"{~/if~}>{~$cat~}</option>
              {~/if~}
       {~/foreach~}
          </
    select>
        </
    td
    I tried doing that but got an error
    I added these around the option line:
    PHP Code:
    {~if( substr($cat04) == "[ADV]" )~}
    {~/if~} 
    I'm not sure why my last message wasn't posted (?)

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    whaaat? you dont need those curly brace things.

    What was the error?

    syntax:
    PHP Code:
    if(condition)
    {
    //do this
    }
    else
    {
    //do this
    }

    foreach(
    $items as $item)
    {
    //do this

    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post

    Maybe I could start a little higher in the foreach statement like a line that says essentially:

    foreach cat where the first 5 characters = '[ADV]'?
    (only do the foreach if the string is there?)

    Let me look around to see if that can be done... ...any help would truly be appreciated.

  • #8
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It's smarty php - the whole script (tens of thousands of lines of code have the brackets)

    That's my understanding

  • #9
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    oK, let me put that code back in and copy the exact error message... gimme a min :-)

  • #10
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Oh. SMARTY. idk. lol but do give the error. that might fix it easy
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #11
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Here's my error with that code in:

    Syntax Error in template "gc_script:_adv_camp_manage" on line 50 "{~if( substr($cat, 0, 4) == "[ADV]" )~}" PHP function 'if' not allowed by security setting

  • #12
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    Oh. SMARTY. idk. lol but do give the error. that might fix it easy
    Thank you, I really do appreciate the help :-)

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    I did a little Googling, and found this:
    http://www.smarty.net/docsv2/en/vari...y.settings.tpl

    It looks like you have to edit the $security_settings of SMARTY to allow the substr() function in an if statement.

    How you DO that? Not sure.

    Edit:
    Found this:

    Quote Originally Posted by http://www.smarty.net/docs/en/advanced.features.tpl#advanced.features.security
    $php_functions is an array of PHP functions that are considered trusted and can be used from within template. To disable access to all PHP functions set $php_functions = null. An empty array ( $php_functions = array() ) will allow all PHP functions. The default is array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array','time','nl2br').
    This appears to be a property of a Smarty_Security class...

    Somehow you need to add 'substr' to that list.



    Dan
    Last edited by whizard; 07-08-2013 at 08:39 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #14
    New Coder
    Join Date
    Jul 2013
    Location
    Chicago, IL USA
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post

    Ok, thank you.

    Would it be possible to make the foreach statement conditional on $cat containing [ADV]? Would that be another route to try? Might you be able to give me some guidance on that? Please...

  • #15
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You could do:
    PHP Code:
    if($myString[0] == "[" && $myString[1] == "A" && $myString[2] == "D" && $myString[3] == "V" && $myString[4] == "]")
    {
    //print the option tag

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •