Web Hosting Talk







View Full Version : How to make a database of recent searches


kenfused
07-31-2003, 05:30 PM
Hello
I'm interested in using a search script on my site.

On the side, I thought it would be interesting to list the last 20 searches, (including links to the search results)

I'm using PHP and I could use a MySQL database if needed (I don't really know how to do much besides setting up the database with PHPmyAdmin)

Hopefully, in the search box, the search term would be searched for (using my search script) then teh term would be saved into a list. Then later, I could post that list on the site as well...
Maybe variables $search1 thru $search20, but I'm not sure of the best way to start.

Any suggestiosn would be appreciated
Thanks!

jb4mt
07-31-2003, 11:31 PM
Well yes you can do this, and you're on the right track as far as using PHP and MySQL (or some other form of data storage, such as maybe XML), but WHATEVER you do, don't write a script with variables: $search1, $search2. Instead, learn how to use arrays - any PHP tutorial on the web worth a darn should explain arrays.

kenfused
08-01-2003, 02:54 AM
Hmmm
Thanks
I looked up some basic info on arrays.

Would this be possible:

I'm trying to make a database with a single column with 20 rows.
(I think I can do this with PHPmyAdmin.

From there. I want to know how I (using PHP) can add a value to one of the 20 rows, after 20 items have been added, adding a new value pushes off the oldest one.

Then I want to be able to get the contents of the table as an array.

I'm trying to make a "Last 20 searches" for my website.
I have the search function done, Now I want to add this table of 20 values, that can get added to and updated with each search query.


Also, would there be a simple way to do this, say with a txt file, since I'm only going to have a single table, with 1 column and 20 rows? Would that be simpler?

Thanks!
K

ciqala
08-02-2003, 03:10 PM
you could write all searches to the table and assign an id number to each one

i.e.

| id | search |
| 1 | web hosting |
...
| 20 | Kittens |

then to display the last twenty you can do

select search from table order by id desc limit 20;

and to keep things tidy you could also have a delete script you run once a week or something that simply deletes every thing from the table except the last 20 entries.

kenfused
08-05-2003, 09:17 PM
If I have a table:

||||||||||||||||||||||||||||||||
| 1 | homer simpson |
| 2 | marge simpson |
||||||||||||||||||||||||||||||||

How do I remove the first column of numbers, so that only the text field is removed?

Thanks!

jb4mt
08-05-2003, 10:44 PM
Originally posted by kenfused
If I have a table:

||||||||||||||||||||||||||||||||
| 1 | homer simpson |
| 2 | marge simpson |
||||||||||||||||||||||||||||||||

How do I remove the first column of numbers, so that only the text field is removed?

Thanks!

OK, I'm picking up on lot's of misconceptions here, including some held by those who are answering.

If you are only ever going to have 20 rows of data, I do not see a MySQL table as being the ideal solution. It's just too much database overhead for so little data.

I see a text file as being the solution. And you don't have to explicity state the row numbers. You will be able to implicitly determine the row number by looping over the contents of the file.

In plain english, this means you can just have a text file with contents such as:

Homer Simpson
Marge Simpson
Bart Simpson

etcetera. Then you can increment a counter for each line in the file.

And there's all sorts of other things going on here, at least for a beginner, and you seem to be (i mean no offense by this). You need to be able to add a new entry to the file, and remove the least recently used entry.

There is just so much to cover here I hardly know where to begin. If you decide on my text file based solution, I would check out the documentation on fopen() at:

http://us3.php.net/manual/en/function.fopen.php

kenfused
08-06-2003, 02:54 AM
I took someone's suggestion, and have built a SQL database that will keep on growing, put I am limiting showing just the latest 20 entries.

So far everything is working, including adding search queries to the database.
I just want to remove the number from the rows

Each row has 2 fields
"ID" holds the number that auto_increments
"Term" holds the search term.

I only want it to return the "term"

Thanks