...

View Full Version : A hard for me problem on HTML pattern processing



rreimche
07-31-2012, 06:18 PM
Hi, all!

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


<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


$_GET[$target_select]

matches a value of an <option> in


'<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.

DrDOS
07-31-2012, 06:57 PM
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.

rreimche
07-31-2012, 09:51 PM
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.

rreimche
08-01-2012, 12:16 PM
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>'?

Arcticwarrio
08-01-2012, 12:51 PM
like this:


<!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>

rreimche
08-02-2012, 06:33 PM
Thanks, but I've meant that I already have an HTML code that I need to process in the earlier mentioned way.

Arcticwarrio
08-02-2012, 09:36 PM
then your going to have to change it.

AndrewGSW
08-03-2012, 12:42 AM
If I've understood correctly then one way to do this is:


<?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_html, 1);
$the_html = str_replace("\"{$_GET['type']}\"", "\"{$_GET['type']}\" SELECTED",
$the_html, 1);
$the_html = str_replace("\"{$_GET['something']}\"", "\"{$_GET['something']}\" SELECTED",
$the_html, 1);
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.

rreimche
08-04-2012, 08:41 PM
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?

AndrewGSW
08-04-2012, 08:59 PM
That approach occurred to me momentarily, but I dismissed it in favour of my previously posted code :thumbsup:

rreimche
08-05-2012, 03:56 PM
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 ,)

AndrewGSW
08-05-2012, 06:48 PM
If the same number/value might recur for different select's then it is better to do the replacements in separate stages:


<?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_cat, 1);

$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_type, 1);

$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_something, 1);

echo "$html_cat\n$html_type\n$html_something\n";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum