One Question on PHP and MySQL

Few days ago, Sir AnthonySterling replied to my question to make feed from static page.
I am thankful to him.

Well, now I am eager to know this,
If I am making a search page, and I simply want to show recent 10 searches, then how could I do that?

I don’t want to store all data going into the sql database.
Just the recent 10 ones should be stored and if the data has 10 records already the old data should be deleted and new 1 inserted.
Is there any way to do this?

Thanks.

you can create a table to store the searches.

before each insert, count the rows in the table. if the number = 10, then delete the oldest row before inserting the new search.

Thanks for your help.
But, if I do that, then the IDs would vary automatically.

I mean, ID=1 to 10 are assigned at first time of allocation.
Than when I insert new one after deleting the 10th item, then the ID would be 11, not 1.
This is not good in SQL…

Or is there any other method to sort out this problem also?

save your searches, but don’t delete any

instead, just always retrieve the latest 10

you will thank me later :slight_smile:

you didn’t mention any conditions on the ID in your 1st post.

in your 1st post you said the old data should be deleted.

it would help if you explain why the id must be from 1-10 at all times after 10 rows have been reached for your application.

Here, something to play with. :slight_smile:


<?php
$store = 'searches.txt';

function get_searches($store){
  return array_splice(array_reverse((array)file($store)), 0, 10);
}

function save_search($search, $store){
  file_put_contents($store, $search . "\
",  FILE_SKIP_EMPTY_LINES | FILE_APPEND);
}

if('POST' === $_SERVER['REQUEST_METHOD'] && false === empty($_POST['search_value'])){
  save_search($_POST['search_value'], $store);
}
?>
<html>
  <head>
    <title>Last 10 Searches</title>
  </head>
  <body>
    <h4>Last 10 Searches</h4>
    <ul>
      <?php foreach(get_searches($store) as $search): ?>
        <li>
          <?php echo htmlentities($search); ?>
        </li>
      <?php endforeach; ?>
    </ul>
    <form action="" method="post">
      <input type="text" name="search_value" />
      <input type="submit" name="search_button" value="Search" />
    </form>
  </body>
</html>

Wow Sir,
you are superb… :smiley:

Thanks you sir for sorting out my problem again this time.
And also, My Special Thanks to r937 and Kalon, because you are also helping me everytime and suggesting me the new ways.

Hi Sir,
Sorry to disturb you again, but I am unable to find my answer anywhere on the internet, and I know you can help me.

Sir, 1 similar Query to above is there,
As you told above, this is perfect in file handling.

Now if it could be possible in the database as well, it will be much better sir. :slight_smile:

If I have data in a record in MySQL Column, and I want to update the new line of data, without removing the old one.
How to do this?


I also tried to read the contents (which were inserted by me manually) this in mysql, but I am only getting the Array[0], not further.

$result_set = mysql_query("select * from photos where id = 1");
$found_photos = mysql_fetch_array($result_set);
$recent = array_splice(array_reverse((array)nl2br($found_photos['image_gallery'])), 0, 10);
print_r($recent);

In the image_gallery column, this is the the images are stored line by line (1 image per line).

UPDATE daTable
   SET Column1 = CONCAT(Column1,'New Data')
 WHERE id = 1

Thank you r937 sir.
It worked…