i am trying to do a pagination but, i cant seem to get the problem..

<?
//pagination

    $objConnect = mysql_connect("localhost","root","") or die(mysql_error());
    $objDB = mysql_select_db("custech");
    $strSQL = "SELECT * FROM proposals ";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    $Num_Rows = mysql_num_rows($objQuery);

    $Per_Page = 4;   // Per Page

    $Page = $_GET["Page"];
    if(!$_GET["Page"])
    {
        $Page=1;
    }

    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;

    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($Num_Rows<=$Per_Page)
    {
        $Num_Pages =1;
    }
    else if(($Num_Rows % $Per_Page)==0)
    {
        $Num_Pages =($Num_Rows/$Per_Page) ;
    }
    else
    {
        $Num_Pages =($Num_Rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }

// Before using $_POST['value'] add this - to avoid error on undefined index

if (!isset($_POST['txtSearch']))
{
    //Instruction if $_POST['value']doesnt exist
    $_POST['txtSearch']="%";
}


//to select all record in database and to display all records

 if($levelbox=='')
 {
    $sql="SELECT * FROM proposals ORDER BY pro_id ASC LIMIT $Page_Start , $Per_Page";
 }
 else {
 $strSQL .=" SELECT * FROM proposals WHERE $levelbox LIKE '%".$_POST['txtSearch']."%' ORDER BY pro_id ASC LIMIT $Page_Start , $Per_Page";
    //$sql="SELECT * FROM proposals WHERE $levelbox LIKE '%".$_POST['txtSearch']."%' ORDER BY pro_id ASC";
 }

//$sql="SELECT * FROM robots WHERE $levelbox LIKE '%".$_POST['txtSearch']."%'";

    $objQuery  = mysql_query($strSQL);
//$result=mysql_query($sql);

//error message if query fails

if (!$objQuery)
{
die;
exit;
}

//if no records found

if (mysql_num_rows($objQuery)==0)
{
echo "<table cellspacing='0' cellpading='3'><tr><td>";
echo "No row found, no row to print, so, am exiting";
echo "</td></tr></table>";
exit;
}



    ?>
    <table width="600" border="1">
      <tr>
        <th width="91"> <div align="center">Proposal ID </div></th>
        <th width="98"> <div align="center">Proposal Name </div></th>
        <th width="198"> <div align="center">Company </div></th>
        <th width="97"> <div align="center">Proposl Date </div></th>
        <th width="59"> <div align="center">Proposal Status</div></th>
        <th width="60"> <div align="center">File</div></th>
        <th width="71"> <div align="center">Proposal By</div></th>
      </tr>
    <?
    while($objResult = mysql_fetch_array($objQuery))
    {
    ?>
      <tr>
        <td><div align="center"><?=$objResult["pro_id"];?></div></td>
        <td><?=$objResult["pro_name"];?></td>
        <td><?=$objResult["pro_company"];?></td>
        <td><div align="center"><?=$objResult["pro_date"];?></div></td>
        <td align="right"><?=$objResult["pro_status"];?></td>
        <td align="right"><a href="<?=$objResult["pro_file"];?>">DOWNLOAD</a></td>
        <td align="right"><?=$objResult["pro_by"];?></td>
      </tr>
    <?
    }
    ?>
    </table>

    <br>
    Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
    <?
    if($Prev_Page)
    {
        echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
    }

    for($i=1; $i<=$Num_Pages; $i++){
        if($i != $Page)
        {
            echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
        }
        else
        {
            echo "<b> $i </b>";
        }
    }
    if($Page!=$Num_Pages)
    {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
    }
    mysql_close($objConnect);
//end of pagination
?>

Recommended Answers

All 22 Replies

Member Avatar for LastMitch

@missy_mi

i am trying to do a pagination but, i cant seem to get the problem..

Deos it show any errors? If yes please post the line.

there just this :

Notice: Undefined index: Page in C:\xampp\htdocs\CDMS\proposals.php on line 302

is this line :

$Per_Page = 4;   // Per Page

    $Page = $_GET["Page"];
    if(!$Page)
    {
        $Page=1;
    }

    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;

try

if (isset($Page)){
       if (!$Page){
           $Page=1;
       }

       // And so on and so forth :)
    }

In these parts for back, next etc, shouldn't Page be $Page?

echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";

no changes guys..sAME RESULT..

Member Avatar for LastMitch

@missy_mi

Notice: Undefined index: Page in C:\xampp\htdocs\CDMS\proposals.php on line 302

Instead of this:

$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;

Try this:

$Page = $_GET["Page"];

if($Page == 0) $Page = 1;
$Prev_Page = $Page - 1;                     
$Next_Page = $Page + 1; 

AndreRet is correct about this query being $Page not $Next_Page:

echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";

It should be $Page not $Next_Page

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page){
echo "<a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}else{
echo "<b> $i </b>";
}
}
if(!$Page=$Num_Pages){

echo "<a href ='$_SERVER[SCRIPT_NAME]?Page=$Page'>Total Pages</a>";

}else if($Prev_Page){

echo "<a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'>Prev Page</a>";

}else if ($Next_Page{

echo "<a href='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next Page</a>";
}
commented: To Rectify what some retard did to LastMitch +0
Member Avatar for diafol

I think it's due to this:

 $Page = $_GET["Page"];

The only index I can see in the code is the above. It means that the get var hasn't been set yet.

$Page = (isset($_GET["Page"])) ? intval($_GET["Page"]) : 1;

That should safely set your page to the integer value of the get variable if it's set or default to 1.

@diafol & all :

i already change it and there are no more notice that..but, the 1 page shows all the the data i specifically code.. 4 -- $Per_Page = 4; // Per Page ..but it display all.. what do i do??

Member Avatar for diafol

he 1 page shows all the the data i specifically code.. 4 -- $Per_Page = 4; // Per Page ..but it display all.. what do i do??

Sorry, I don't understand

Member Avatar for LastMitch

@missy_mi

i already change it and there are no more notice that..but, the 1 page shows all the the data i specifically code.. 4 -- $Per_Page = 4; // Per Page ..but it display all.. what do i do??

OK, I'm getting confused too. The issue is that all of the 4 pages appear at once? You want it to display one page at a time?

Can you post your update code so we can see what is the issue?

okayh...the item displayed should have only 4 line of data..e:g

-harry, 12
-sully, 15
-daniel, 12
-maria, 15
-george, 17

other data should not appear in the first page because i applied :

$Per_Page = 4;

but, the resul i got was :

-harry, 12
-sully, 15
-daniel, 12
-maria, 15
-george, 17
-cullen, 11
-sarah, 14
-henry, 13
-michelle, 17

  • it means that all the data is display..instead of just 4 data per page..
    do get what i mean??
Member Avatar for LastMitch

@missy_mi

it means that all the data is display..instead of just 4 data per page..
do get what i mean??

Can you post your update code not the results. Right now, I don't know what is wrong with your code because you mention you made some adjustments.

commented: To Rectify what some retard did to LastMitch +0
<?php

//open connection

$connectdb=mysql_connect("localhost","root");
if(!$connectdb)
{
die('Unable to connect to server:'.mysql_error());
exit;
}

//select database

$selectdb=mysql_select_db("custech", $connectdb);
if (!$selectdb)
{
die('Unable to select database custech:'.mysql_error());
exit;
}

if (!isset($_POST['levelbox']))
{
    //Instruction if $_POST['value']doesnt exist
    $_POST['levelbox']="%";
}

$levelbox = ($_POST['levelbox']);

?>

<form method="POST" name="frmSearch" action="<?"proposals.php"?>">
<table width="900" border="0" cellspacing="0" cellpadding="0">

   <td width="70%"><p>
   <select name="levelbox" class="inputbox2">
    <option  value=""/>-- ALL --</option>
    <option  value="pro_name"/>Proposal Name</option>
    <option  value="pro_status" />Status</option>
    <option  value="pro_company" />Company Name</option>
    <option  value="pro_date" />Date</option>
    </select>
    <input name="txtSearch" type="text" id="fullname" size="30" />
        <input type="submit" name="cmdSearch" value="Search">
   </td>
  </tr>


</table> 
</form>


<?
//pagination
    $Per_Page = 4;   // Per Page


    $objConnect = mysql_connect("localhost","root","") or die(mysql_error());
    $objDB = mysql_select_db("custech");
    $strSQL = "SELECT * FROM proposals ORDER BY pro_id ASC ";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    $Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 4;   // Per Page

        $Page = (isset($_GET["Page"])) ? intval($_GET["Page"]) : 1;
    if(!$Page)
    {
        $Page=1;
    }

    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;

    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($Num_Rows<=$Per_Page)
    {
        $Num_Pages =1;
    }
    else if(($Num_Rows % $Per_Page)==0)
    {
        $Num_Pages =($Num_Rows/$Per_Page) ;
    }
    else
    {
        $Num_Pages =($Num_Rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }

// Before using $_POST['value'] add this - to avoid error on undefined index

if (!isset($_POST['txtSearch']))
{
    //Instruction if $_POST['value']doesnt exist
    $_POST['txtSearch']="%";
}


//to select all record in database and to display all records

 if($levelbox=='')
 {
 $strSQL;
    //$sql="SELECT * FROM proposals ORDER BY pro_id ASC LIMIT $Page_Start , $Per_Page";
 }
 else {
 $strSQL .="WHERE $levelbox LIKE '%".$_POST['txtSearch']."%' ORDER BY pro_id ASC ";
    //$sql="SELECT * FROM proposals WHERE $levelbox LIKE '%".$_POST['txtSearch']."%' ORDER BY pro_id ASC";
 }

//$sql="SELECT * FROM robots WHERE $levelbox LIKE '%".$_POST['txtSearch']."%'";

    $objQuery  = mysql_query($strSQL);
//$result=mysql_query($sql);

//error message if query fails

if (!$objQuery)
{
die;
exit;
}

//if no records found

if (mysql_num_rows($objQuery)==0)
{
echo "<table cellspacing='0' cellpading='3'><tr><td>";
echo "No row found, no row to print, so, am exiting";
echo "</td></tr></table>";
exit;
}



    ?>
    <table width="600" border="1">
      <tr>
        <th width="40"> <div align="center"><font color="blue" size="3">Proposal ID </font></div></th>
        <th width="98"> <div align="center"><font color="blue" size="3">Proposal Name </font></div></th>
        <th width="80"> <div align="center"><font color="blue" size="3">Company </font></div></th>
        <th width="97"> <div align="center"><font color="blue" size="3">Proposal Date </font></div></th>
        <th width="59"> <div align="center"><font color="blue" size="3">Proposal Status</font></div></th>
        <th width="60"> <div align="center"><font color="blue" size="3">File</font></div></th>
        <th width="71"> <div align="center"><font color="blue" size="3">Proposal by </font></div></th>
      </tr>
    <?
    while($objResult = mysql_fetch_array($objQuery))
    {
    ?>
      <tr>
        <td><div align="center"><?=$objResult["pro_id"];?></div></td>
        <td><div align="center"><?=$objResult["pro_name"];?></div></td>
        <td><div align="center"><?=$objResult["pro_company"];?></div></td>
        <td><div align="center"><?=$objResult["pro_date"];?></div></td>
        <td><div align="center"><?=$objResult["pro_status"];?></div></td>
        <td><div align="center"><a href="<?=$objResult["pro_file"];?>">DOWNLOAD</a></div></td>
        <td><div align="center"><?=$objResult["pro_by"];?></div></td>
      </tr>
    <?
    }
    ?>
    </table>

    <br>
    Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
    <?
    if($Prev_Page)
    {
        echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
    }

    for($i=1; $i<=$Num_Pages; $i++){
        if($i != $Page)
        {
            echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
        }
        else
        {
            echo "<b> $i </b>";
        }
    }
    if($Page!=$Num_Pages)
    {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
    }
    mysql_close($objConnect);
//end of pagination
?>

sorry its a bit messy..still in trial version

Member Avatar for LastMitch

@missy_mi

You have a duplicate of this (it should be one):

$Per_Page = 4; // Per Page

The issue is here:

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
commented: To Rectify what some retard did to LastMitch +0

i dont understand, sir..what seems to be the problem..??

Member Avatar for LastMitch

@missy_mi

i dont understand, sir..what seems to be the problem..??

You have a duplicate line 54 and on line 63:

$Per_Page = 4; // Per Page

The issue is here about echo out more than 4 lines:

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
commented: To Rectify what some retard did to LastMitch +0

even if i delete the duplication..the result is still de same..help..

Member Avatar for LastMitch

@missy_mi

Try this:

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}

I don't have db for your code so you have test it out and let me know whether it work or not.

commented: To Rectify what some retard did to LastMitch +0

There is no fault in the code, it's perfect as it seems. I don't understand why this problem has occured.

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.