hi all PHP masters and newbies
i have a question about pagination. this news script displays 4 little images and links at the right side and one large image at the left side. and all the links submitted shows below.
i need let at the page displays 8 news with little images and pagination. for ex i have 100 news let it displays latest 8 of them and other will be enabled by click next or 2 page (pagination simly)
thanks for attention

<?php
	require_once('auth.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bismillah</title>
<script type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<p>
<? 
include("connect.php"); 

include ("image_edit.php"); 
?>

</p>
<p style="font-size:24pt; color:#FF0000;">add news</p>

<hr />
Mesajlar :&nbsp;
<? //

$no=$_POST[no];
$title=$_POST[title];
$subject=$_POST[subject];
$explanation=$_POST[explanation];
$aktif=$_POST[aktif];
$search=$_POST[search];
$show=$_POST[show];

// image ayarlari burada
	$large_image="haber_kucuk"; // open this folder
	$little_image="haber_buyuk"; // give mode chmod 777

	$large_image_en=300;
	$large_image_boy=200;

	$little_image_en=100;
	$little_image_boy=75;
	
	
	
//if ($image)

?>
<?  if ($_POST[delete])
{// if isset delete button
$query=mysql_query("select * from haber where no='$no'");
while($row=mysql_fetch_row($query))
	
	{ @unlink($row[4]); // 
		@unlink($row[5]); // 
	}

if (mysql_query("delete from haber where no='$no'")) echo " deleted";
else echo "not deleted";

}
?>
<? 
if ($_POST[edit])
{
//
$query=mysql_query("select * from haber where no='$no'");
	while($row=mysql_fetch_row($query))
	{ 
	$no2=$row[0];
	$title2=$row[1];
	$subject2=$row[2];
	$explanation2=$row[3];
	$littleimage2=$row[4];
	$image2=$row[5];
	$aktif2=$row[7];
	}
}
?>
<? if($_POST["add"])
{ 
if(strlen($_FILES["image"]["name"])>4)
		{
		$find=$_FILES["image"] ;
		$to_be_finded = array('ç','Ç','ý','Ý','ð','Ð','ü','ö','Þ','þ','Ö','Ü',',',' ','(',')','[',']'); 
		$change  = array('c','C','i','I','g','G','u','o','S','s','O','U','','_','','','',''); 
	
		$_FILES["image"]=str_replace($to_be_finded, $change, $find);
		

		$image = "$haber_large_image_klasor/".$_FILES["image"]["name"]; 	   
		move_uploaded_file($_FILES["image"]["tmp_name"],$image);
		
		$littleimage  = "$haber_little_image_klasor/".$_FILES["image"]["name"];  
		move_uploaded_file($_FILES["image"]["tmp_name"],$littleimage);

		
		// little image resampling
		$icerik = resample($image,$little_image_en,$little_image_boy); 
		$folder  = fopen ($littleimage,"w+"); 
				   fwrite($folder,$icerik); 
				   fclose($folder); 
				   
		// large image resampling
		$icerik = resample($image,$large_image_en,$large_image_boy); 
		$folder  = fopen ($image,"w+"); 
				   fwrite($folder,$icerik); 
				   fclose($folder); 

	
		
		
		
		}


if (!$no)
	{
		
	if (mysql_query("insert into haber values ('','$title','$subject','$explanation','$kucukimage','$image','$aktif')"))
	{echo " changed"; $image="";$littleimage="";}
	else echo "not changed!";
	
	}
else 
{ //
	if ($image) $imageek="image='$image',littleimage='$littleimage'";
	if (mysql_query("update  haber set title='$title',subject='$subject',explanation='$explanation',aktif='$aktif'$imageek where no='$no'"))
	{echo "added"; $image="";$littleimage="";}
	else echo "not added";
	}
}
?>
<hr />

<form id="form1" name="form1" method="post" action="">
News :
  <label>
  <select name="no" id="no">
  <?
  $query=mysql_query("select * from haber order by no desc");
while($row=mysql_fetch_row($query))
{
echo "<option value='$row[0]'>$row[1]</option>";
}
  ?>
    
  </select>
  </label>
  <label>
  <input name="edit" type="submit" id="edit" value="edit" />
  </label>
  <label>
  <input name="delete" type="submit" id="delete"  value="delete" />
  </label>
  <label></label>
</form>
<hr />
<form id="form2" name="form2" method="post" action=""   enctype="multipart/form-data">
  <table width="250" border="1" cellspacing="5" cellpadding="0">
    <tr>
      <td width="80">no 
      <input name="no" type="hidden" value="<? echo $no2; ?>" /></td>
      <td width="100"><label></label>	  </td>
    </tr>
    <tr>
      <td>image
 <? if($littleimage2)
 
{echo "<img src=$littleimage2 align=right><br />";

}
?>
</td>
      <td>   <label>
      <input name="image" type="file" id="image" />
      </label></td>
    </tr>
    <tr>
      <td>title</td>
      <td><input name="title" type="text" id="title" value="<? echo $title2;?>" size="50" /></td>
    </tr>
    <tr>
      <td>subject</td>
      <td><input name="subject" type="text" id="subject" value="<? echo $subject2;?>" size="50" /></td>
    </tr>
    <tr>
      <td>explanation</td>
      <td><label>
        <textarea name="explanation" cols="80" rows="5" id="is"><? echo $explanation2;?></textarea>
      </label></td>
    </tr>
    <tr>
      <td>Aktive</td>
      <td><label>
        <input <?php if ($aktif2=="aktif") {echo "checked=\"checked\"";} ?>  name="aktif" type="radio" value="aktif" checked="checked" />
      aktivate 
      <input <?php if ($aktif2=="pasif") {echo "checked=\"checked\"";} ?>  name="aktif" type="radio" value="pasif" />
      Passivate</label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name="add" type="submit" id="add" value="add" />
        <label>
        <input name="search" type="submit" id="search" value="search" />
        <input name="show" type="submit" id="show" value="news show" />
        </label></td>
    </tr>
  </table>
</form>

<p>&nbsp;</p>
<? 
if (($search) || ($show))
{
if ($search)
{
if ($title) $query=mysql_query("select * from haber where title like '%$title%'");
if ($subject) $query=mysql_query("select * from haber where subject like '%$subject%'");
if ($explanation) $query=mysql_query("select * from haber where explanation like '%$explanation%'");
}
 if ($show)
{
$query=mysql_query("select * from haber order by no desc");
}
echo "<table border=1>
<tr>
<td>Edit</td>
<td>delete</td>
<td>No</td>
<td>title</td>
<td>subject</td>
<td>explanation</td>
<td>littleimage</td>
<td>aktive</td>
</tr>";
while($row=mysql_fetch_row($query))
{echo "
<tr><form action=\"\" method=\"post\"><input name=\"no\" type=\"hidden\" value=\"$row[0]\">

<td><input name=\"edit\" type=\"submit\" value=\"change\">	</td>
<td><input name=\"delete\" type=\"submit\" value=\"delete\"></td>

<td>$row[0]&nbsp;</td>
<td>$row[1]&nbsp;</td>
<td>$row[2]&nbsp;</td>
<td>$row[3]&nbsp;</td>
<td><img src=$row[4]>&nbsp;</td>
<td>$row[6]</td>
</form>	</tr>";
}
echo "</table>";
}
?>
<?php
echo "<a href=\""> return to the main page</a>";
?>
</body>
</html>

Dani AI

Generated

Brief note: 's pointer helped get basic paging working and confirmed the problem was solved. The following adds practical, modern guidance to make pagination in that script robust, secure, and maintainable (useful if the page is being read years later).

A minimal, safe workflow:

  1. Count the total number of visible news rows (use the same WHERE clause you display, e.g. only active items).
  2. Decide perPage (8) and compute pages = ceil(total / perPage). Clamp the requested page to [1, pages].
  3. Query only the current page of rows (compute offset = (page-1)*perPage) with a prepared statement and integer-bound parameters to avoid injection and errors. Example using PDO:
$perPage = 8;
$page = max(1, (int) ($_GET['page'] ?? 1));

$count = $pdo->prepare('SELECT COUNT(*) FROM haber WHERE aktif = ?');
$count->execute(['aktif']);
$total = (int) $count->fetchColumn();
$totalPages = max(1, (int) ceil($total / $perPage));

$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare('SELECT no,title,subject,explanation,littleimage FROM haber WHERE aktif = ? ORDER BY no DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute(['aktif']);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Security, UX and performance notes:

  • Always cast and clamp the page input to an integer.
  • Escape displayed text with htmlspecialchars(..., ENT_QUOTES, 'UTF-8') to avoid XSS.
  • For very large tables consider keyset pagination (seek method) instead of offset-based paging for better performance.
  • Add simple UI: first/prev/next/last links and show the current page number; avoid exposing negative or out-of-range pages.

File-upload and legacy-code cautions:

  • The script uses older extensions and unvalidated uploads. Migrate away from deprecated mysql_* to PDO or MySQLi and validate $_FILES (check MIME/type with finfo or getimagesize, limit size, generate unique filenames, store outside web root when possible). See the PHP docs on prepared statements and uploads for details: PDO::prepare and Handling file uploads. For upload threats, OWASP has a concise writeup: Unrestricted File Upload.

These steps keep the pagination accurate, prevent common injection/XSS issues, and make the news admin/upload flow safer and easier to maintain.

Recommended Answers

All 4 Replies

You should look into mysql's LIMIT for pagination. It is really easy to implement.

thanks
i know
but i dont know how to apply

Here is a pretty good tutorial on the subject:

thanks have already solved this problem
thanks for attention

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.