Hi..

I need help..

I have page address :
http://localhost/britis/intra/intdetail.php?lap_id=1

this will be show complete query from mysql database and while i change lap_id at the address of the browser, tha content should be change.

my question:

how can I create php script in button 'onClick' to go to Next ID and back To previous ID that will be change the lap_id to next or previous data.
<< Back Next >>

Best Regards,
Erick

    if (isset($_GET['lap_id']))
    {
    	$lapid = (int) $_GET['lap_id'];
    	if (isset($_GET['direction']))
    	{
    		if ($_GET['direction'] == 'previous')
    		{
    			$q = sprintf('SELECT field, stuff FROM table '.
    					'WHERE lap_id < &#37;d'.
    					'ORDER BY lap_id DESC, LIMIT 1', $lapid);
    			# test code to decrease lap_id. This row should be removed
    			--$lapid;
    		}
    		elseif ($_GET['direction'] == 'next')
    		{
    			$q = sprintf('SELECT field, stuff FROM table '.
    					'WHERE lap_id > %d '.
    					'ORDER BY lap_id ASC, LIMIT 1', $lapid);
    			# test code to increase lap_id. This row should be removed
    			++$lapid;
    		}
    	}
    	else
    	{
    		$q = sprintf('SELECT field, stuff FROM table WHERE lapid = %d', $lapid);
    	}
    }
    else
    {
    	$q = 'SELECT field, stuff FROM table ORDER BY lapid ASC LIMIT 1';
    
    # test code to initialize lap_id. This row should be removed
    $lapid = 5;
    }
    
    $r = $db->query($q)
    
    # if no row is found, you've reached either max or min.
    # if (isset($lapid)) perform new query with lap_id = $lapid
    # else use the last query above.
    
    
    printf('<div>%s</div>', $q);
    printf('<div>lap_id: %d</div>', $lapid);
    
    printf('<div><a href="?lap_id=%d&amp;direction=previous">&lt;&lt;</a></div>', $lapid);
    printf('<div><a href="?lap_id=%d&amp;direction=next">&gt;&gt;</a></div>', $lapid);
    

    Do note that while working, this approach is highly inefficient. If you are viewing the first row and want to get the next row, the DB has to retrieve all rows but one, order them, and return the first of those rows. If you have few rows, this will work fine, but if you have many rows, this query will take too much time.
    If speed is a problem, each row would need to store lap_id for next and previous rows so you can

    SELECT t.field, t.stuff
    FROM table AS t
    INNER JOIN table AS current ON current.previous = t.lap_id
    

    Now selecting the next or previous row becomes trivial, but extra care must be taken on deletes. You'd first have to SELECT previous, next FROM the row you are about to delete, and then update the next row's previous field with the selected previous value, and the previous row's next field with the selected next value.

      Write a Reply...