-3

Idea:

I want to do a print layout, my solution is by absolute div's. to fill it, i use php. On every page is a table. On one page is enough space for 10 rows.

So i do the following code to prevent more than 10 rows on a page, the rest gets ignored (im solving this later by a message).

here is the code:

<table>     
<?php $i=1; 
while($info5=mysqli_fetch_array($data5)): ?>    
<?php while($i <'10'):?>    
<tr>     
<td width="50px"><?php echo $i; ?></td>     
<td> foo </td>     
<td> bar </td>     
</tr>     
<?php endwhile; ?>     
<?php  $i++; endwhile; ?>

Unfortunately this code causes firefox, chrome and IE to break. The site starts loading, and then freezes, ending up in a "send crash report".

Why?

AyB
  • 11,609
  • 4
  • 32
  • 47
HKK
  • 237
  • 1
  • 3
  • 17
  • the code should do something while `$i` is smaller than 10, because only 10 rows fit on a page... – HKK May 04 '14 at 08:21
  • 1
    Why not fix your query to only grab 10 rows? – Collin Grady May 04 '14 at 08:24
  • Then i have to fire endless queries? its to create packing lists, these can be 1 Position to 10 000 Positions long, i want to print 10 rows on the first page, and on all following pages 35 rows, unless $i is smaller than mysqli row count, a new page gets inserted all 35 rows .. – HKK May 04 '14 at 08:28
  • Why would you need endless queries? If page 1, pull the first 10 rows. If page 2, pull rows 11-45, and so on. – Collin Grady May 04 '14 at 08:29
  • how to find out how many rows there are? `$data5=mysqli_query($link,"SELECT * FROM bookings WHERE BI_ID = '$b_id' ORDER BY amount");` that is the query. i don't know how many rows there will be! – HKK May 04 '14 at 08:30
  • and isn't it better for the sql server to just make one query? than do so many? what about overload? – HKK May 04 '14 at 08:31
  • What are you talking about? You are displaying 1 page from 1 request. If you load all the data on every page, even when only showing 10-35 rows, you're doing way worse for performance. – Collin Grady May 04 '14 at 08:32
  • `SELECT * FROM bookings WHERE BI_ID = '$b_id' ORDER BY amount LIMIT 10` <- page 1; `SELECT * FROM bookings WHERE BI_ID = '$b_id' ORDER BY amount LIMIT 10,35` <- page 2; and so on – Collin Grady May 04 '14 at 08:34
  • no, my way: 1 Query, all the data has been transferred, your way: every page fires a new query, for eg.10 pages that would make 10 queries in about 2 seconds, depends on php speed.? – HKK May 04 '14 at 08:35
  • Your way is impossible. You cannot run one query, load all the data, and reuse that data between page loads. – Collin Grady May 04 '14 at 08:35
  • @HKK You can have a GET variable like `www.mysite.com/abc.php?page=3` and then on abc.php, you need to modify your query to paginate the results. refer: http://stackoverflow.com/questions/3095474/php-mysql-pagination – Haywire May 04 '14 at 08:35
  • i think i wrote page wrong, page is not a php page, its a print page. and the pages are all div's under each other, like a word document. – HKK May 04 '14 at 08:38
  • if div 1 is full, the code will jump to the next div and fill in there, and there is a loop `$i < $msqli_rowcount` – HKK May 04 '14 at 08:39
  • @HKK Lets say you have 100k or 1000k or more rows in the database. now you run your version of the query and the site starts crawling like a snail because your database is busy executing WHERE clauses on all those millions of rows and does not finish until the the rows have been processed. whereas Collin Grady's query stops at 35-10 = 25 (limit-offset=rows count) results set and displays the data to the user immediately. – Haywire May 04 '14 at 08:40
  • BUT all my DIV's are on the SAME page, so all the queries with offset will be fired after eachother, with a delay of a few ms, the time php takes to "echo" the content of the first div... – HKK May 04 '14 at 08:45

1 Answers1

4
 $i++;

Should be inside the inner while, otherwise $i always remains 1 and that generates an endless loop and that is what causes your page to crash.

Like this

 <?php  $i++;?>
 <?php endwhile; ?>     
 <?php endwhile; ?>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95