Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hi I am new to web technology (Well not advanced). I am trying to build an online store (computer hardware) with mysql and PHP, I am wondering how to add a search functionality (not google's).
I am planning to make a search bar where visitors can enter key word or key words for search.
The search for these key words should span many tables with totally different content.
I know about SQL syntax, I have a good understanding of REGEXPs, I am good with indexes and views...
The only thing I want is guidance, a general idea.
you should first design your database. then make website design and program it in PHP.
as far as search functionality concerns you should make something like that,
eg.
database and tables and their columns etc..
for example, if you have one table named hardwares
+--id---+---Name----+---Cost----+-Warrenty--+
+-------+-----------+-----------+-----------+
| 1 |hardware1 | 2000 | 2 |
| 2 |hardware2 | 5000 | 1 |
| 3 |hardware3 | 5000 | 3 |
+-------+-----------+-----------+-----------+
then in coding part of the website there will be query fired something like that,
select * from hardwares where Name LIKE '%$search_input%`
here, the search input is taken from the user and this query will result the particular hardwares' information and then from the results you can get ID of that hardware which is already stored in that table.
from that ID, you can make a page that will accessed by a particular query for example,
http://www.yourwebsite.com/hardwares.php?id=2
this page will load that particular hardwares' page and it will have all the information regarding to that hardware.
Search from catalog items - this is search for database.
MySQL code
SELECT nameItem FROM catalogItem WHERE `nameItem` LIKE '%search phrase%' OR `descriptionItem` LIKE '%search phrase%'
This is the simplest example.
A architecture of search I would do with caching results in a separate table.
PS See how to implement search in popular CMS
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am creating a system where users (who are identified by a user id number) will be allowed to vote on posts (think Reddit, StackOverflow, etc).
Users can vote a post up or not vote at all on it.
The number of votes on a given post can easily be stored within the table containing the posts.
Keeping track of who has voted, however, is a different task entirely that I'm not sure how to approach.
I was thinking I could have a table that would have two columns: user id and post id.
When they vote on a post, I add their user id and post id to that table. If they unvote, I remove that entry from the table.
EG:
User ID | Post ID
1 | 3949
1 | 4093
2 | 3949
etc...
Is this a reasonable solution?
Yes this is reasonably simple and easy solution to the problem. You can do the same for your comments(if you like to). In your MAIN_POST table assign a post_id and use this same post_id in other tables (comments(post_id, user_id, post_comment, comment_time) and votes(post_id, user_id, vote_status(you can use 1 for vote up and 0 for vote down))). It will complicate your sql queries, to retrieve data, a little but you can do it. And on android side there are alot of tricks to handle and furnish this data in application and you can make this vote(like) and comments idea just like facebook (YOU for your comments and likes and NAMES for others).
I wouldn't remove rows from the table. I understand why you would want to do that, but why lose the information? Instead, keep a +1/-1 value for each entry and then sum up the values for a post:
select sum(vote)
from uservotes
where postid = 1234;
And, I agree with Rick that you should also include the creation date/time.
Using an 'in between' or 'joining' table is a perfectly acceptable solution in this case. If relevant you could even add a timestamp to the relation and show to the user when a user has upvoted something.
Also it is important to take care of proper Indexes and Keys to have your table structure also perform properly once the dataset grows.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently trying to get values from tables I have in a webpage into a database using PHP. However, the order of the values inside each box is important, so I want the first box to be ranked 1 and the second box to be ranked 2 and so on... There is no limit to the number of items in each box. There may be as many as 100 or 0. Each item in each box is dragged from a bank of items into the table, which represents a topic. The tables are the output of the interface.
So example table:
Rank1
Rank2
Rank3
I've currently tried dumping the entire page once the user fills it in into a text file and parsing it from there but i'm looking for a more functional and practical way of doing it.
If I understand your goal correctly, you have two sections on your webpage. A word bank and a <table>. A user can drag and drop items from the word bank to a cell in your <table>. Next you want the order of the contents of the cell to be preserved when the data is saved to a database-table.
Given the description you have provided, it seems you may be new to using databases. It is not a problem but you will need to do some study on how databases work and how to use them.
Again, if I understand correctly, your <table> looks like the following:
| topic-1 | topic-2 | topic-3 |
-------------------------------
| ---A--- | ---J--- | ---V--- |
| ---B--- | ---J--- | ---X--- |
| ---C--- | NULL | ---Y--- |
| NULL- | NULL | ---Z--- |
To move this into a database you need to create a database, and some table, for example MyDataTable. Next that table may have three columns: id, contents, category.
Here is some info on Mysql Insert.
INSERT INTO MyDataTable(`id`, `contents`, `category`)
VALUES (1, A, Topic-1)
You will need to format that call to work correctly via php, but that is the general idea. In your php code you will need to iterate over the elements in the order you want, and you can use the id to refer to your order... Alternatively you could add another column to your database-table and have that refer to your order, which would allow you to use some other value, perhaps auto-increment, as the primary key.
I would recommend becoming comfortable with MySQL if you aren't already, before tackling your project's specific needs. Namely, INSERT, DELETE, UPDATE, SELECT and WHERE will almost certainly be necessary MySQL functions.
One resource I found very helpful in grasping MySQL is this book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming .
Using the phpMyAdmin page can be very helpful in debugging SQL queries, once the query syntax is correct, you can translate it to php and incorporate your dynamic variables. Also if you store your sql-syntax in php as $sql, using var_dump($sql) can also be a great help when debugging sql syntax.
In programming, iterate over, typically refers to creating a for-loop to iterate, or step through 1-by-1, each of the items of an array or similar container.
$MyArray = array();
$MyArray = GetTableContents(); //You have to write this function.
for ($idx = 0; $idx < count($MyArray); $idx++)
{
// do my stuff, like, Insert into my DB-table.
}
In order for the code above to work, you need to fill MyArray with the contents from your <table>. In the example code above, the $idx, could be used as your id.
In addition to MySQL, it seems it would also be worth your time to learn more about php programming. Read simple tutorials, how-to's, and books. There are numerous resources on the internet about php. One such example is here.
I hope this is helpful.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Such a good day, everyone is well, the question is the following.
I think a couple of tables in mysql, I sometimes get data from both research I come across the typical "link two tables", "make two queries to two tables", but this has me confused.
That is, if we have two tables, as follows:
Tabla1
| id | nick |
|-------------
| 1 | admin|
tabla2
| id_post | content | autor |
|---------------------------------
| 100 | asdasd | 1 |
Why relate from mysql, but when you query you can do:
select tabla1.nick, tabla2.* from tabla1, tabla2, where id="1" and tabla2.autor = tabla1.id
What is the difference between the two?, Or what is the benefit to having one or the other?
If I understand your question correctly, you're asking about constraints, why actually make column X on table A refer to column Y on table B, when you can just join the two tables in a SELECT query?
This is to enforce referential integrity, to reduce redundancy, etc. Doing so makes the data itself reliable so that when you use joins in your SELECT statement, they work as they should.
If you had an ASSIGNMENTS and a SUPERVISORS table, for instance, and each assignment is always assigned to a supervisor on a supervisors table, a foreign key constraint between the supervisor field on ASSIGNMENTS and SUPERVISORS will ensure that happens. It also gives you flexibility as to what should occur if the supervisor value changes on one table (should it be restricted? should the change be carried through to the other table? etc.)
Without the relationship being defined, an assignment might be assigned to a supervisor who does not even exist. And then the results of your SELECT statements won't be all that reliable...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
As part of a bigger project, I'm building a reporting application which analyses business data. The aim is to have default datasets and also allow for users to upload their own datasets.
I'm building this in PHP however I'm not sure what the best database is to use. MySQL is great and usually my first pick, but this is application does not really require a relational database for the datasets. Datasets could be thousands of columns and millions of rows. Getting this to work MySql is quite a challenge.
I've seen HDF5 which seems like a good database though not very well supported from a web application point of view.
Do you have any other suggestions for a database that can store such large datasets?
P.S - I should clarify the reason for the title.... I need to be able to allow users to upload datasets. The only way with MySQL would be to allow for a table to be created each time the users uploads a dataset. Doesn't really sound very good.
You should look into NoSQL databases. That sounds like what you are looking for. With NoSQL you don't have to create databases but store what ever and still be able to search the data fast.
If I understand you correctly, you need to create a sub table ( or something similar to this ) and store the custom fields in there:
Table name: custom_fields
+---------+----------------+----------+
| user_id | field name | value |
+---------+----------------+----------+
| 10 | favorite_sport | hockey |
| 10 | favorite_food | hot-dogs |
| 20 | sisters_name | Ashley |
+---------+----------------+----------+
And after this, just join the data together and show what you need.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Today I was working on my website and I asked myself a simple question.
Does storing an array with all informations is better than saving those one in different fields?
For example if I store a word, a password and a number in one field on the database in this way
+-------------+----------------------------------------------------------------+
| Field | Value |
+-------------+----------------------------------------------------------------+
| all | ["test","fa26be19de6bff93f70bc2308434e4a440bbad02","25468684888"] |
+-------------+----------------------------------------------------------------+
Is it better than saving it in this way?
+-------------+------------------------------------------+
| Field | Value |
+-------------+------------------------------------------+
| word | test |
| password | fa26be19de6bff93f70bc2308434e4a440bbad02 |
| number | 25468684888 |
+-------------+------------------------------------------+
I think that the first method is faster than the last one because you need only to SELECT one field and not three or more. What do you think about it?
The second method. By far.
You should never put more than one piece of data into a single column.
A single row of data shuld contain all the information you need:
id name password
1 Fluff itsASecret
2 Flupp Ohnoes
Basically, it has to do with updates, selects, searches and pretty much everything that databases do. They are made to do it on single columns, not little bits of data inside a string.
Taking your example, how do you update the password? How do you put an index on the user ID?
What if you also had a bit of data called "NumberOfVotes" If you had it all in one column in a pseudo-array, how do you get a tally of all the votes cast by all users? Would you REALLY want to pull each entry out into PHP, explode it out, add it to the running total and THEN display how many votes have been cast? What if you had a million users?
If you store everything in a ingle column, you could do a tally really easily like this:
select
sum(NumberOfVotes)
from
yourTableName
Edit (Reply to faster query):
Absolutely not, the time it takes to compelte a query will come down to two things:
1) Time it takes to execute the query
2) Time it takes to return all the data.
In this case, the time it takes to return the data will be the same, after all, the database is returning the same amount of bytes. However, with tables that are properly set up, just FINDING the right data will be faster by orders of magnitue.
As an example of how difficult it would be to simply USE a table that has the various bits of information all mumbled together, try to write a query to update the "number" value in the row that starts with the word "test".
Having said that, there are possibly some potential cases where it can in fact be okay to store multiple "fields" of data in one column. I once saw (and copied) an exceptionally interesting permissions system for users that stored the various permissions in binary and each digit in the number equated to being allowed/not being allowed to perform a certain type of action. That was however one interesting example - and is pretty much what I would call an exception that proves the rule :)
I think that the first method is faster
is your main problem actually. You are comparing solutions from only "is it faster" point of view. While you have no measure to tell if there is any difference at all. Or, if even there is, if such a difference does matter at all. So, the only your reason is a false one. While you completely overlook indeed important, essential reasons like proper database design.
Saving in separate fields is a lot more flexible as you are then able to easily search/manipulate data using SQL queries, whereas if they were in an array you would frequently find yourself needing to parse data outside SQL. Consider the following example:
+-------------+----------------------------------------------------------------+
| Field | Value |
+-------------+----------------------------------------------------------------+
| all | ["1","fa26be19de6bff93f70bc2308434e4a440bbad02","25468684888"] |
+-------------+----------------------------------------------------------------+
Using the above table, you need to find the number field for the user with id 1, however there is nothing to search for, you can't simply to a query for the value 1 somewhere in the all field, as that would find every instance of the number 1!
You'll also encounter this problem when changing data in your DB, as you'll have to get the current array, parse it, change the value, then reinsert it.
Also you'll need to put some form of ID as a field to act as a primary key.
However with separate fields for each value, it's fairly simple:
+-------------+------------------------------------------+
| Field | Value |
+-------------+------------------------------------------+
| id | 1 |
| password | fa26be19de6bff93f70bc2308434e4a440bbad02 |
| number | 25468684888 |
+-------------+------------------------------------------+
SELECT `number` FROM mytable WHERE id = 1
The second option is better because its more readable and maintainable.
If someone who didnt write the code has to maintain it, the first option is terrible.
If you ever need to change a field, or add a field, likewise, the first option is a nightmare.
The second option requires much less work.
Keep it simple!
I think given example is trivial and that's why answer for specific example is 2nd method. But there are time's when first method is far more easy to implement. For example you create pages for website dynamically from admin panel, and in start you don't know all the values that will be used in every page. So you put general options like in 2nd method, and put something like page_data and use it to store serialized object. Now you should use serialized object for data that are not likely to change individually, as they are treated as single piece of data.
In your code you fetch serialized object, do unserialize and use them as normal. This way you can add page specific data that are not generalized for every page, but still the page's are the same.