Hello everyone. Can anybody guide me to a tutorial or so or give some tips on how i could display in my website, recent searches displayed as links? So whenever a search is made, the newest is listed on top etc. Website is made with php/mysql/html Help would be much appreciated. Thanks, Seb
One way of doing it is something like this, assuming you already have a MySQL table with your site search queries, containing info about: search query (string), date/time (i prefer UNIX timestamp, a lot easier to work with) and optionally count (the number of times that search query was run, can be used in many ways): $query = @mysql_query("SELECT query FROM search_queries ORDER BY time DESC LIMIT {$number_of_queries_to_be_displayed}"); while($row = @mysql_fetch_assoc($query)) { $query_url = "http://yourwebsite.com/search.php?query={$row['query']}"; // 'ucfirst' converts all first letters into uppercase, optional $query_text = ucfirst($row['query']); $search_queries[] = "<a href=\"{$query_url}\">{$query_text}</a>"; } // Now you can make a usable template part out of it by doing something like $search_queries_html = implode(' ', $search_queries); // or $search_queries_html = "<ul><li>".implode("</li><li>", $search_queries)."</li></ul>"; PHP:
How many recent searches? If it's one you could have one variable in one table that would be replaced anytime a search is done. For multiple recent searches: You could simply have a search added to a basic mysql database and just set ID indexing. <?php //search is made and submitted if (isset($_POST["var"])){ $var = $_POST["var"]; mysql_query(INSERT INTO table SET var = 'var', x = 'x'...); } ?> Then do a query to the database and order by ID desc (last first) and limit the number of returns. $var2 = mysql_query("SELECT * FROM table ORDER BY 'ID' DESC LIMIT 10"); while ($readvar2 = mysql_fetch_array($var2)){ echo $readvar2["search"]; }
Thanks guys, knew i had to save the actual search. There is no way of doing it without mysql right? I want to display latest 10 searches. When the query is submitted into the database is there a way to lets say: - 10 searches made and successful stored in the database - 11th is made, which replace the oldest row in the search table Thanks again, will try out those 2 methods Seb
surely you have. If you want to save only the successful searches (namely those returned some results) then you have to run the insertion query after results check, such as: if ($num_searchresults > 0) { mysql_query("insert into searches values (..... )"); } and if you want to limit results to ten, simply use CREATE sequence with the searches table. CREATE SEQUENCE "search_num" start 1 increment 1 maxvalue 5 minvalue 1 cache 1; this will rotate the number of rows to 5 in the table.
if you dont use MySQL ..you'll be saving to text files or something , and you'll end up doing as much work and inevitably trying to make a text file behave like a database anyway. Better off to use MySql database and give them a user name, or IP combination (IP not always good idea because of people on dial-up having multi-IPs) then check the search database, see if there are at least 4 there, if not, save the new one. If there are 4 there already .. delete the one with the oldest time , and add the new one. Then it will do a base query and display script to show their most recent searches.
Hmm iam stuck here lol this is my current query which submits to the database using Unix timestamp and an if to see if the num of rows is 10 $search_date = time(); $result = $DB->query("SELECT * FROM {$CONF['sql_prefix']}_search ORDER BY search_date DESC", __FILE__, __LINE__); $rowtotal = mysql_num_rows($result); if ( $rowtotal < 10 ) { $DB->query("INSERT INTO {$CONF['sql_prefix']}_search (search_query, search_text, search_date) VALUES ('{$TMPL['query']}', '{$TMPL['query']}', '{$search_date}')", __FILE__, __LINE__); } PHP: Or if num rows == 10 delte from blabla where date = ???? else insert into Question here is i guess how do i determine the oldest entry? Which is the code for date = oldest?
You would need something like // Two ways of grabbing the oldest, or last inserted row. Using a PHP function $oldest = mysql_insert_id(); // OR doing a query on the database $sql = "select * from TABLE_NAME order by date DESC limit 1"; if($rowtotal >= 10) $sql = "delete from TABLE_NAME where date = '$oldest' limit 1"; else $sql = "insert . . ."; PHP: Something like that
Ok guys, thanks everyone deletion and replacement work now, but its spitting out an error once the firs entry gets deleted this is the line $DB->query("DELETE FROM {$CONF['sql_prefix']}_search WHERE search_date <= '$search_date' ORDER BY search_date ASC LIMIT 1"); PHP: Error Warning: Missing argument 2 for sql_mysql::query(), called in /home/anachron/public_html/sources/search.php on line 74 and defined in /home/anachron/public_html/sources/sql/mysql.php on line 42 Warning: Missing argument 3 for sql_mysql::query(), called in /home/anachron/public_html/sources/search.php on line 74 and defined in /home/anachron/public_html/sources/sql/mysql.php on line 42 PHP: Someone know why it does that? Everything works, just those nasty error destroy my page -.-
Check sql_mysql class for 'query' method definition. It seems it needs three arguments. I dont know why you stuck to that class. PHP has native support for mySQL Example from oficial php site <?php // Connecting, selecting database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Could not select database'); // Performing SQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); ?> PHP:
And what is the problem now? iam very new to php. This is my complete code // Recent Searches $search_date = time(); $result = $DB->query("SELECT * FROM {$CONF['sql_prefix']}_search ORDER BY search_date DESC", __FILE__, __LINE__); $rowtotal = mysql_num_rows($result); if ($rowtotal >= 10) { $DB->query("DELETE FROM {$CONF['sql_prefix']}_search WHERE search_date <= '$search_date' ORDER BY search_date ASC LIMIT 1"); $DB->query("INSERT INTO {$CONF['sql_prefix']}_search (search_query, search_text, search_date) VALUES ('{$TMPL['query']}', '{$TMPL['query']}', '{$search_date}')", __FILE__, __LINE__); } else { $DB->query("INSERT INTO {$CONF['sql_prefix']}_search (search_query, search_text, search_date) VALUES ('{$TMPL['query']}', '{$TMPL['query']}', '{$search_date}')", __FILE__, __LINE__); } // End Recent Searches PHP:
Seems like you might be going a long way round to do a relatively simple job, why don't you try just doing // Recent Searches $result = $DB->query("SELECT * FROM {$CONF['sql_prefix']}_search ORDER BY search_date DESC LIMIT 10", __FILE__, __LINE__); $rowtotal = mysql_num_rows($result); } // End Recent Searches PHP: This should simply return the 10 most recent search results, sorry if I've missunderstood but this seems much simpler
yes its simplier i know that very well and had that before. But my database gets stuffed with way to many rows then. And because i simply want to display 10 results, why should i overload my table. Thats why i only want 10 rows in my table. I mean all that works, so the query is doing its job but it spit out those 2 errors ( query works even with those errors ). Dont know what they mean, which arguments.. are they generate because of WHERE search_date <= '$search_date' ??? Dunno how to do it else.
WHERE search_date <= '$search_date' Code (markup): try it without the apostrophes as apostrophes usually suggest a string and you cant use a <= with a string so: WHERE search_date <= $search_date Code (markup):
Hmm, no thats not it. Still the same error. Even if i remove that part completly, still the same. Any more ideas?