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.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A hard for me problem on HTML pattern processing

    Hi, all!

    Let's suppose I have some HTML with multiple SELECT boxes:

    Code:
    <select name='category'>
    <option value="1">1</option>
    <option value="25">25</option>
    ...
    <option value="333">333</option>
    </select>
    
    <select name='type'>
    <option value="3">3</option>
    <option value="15">15</option>
    ...
    <option value="335">335</option>
    </select>
    
    <select name='something'>
    <option value="66">66</option>
    <option value="81">81</option>
    ...
    <option value="233">233</option>
    </select>
    Values are always integers.

    I also have $_GET['category'], $_GET['type'] and $_GET['something'].

    What I need is to process this HTML so, that if

    Code:
    $_GET[$target_select]
    matches a value of an <option> in

    Code:
    '<select id='.$target_select.'>'
    then " selected" is added to the end of <option> before the closing ">".

    In other words, I need to process the HTML so, that if an option value in a corresponding SELECT matches the value of, for example, $_GET['category'], then the option is being marked as selected.

    Well, I hope I told my problem enough understandable ,)

    Could I get some help? I think I must use some REGEXP processing, but not sure with with function and what regexp should I use (I am null in regexps).

    Thank you.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    Where is the value of $_GET[$target_select] coming from and when does it enter the picture? If you use php to define it you will have to reload the page. This really looks like a job for javascript, it can make those changes in the form without having to reload the page, then when you submit the form it will have everything. But why do you have to add selected? When something is selected that shows in the form, it just holds that value.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A script on a webpage launches an AJAX request, the php script gets $_GET from this request, constructs an URL using $_GET, opens connection to a remote server, gets the result, then processes the result and gives back to AJAX and the final result is being displayed on the webpage.


    So, $_GET enters the picture at the very beginning of this PHP process.

  • #4
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, let's simplify it:

    how can I transform the string '<option value="'.$_GET['the_something'].'">' to '<option value="'.$_GET['the_something'].'">' if the source string is between '<select name="the_something">' and '</select>'?
    Last edited by rreimche; 08-01-2012 at 12:22 PM.

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    like this:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <?PHP
    //add as many as you like or from a database in the format 'text'=>'value' no comma after the last option
    $options = array('1'=>'1','25'=>'25','333'=>'333','56'=>'56','help text'=>'help','something text'=>'something','key'=>'value');

    echo 
    "<select name='category'>";

    foreach (
    $options as $text => $value){
        echo 
    '<option value="'.$value.'" ';
        
        if (
    $_GET['target_select'] == $value){
            echo 
    'selected';
        }
        echo 
    '>'.$text.'</option>'.PHP_EOL;
    }


    echo 
    '</select>';
    ?>
    </body>
    </html>

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but I've meant that I already have an HTML code that I need to process in the earlier mentioned way.

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    then your going to have to change it.

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If I've understood correctly then one way to do this is:

    PHP Code:
    <?php

    $the_html 
    = <<< HEREDOC
    <select name='category'>
    <option value="1">1</option>
    <option value="25">25</option>
    ...
    <option value="333">333</option>
    </select>

    <select name='type'>
    <option value="3">3</option>
    <option value="15">15</option>
    ...
    <option value="335">335</option>
    </select>

    <select name='something'>
    <option value="66">66</option>
    <option value="81">81</option>
    ...
    <option value="233">233</option>
    </select>
    HEREDOC;

    $the_html str_replace("\"{$_GET['category']}\"""\"{$_GET['category']}\" SELECTED"
        
    $the_html1);
    $the_html str_replace("\"{$_GET['type']}\"""\"{$_GET['type']}\" SELECTED"
        
    $the_html1);
    $the_html str_replace("\"{$_GET['something']}\"""\"{$_GET['something']}\" SELECTED"
        
    $the_html1);
    echo 
    $the_html;
    ?>
    But if a chosen number repeats for different selects then this approach might fall down; it will replace the first occurrence of that number, regardless of which select it relates to. Added: If this might be an issue then just split it into three different HEREDOCs and apply str_replace to the appropriate section.
    Last edited by AndrewGSW; 08-03-2012 at 12:45 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, thanks, that seems to be something that might help. But the problem you mentioned is right the problem because of which I started this topic ,)

    Hm... I've just got an idea ,)

    1. Parse the code till '<select name="category">' or '<select name="type">' or '<select name="something">' is met. Is there a function for that?

    3. Assign the whole code of the found select box to a variable. I mean the whole code between '<select name="OURVARIABLENAME">' and the closest '</select>'.

    3. Make a copy of the variable to have the code of the select box after modifying it. It is needed to find this code later in the source code for the purpose of replacing.

    4. Apply "str_replace" function to this variable in oder to add "SELECTED" to an appropriate '<option>', if there is one.

    5. Apply "str_replace" function to the source code in oder to replace the code of the select box with the new one. In this step we search for the "source" code of the select and replace it with the code from the result of step 4.

    Guys, I hope all that is understandable ,) I tried to make it so, honestly ,)

    Well... can all that be done?

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    That approach occurred to me momentarily, but I dismissed it in favour of my previously posted code
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #11
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, now I see ,) I've missed the count parameter of the str_replace().

    So, we run str_replace 3 times in appropriate order, every function replaces only one occurrence of the search string and this together gives us what we want. Yeah, this should do it ,) Thanks ,)

  • #12
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If the same number/value might recur for different select's then it is better to do the replacements in separate stages:

    PHP Code:
    <?php

    $html_cat 
    = <<< HEREDOC
    <select name='category'>
    <option value="1">1</option>
    <option value="25">25</option>
    // ...
    <option value="333">333</option>
    </select>
    HEREDOC;
    $html_cat str_replace("\"{$_GET['category']}\"""\"{$_GET['category']}\" SELECTED"
        
    $html_cat1);

    $html_type = <<< HEREDOC
    <select name='type'>
    <option value="3">3</option>
    <option value="15">15</option>
    ...
    <option value="335">335</option>
    </select>
    HEREDOC;
    $html_type str_replace("\"{$_GET['type']}\"""\"{$_GET['type']}\" SELECTED"
        
    $html_type1);

    $html_something = <<< HEREDOC
    <select name='something'>
    <option value="66">66</option>
    <option value="81">81</option>
    ...
    <option value="233">233</option>
    </select>
    HEREDOC;
    $html_something str_replace("\"{$_GET['something']}\"""\"{$_GET['something']}\" SELECTED"
        
    $html_something1);

    echo 
    "$html_cat\n$html_type\n$html_something\n";
    ?>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    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
    •