GetDotted Domains

Viewing Thread:
"Calling PHP geeks! Help required with php search script"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Sun 28/03/10 at 13:26
Moderator
"Are you sure?"
Posts: 5,000
I know a few of you speak 'php' as your first language(!) so hopefully you'll be able to help a 'cut-n-paster' :¬)


I have a basic php flatfile search script that I'm trying to modify.
The script works fine for visitors using a free form search but I would like to add some form checkboxes to make it friendlier.

The script uses a variable called 'keyword'.
The freeform search passes single or multiple search terms successfully but the FORM i'm attempting to use with checkboxes only makes use of the last selection.

The form I've created uses duplicate names - 'keyword'


<input type="checkbox" name="keyword"
value="hosting"

<input type="checkbox" name="keyword" value="broadband">broadband


In my ham fisted way I've read that I can change this to "keyword[ ]" and this should create an array. When I try this the script ends up searching for Array !

You can see the problem/code here:
www.hmmm.ip3.co.uk/searchtesting

Using FORM 1 searching for 'hosting broadband' only returns Freeola in the results.

Using FORM 2 selecting both hosting and broadband the script only searches for broadband - only picking up the last keyword.

Any help changing the FORM and/or script would be appreciated :¬)

Thanks.
[s]Hmmm...[/s]
Tue 30/03/10 at 19:59
Moderator
"Are you sure?"
Posts: 5,000
Nearly there! :¬)

hacked version

All seems to work but I need to separate the keywords.
At the moment selecting one from both selections puts them into one work.

Bet that's easy for you to fix!!! Not me though :¬(

Thanks again,
I'll be back in the morning as I'm already late...
Tue 30/03/10 at 19:45
Regular
"Devil in disguise"
Posts: 3,151
Not really. :/

Oh well, maybe I can explain without seeing. Insert this

$keyword='';
foreach($_POST as $key => $value) {
if (substr($key,0,7)=='search_') {
if (is_array($value)) {
$keyword.=' '.trim(implode(' ',$value));
} else {
$keyword.=' '.trim($value);
}

}
}
$keyword = trim(preg_replace('/[^a-zA-Z0-9s]/', '', $keyword));


after the line...

$filename = "$root/site.dat";

Change

if(strlen($_POST['keyword']) <= 1){

to

if(strlen($keyword) <= 1){

Finally delete...

$keyword = $_POST['keyword'];
if (is_array($keyword)) {
$keyword=implode(' ',$keyword);
}
$keyword = preg_replace('/[^a-zA-Z0-9s]/', '', $keyword);


Then name all your input /check / text boxes starting with "search_", like...

<form action="hmmm.php" method="post">
<input type ="hidden" name="action" value ="search">
<input type="radio" name="search_keyword1[]" value="hosting" checked>hosting�����
<input type="radio" name="search_keyword1[]" value="broadband">broadband�����
<input type="radio" name="search_keyword1[]" value="it">it����<p>
Would you like to add?<br>
<input type="radio" name="search_keyword2[]" value="bbc">bbc
<input type="radio" name="search_keyword2[]" value="help">help�����
<input type="radio" name="search_keyword2[]" value="stuff">stuff�����
<input type="radio" name="search_keyword2[]" value="blabla">blabla�����
<input type="submit" name="submit" value="Search" class="arial12"> <br>
</form>


Any field starting with search_ gets processed and appended to your search string.
Tue 30/03/10 at 19:33
Moderator
"Are you sure?"
Posts: 5,000
Hi Garin,
Is this what you are after?
This is the code for the more page

!!! That failed to post due to the forum's restrictions.
So I've updated the above page :¬)
Tue 30/03/10 at 19:25
Regular
"Devil in disguise"
Posts: 3,151
Easy enough to fix, do you have a copy of the code with the indentation intact?
Tue 30/03/10 at 19:11
Moderator
"Are you sure?"
Posts: 5,000
Eccles wrote:
> I think I understand. Don't use radio buttons in your form.
> They're not really designed for multiple selections. Use
> checkboxes or multi-option selects. If you are using checkboxes
> then users can select as many as they want.
> If you want only 1 option on each row selected then you can
> either use radios with each row having a different name attribute
> and then use php to loop through each in turn or us JS to stop
> more than one checkbox being selected (not foolproof I know).


Thanks for that.

I've tried some of that.
Checkboxes work but I need more than one mutually exclusive selection.

I can't change 'names' as you would normally do else it breaks my array using keyword[] which I need to keep to make the search work.

I've been trying JavaScript to make checkboxes behave like radio buttons linky but I don't think it likes having '[ ]' in the name!

[s]Hmmm...[/s]
Tue 30/03/10 at 19:01
Staff Moderator
"Aargh! Broken..."
Posts: 1,408
I think I understand. Don't use radio buttons in your form. They're not really designed for multiple selections. Use checkboxes or multi-option selects. If you are using checkboxes then users can select as many as they want.
If you want only 1 option on each row selected then you can either use radios with each row having a different name attribute and then use php to loop through each in turn or us JS to stop more than one checkbox being selected (not foolproof I know).
Tue 30/03/10 at 18:05
Moderator
"Are you sure?"
Posts: 5,000
I'm back for more! :¬)

I thought Garin's help would allow me to do everything, but I've found more problems :¬(


I want my visitors to be able to search for more than one selection.

Using 'radio' buttons works fine using keyword[] but if I need more than one group it fails.

I've created a new page to show the problem:
www.hmmm.ip3.co.uk/searchtesting/more.php

I can't change the 'name' keyword[] or it will break everything!

I'm not sure if I've explained this properly - if you need more info please let me know.

Thanks again.
[s]Hmmm...[/s]
Sun 28/03/10 at 15:06
Moderator
"Are you sure?"
Posts: 5,000
All working!
I've left my original page broken and the 'garin' link is working as required.

Thanks Garin - that's just what I wanted!
Very impressive - complete double-dutch to me...









[s]Hmmm...[/s]
Sun 28/03/10 at 14:59
Regular
"Devil in disguise"
Posts: 3,151
Yes, was about to say you were posting to index.php still and not garin.php
Sun 28/03/10 at 14:57
Moderator
"Are you sure?"
Posts: 5,000
Hold on - I think that might have been me...

brb

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Many thanks!
You were 100% right - great support!
Second to none...
So far the services you provide are second to none. Keep up the good work.
Andy

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.