1

I'll try to explain this as best as possible.

I have data coming in from a MySQL database via a PHP foreach loop. It loops through 50+ records and displays data based on different variables. I want to stop the loop after say, 12 records, and then click a button to display the next 12, and so on.

Is there a way to stop, or pause a foreach loop on a certain iteration, then click a button to "play" the loop again for 12 more iterations?

Below I've posted a simplified example of this foreach loop.

foreach ($view->likeDataSet as $productData){

echo '<p class="pName">' . $productData->getPname() . '</p>';

}

This will load the name of a product I have in my database, and continue to do this until all records

Any help would be greatly appreciated, thanks!

Mayzed
  • 75
  • 1
  • 8
  • If by "play" you would permit a page refresh, then yes. If by "play" you want it to NOT refresh the page, but just add more records, then you're talking a more complex AJAX solution. – random_user_name Feb 03 '17 at 17:51
  • How would you do it via a page refresh? If I can figure that out, I can just wrap it around an AJAX function that does the refresh behind the scenes can't I? – Mayzed Feb 03 '17 at 17:55
  • Pagination could be a option in this case, retrieve just data that you want to show. – rescobar Feb 03 '17 at 18:00

2 Answers2

2

You have two options:

  • page refresh where you'll past some GET parameter to the page and the foreach loop will show more records based on that parameter.

Simplified example:

$itemCount = $_GET['showitems'];

$mysqli = new mysqli(sqlhost, user, bassword, db);
$stmt = $mysqli->prepare('SELECT * FROM products LIMIT ?'); //prepare the statement
$stmt->bind_param('i', $itemCount);                         //bind parameters to the statement
$stmt->execute();

$result = $stmt->get_result();

...
your logic to transform the data into the $view->likeDataSet structure 
...

foreach ($view->likeDataSet as $productData){
  echo '<p class="pName">' . $productData->getPname() . '</p>';
}

echo '<a href="yourcript.php?showitems='.($itemCount + 10).'">Get next 10 rows</a>';
  • more complex option where you'll modify actual page content with javasript and AJAX. So your php script shoud generate whole page code for the first time and then only part of the page with additional records you want to add when you click your "play" and of course javascript function under this "play" button that will make AJAX request and place the data into the code
Jan Rydrych
  • 2,188
  • 2
  • 13
  • 18
  • 1
    OK, great! Unfortunately, I cannot support any answer with the `mysql_*` library, so can you switch it up quickly to show proper `mysqli_` with binding? Or PDO? – random_user_name Feb 03 '17 at 21:22
  • I'm used to type-check and escape variables way before using them in the SQL statement like in this example, so sorry for the simplification before. I understand your demands to show the proper way for learning porposes ;-) – Jan Rydrych Feb 04 '17 at 18:55
  • 1
    No worries! We just want to be sure future visitors are seeing good, best practices with database access! +1 – random_user_name Feb 04 '17 at 19:51
0

You should take a look at DataTables assuming that you can use jQuery in your application. It can either take all results at once and allow you to paginate, filter etc on the whole list, or use with server-side code to pull in data in chunks

cEz
  • 4,932
  • 1
  • 25
  • 38