Jump to content

[SOLVED] Please help to display rows number output from DB


Recommended Posts

I have 6 records (6 rows in DB). I want to print out 3 recods per page, it works fine. I want to count and print the number that match with the record number, for example

on the first page it displays

1.-------(record1)

2.-------(record2)

3.-------(record3)

but when I click next it goes to the second page

1.-------(record4)

2.-------(record5)

3.-------(record6)

I want it to display

4.-------(record4)

5.-------(record5)

6.-------(record6)

 

Please help ! below is my code:

 

$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

 

// select database

mysql_select_db($db) or die ("Unable to select database!");

mysql_query("SET NAMES utf8");

 

if (!(isset($pagenum)))

{

$pagenum = 1;

}

 

// create query

$query =  "SELECT * FROM techNeed where state='Alabama' order by city";

 

// execute query

$data = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 

$rows=mysql_numrows($data);

 

$page_rows = 3;

 

//This tells us the page number of our last page

$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages

if ($pagenum < 1)

{

$pagenum = 1;

}

elseif ($pagenum > $last)

{

$pagenum = $last;

}

 

//This sets range that we will display in our query

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

 

$result = mysql_query("SELECT * FROM techNeed where state='Alabama' order by city $max") or die(mysql_error());

 

$count=1;

while($info = mysql_fetch_array($result))

{

$salonName=mysql_result($result,$i,"salonName");

$city=mysql_result($result,$i, "city");

$state=mysql_result($result,$i,"state");

$phone=mysql_result($result,$i,"phone");

 

echo " <table border='1' cellpadding='1' bgcolor='#D5EAEA' width='900'>

<tr><td>$count</td><td>$salonName</td><td >$city  </td></tr>

 

<tr><td> </td><td>$state</td><td>$phone</td></tr>

</table>"; 

 

$i++;

$count ++;

echo "<br>";

}

 

if ($pagenum == 1)

{

}

else

{

 

$previous = $pagenum-1;

echo "<b><a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> << Trở Lại</a> ";

}

$next = $pagenum+1;

for($k = 1; $k <= $last; $k++){

    if(($pagenum) == $k){

        echo "<b>$k ";

        } else {

        echo "<b><a href='{$_SERVER['PHP_SELF']}?pagenum=$k'>$k</a> ";

    }

}

if ($next == $last+1)

{    echo "<b> Hết ";

}

else {

 

echo "<b><a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Trang Kế >></a> ";

 

}

 

?>

Please edit your post and put

...

tags around it.

 

Give this a try:

 

<?php
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");
mysql_query("SET NAMES utf8");

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

// create query
$query =  "SELECT * FROM techNeed where state='Alabama' order by city";

// execute query
$data = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$rows=mysql_numrows($data);

$page_rows = 3;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets range that we will display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query("SELECT * FROM techNeed where state='Alabama' order by city $max") or die(mysql_error());

// *** This is what I've added ***
$index = ($pagenum * $page_rows) - ($page_rows - 1);

while($info = mysql_fetch_array($result))
{
$salonName=mysql_result($result,$i,"salonName");
$city=mysql_result($result,$i, "city");
$state=mysql_result($result,$i,"state");
$phone=mysql_result($result,$i,"phone");

echo " <table border='1' cellpadding='1' bgcolor='#D5EAEA' width='900'>
<tr><td>$index</td><td>$salonName</td><td >$city  </td></tr>

<tr><td> </td><td>$state</td><td>$phone</td></tr>
</table>"; 

$i++;
$index++;
echo "
";
}

if ($pagenum == 1)
{
}
else
{

$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> << Trở Lại[/url] ";
}
$next = $pagenum+1;
for($k = 1; $k <= $last; $k++){
    if(($pagenum) == $k){
        echo "$k ";
        } else {
         echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$k'>$k[/url] ";
    }
}
if ($next == $last+1)
{    echo " Hết ";
}
else {

echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Trang Kế >>[/url] ";

}

?>

 

I just added this small calculation...

 

$index = ($pagenum * $page_rows) - ($page_rows - 1);

 

That should work.

 

Regards

Huggie

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.