Need assistance with design of website search using sql query - php

I have been trying to come up with a way design a webpage to allow users to make selections on a webpage and then query those selections in a database and returned to the user.
For example, my database is a list of houses. The data base table is set up something like this:
Country Region State City House
1 1 1 1 A
1 1 1 1 B
1 1 1 2 C
1 1 1 2 D
1 1 1 3 E
1 2 2 4 G
1 2 3 5 H
1 3 4 6 I
1 4 5 7 J
From the search bar, I want the user to be able to say show me any and all houses in region 2, any houses in state 3, and any houses in city 1.
In this example, the data base query would come back with houses G,H,A, and B. The order of the search and results don't really matter right now.
Is something like this even possible? I have considered using check boxes on the website but ideally I would rather just have one search bar using autocomplete (jquery) where each entry has specific values tied to it. These values are what would be passed to the search query as variables.

you could use comma separator as when you use google maps, then split the content of the search box using the comma delimiter and then you will have the 3 parameters that you need.

Your best bet would be <select> elements in a <form method="GET" action="somefile.php">, and then within the file somefile.php retrieving the $_GET values that the user posted on the form in the <select> elements and then generating SQL queries appropriately to retrieve the data.
The answer is yes, it is possible, but you're asking a lot if you're asking for someone to do it for you.

As #khanahk said, you could use select boxes.
I couldn't figure out how to search for more than just one term at a time.
You could have multiple select boxes, such as four or five on one line, with a submit button to search using the paramters selected by those select boxes.
Or you could do something really cool with a search box that uses keywords to separate the different type of search, ex.) House-a IN region-1
I'd take a look at the following link:
http://www.arroyocode.com/client-side-keyword-search-with-jquery-ui-autocomplete-and-asp-net-mvc-4

Related

How to create menu and submenu using two tables?

Please help me how to create vertical menu or sub menu using these tables in PHP.
The two tables are cat(id,cat_name,main_cat_id) and main_cat(id,main_cat_name).
main_category table
id main_category_name
1 Hotels
2 Compressors
3 Apparel & Clothing
4 Automobile
5 Car Rentals
6 Mobiles & Accessories
7 Computers & Peripherals
8 Doctors
9 Education
category table
id category_name main_category_id
1 Pizza Center 1
2 2 Star Hotel 1
3 Hotel & Restaurant 1
4 5 Star Hotel 1
5 Air Compressor 2
6 Flare Nuts 2
7 Auto Accessories-Car Stereos 4
8 Automotive Parts, Components & Spares 4
9 Motorcycles, Scooters & Two Wheeler Parts 4
My policy of encouraging a prior attempt applies here. You've laid out the database, which is great, but you need to open a text editor and start on the PHP. You can do it!
Readers here could give you a working answer, but that suffers a number of drawbacks:
Firstly, they might misunderstand what you need, and so spend a long time on something that does not help you.
Secondly, there are several million people out there that would like people to work for them for free, and we'd be here until Doomsday if we did that.
Finally, if you get a working answer with no personal research, you might not learn anything.
Here's a suggested plan of action:
Install WAMP/MAMP/LAMP on your computer
Set up your database structure and test data in a MySQL instance on your development machine
Write some PHP code to connect to the database and run a test query on it, dumping the data in a web page
Once that works, modify the query to read the categories, using an ORDER clause to get them in the right order
Now you have enough information to create a menu. Look up the format of a <select> tag using <option> tags, each of which contains the name from your database. You'll need a value attribute for each one - this is usually the id from each row of the database
Wrap the above in a <form>
From here, you can expand your menu to contain a second level. The simplest way of doing that is to research the <optgroup> tag - much under-used, in my opinion. This allows you to set up a two-level menu that is intuitive for users and simple to read values from in code.
Give that a go?

Using Ajax to suggest unavaliable number using current list numbers

is it possible to using ajax with PHP/SQL to suggest the next avaliable number for a user based on the current numbers in the list? this is to avoid duplication.
I have a form/table http://i.stack.imgur.com/TYhSG.png and in this table I have for an example departments and a list(the numbers in the list needs to be unqiue). because of the way the system is disigned, users had to manually rearrange the numbers if they wanted to the same number in another department. for an example number "4" can only be used once, this example applies to any other numbers.
The numbers in those list are all avaliable in the database, what this means is that I have a select statement and i echo $list to redender those list you show in the image.
Tp avoid users having to manally tryping those numbers especially if the list is up to 100 or more. I basically want to use ajax to first check if the number in the list is already in used and if yes then suggest a number to them.
Can this be done? If so please show me an example.
Thanks for your time.
p.s.
i missed out an information piece of information they is another column called `values
tablename: check
values type list
cars 1 2
cars 1 4
cars 1 3
bicycle 1 2
bicycle 1 3
bicycle 1 4
the numbers in the list can be used again if the values is different so basically the list needs to be unique to the values is it in.
If the column list needs to be unique and automatically increment, you should be using MySQL's AUTO_INCREMENT
This will automatically populate the column with the next available number on row creation.

Pass data to website and from website

I'm working on one project and having problem with obtaining data from mysql to website and from website to mysql.
It's about getting data from mysql--> rotate data(to pick one value of more)--> show this on website--> count visits and if someone click on some link, track it as +1 to database.
Mysql design for better understanding:
Campaigns Table
-c_id 1 2
-name Campaign 1 Campaign 2
Parameters Table
-p_id 1 2
-name Title Image
-c_id 1 1
Variations Table
-v_id 1 2 3 4
-variation Welcome1 Welcome2 img1.jpg img2.jpg
-p_id 1 1 2 2
-c_id 1 1 1 1
So when php/js file is called it should return 1 variation(Welcome1 or Welcome2 for parameter Title with id 1 and img1.jpg or img2.jpg for parameter Image with id 2) of all parameters(Title, Image).
This data should be added to website with php echo or js document write. (php echo $parameter1; ).
After that there should be onclick on link/s so I can track with which combination user clicked on link. Problem I'm facing here is I don't know how to get and pass which elements was show on website(which variation) and also how to pass.
This will not be placed in same folder and domain as website. Only the server will be same. So I think there won't be problem with accessing database.
I'm not asking for full code of anything just for ideas how to make it the simplest way and also effective.
The database looks a little strangely built, but if that suits you then it should be fine. There are also some other things missing and I am not sure what you are trying to accomplish.
But, when passing the parameters to JS or PHP also pass the ID of the variation, not only the value. When you pass the parameters pass
Title = Welcome1
TitleId = 1
Image = img1.jpg
ImageId = 3
When you create the link that will take you to the other page you should create it passing to it the 2 parameters link?TitleId=1&ImageId=3
In the page that gets process with link count that the visitor got there by pressing on a link with TitleId=1 & ImageId=3

How to Store Multiple Options selected by User in a Table

So I want my users to be able to restrict who may contact them.
There are several factors they should be able to filter, including Age (e.g. Must be between 18 - 29), income (must earn between $25,000 - $60,000), what they're looking for (e.g. Friendship, Hang out, etc.), what drugs they do (Marijuana, Meth, Cocaine, etc), etc.
The problem is, I want them to be able to select and store multiple choices for some of the criteria (e.g. drugs), but I do not know how I should store that in the DB or how I should structure the table to best accomplish that.
For example, how would I store a user's row that for "drugs" chose "Marijuana", "Cocaine", and "Heroin" within this context? Would I simply store those as comma-separated values in the "Drugs" column? Or should I do it in a completely different way?
What would be the best way to do this (considering I will obviously have to retrieve and check this information every time a user wants to contact another user) and why?
No, don't store the values in CSV format in the database. Instead create a join table called user_drug and store one row for each user/drug combination:
user
id name income
1 Foo 10000
2 Bar 20000
3 Baz 30000
drug
id name
1 Marijuana
2 Cocaine
3 Heroin
user_drug
user_id drug_id
1 1
1 2
2 1
2 3
3 3
A DB column (at least theorethically) should NOT hold multiple values. Unfortunately, there are some programmers that store multiple values in a single column (values separated by comma for examples) - those programmers (in most cases) destroy the concept of DB and SQL.
I suggest you to read about Database Normalization to get a start in organizing your tables. And, do your best to achieve the Codd's Third Normal Form
You can try with this:
criterium
------------
user_id type value
1 AGE_MIN 18
1 AGE_MAX 29
1 INCOME_MIN 25000
1 INCOME_MAX 60000
1 DRUGS Marijuana
1 DRUGS Meth

Storing table references in text field (for Agile Toolkit)

I have been experimenting with RDB design lately, and I was wondering about storing items in a field that can have more than one value:
CARS Color_avail
1 corvette 1, 2, 3 <<<<<<<
2 ferrari 2
3 civic 1
COLORS
1 red
2 White
3 black
so on CRUD I would like to add more than one item via a drop down / checkboxes or something that would hold multiple values.
I can see the benefit of displaying the output like this in a form, but do you really want to store it like this in the database ?
For example with a datamodel that holds a comma separated list as in your example, what SQL would you use to identify all the cars available in white ?
The traditional way to hold a many to many relationship like this is to use an additional table e.g. you have a separate table that holds CAR_COLOUR with the following contents
CAR COLOUR
1 1
1 2
1 3
2 2
3 1
So now you can easily query things like, get a list of all cars and colours
SELECT CAR, COLOUR
FROM CARS CA,
COLOUR COL,
CAR_COLOUR CACOL
WHERE CA.CAR=CACOL.CAR
AND CACOL.COL=COL.COLOUR
OR if you just want the white cars, add the following to the WHERE clause
AND COL.COLOUR='White'
an index on the id fields and on both fields in CAR_COLOUR will mean you get great performance even if you have thousands of rows whereas putting them all in a comma separated list in a single field will mean you have to use substr or like which would prevent the use of indexes and mean as the amount of data grows, the performance will degrade rapidly.
Storing relations in the coma-separated list makes sense in some senses. You don't need commas though. There are 2 existing controls which can help you with that.
Displaying list of values with checkboxes in a form:
$form->addField('CheckboxList','corvette')->setValueList($array);
(you can populate array through $model->getRows() although I think it needs to be associative. You can probably join them with var_dump and foreach).
Your other options is to use a hidden field with selectable grid.
$field = $form->addField('line','selection');
$grid = $form->add('MVCGrid');
$grid->setModel('Colors',array('name'));
$grid->addSelectable($field);
$form->addSubmit();
To hide the actual field, you can either use "hidden" instead of "line" or use JavaScript to hide it:
$field->js(true)->hide();
or
$field->js(true)->closest('dl')->hide();
if you need to hide markup around the field too.

Categories