Click to See Complete Forum and Search --> : link problem [1][2]-----


zuzupus
09-16-2003, 06:22 AM
hi,

form below piece of code i try to get the next page when 3 rows shown in table ,once user enter 4th row he will get page [1][2][3]
...and so on same liek this forum
so i jsut counted the rows from table t_emp where user
[PHP]
$Qpages=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
$pages=ceil($Qpages[0]);
$offset=($HTTP_GET_VARS["page"]*3)-3;
$result = mysql_query(" SELECT
bah balh from
t_emp order by id DESC LIMIT $offset,3");
then here is some problem
<? for ($i=3; $i<=$pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a>&nbsp";?>
as now what happening once user submit the form it shows [1] [2] [3] and so on

if anybody figure it out be appreciated and anything not able to understand plz let me know

regards

DaiWelsh
09-16-2003, 08:11 AM
I cannot really match what you are saying to the code you are showing. Are you saying you want the rows from t_emp to be shown three to a page? So if there were say 7 rows then initially the user would see rows 1-3 on screen with links to page 2 and 3, page 2 would show rows 4-6 and page 3 would show row 7?

zuzupus
09-16-2003, 08:22 AM
<I cannot really match what you are saying to the code you <are showing. Are you saying you want the rows from t_emp <to be shown three to a page? So if there were say 7 rows <then initially the user would see rows 1-3 on screen with <links to page 2 and 3, page 2 would show rows 4-6 and <page 3 would show row 7?

exactly u guess right but once he submit 4th row then

[1][2]

then [2]

have 4ht row and [1] have 1-3 rows

hope this is clear

thnks

DaiWelsh
09-16-2003, 09:14 AM
ok, then let me go through and mark up what I think i wrong with your code

$Qpages=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
$pages=ceil($Qpages[0]);

- it seems to me that $pages is not really the number of pages but the number of records. To get number of pages you need to divide by 3 (and ceil again)

$offset=($HTTP_GET_VARS["page"]*3)-3;

- this may be ok, provided that you are performing checks to set $HTTP_GET_VARS["page"] to 1 if it is not defined or is out of range, which I cannot see

$result = mysql_query(" SELECT
bah balh from
t_emp order by id DESC LIMIT $offset,3");
for ($i=3; $i<=$pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";?>

- I am not sure why you are startingfrom 3 and counting up 1 at a time but this seems wrong. Either start at 3 and count up 3 at a time while comparing to number of records or start at 1 and count up by one comparing to number of pages. This mixture of the two does not make sense - e.g. if there are 7 records this will start from $1=3 and go through $i=4, $i=5 up to $i=7 outputting a link each time so you should get links [3][4][5][6][7] which is not what you want.

My (untested) alternative code would be something like

$recs_per_page = 3;
$Qrecs=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
$num_records = $Qrec[0];
$num_pages = ceil($num_records / $recs_per_page);
if($num_pages == 0) { $num_pages = 1; }

$page = isset($HTTP_GET_VARS["page"])?$HTTP_GET_VARS["page"]:0;
$page = preg_replace('/\D/','',$page);
if($page < 1) { $page = 1; }
if($page > $num_pages) { $page = $num_pages; }

$offset = ($page - 1) * $recs_per_page;
$result = mysql_query(" SELECT bah balh from
t_emp order by id DESC LIMIT $offset,$recs_per_page");

for ($i=1; $i<=$num_pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";

have used a variable for number of recs per page so that you can adjust it easily later if required. I have put in some error checking buit you will probably want to be more thorough in your production code.

HTH,

Dai

zuzupus
09-16-2003, 09:33 AM
thanks dai its awesome but one problem

when i login it always show [1]

i want this [1}when 4 rows r there so that it show[1][2]
so that when i click on [1] i can see 3 rows and on [2] 4th one

let say user created only 3 rows then no [1]
once 4th row then [1] and [2]

hope this is clear and one more thing in my page im getting
[1][2] and Top exactly on center

i want these [1][2] on right and Top on left

but im getting both on centre as i used

<form>
<body>
<?
for ($i=1; $i<=$num_pages; $i++) echo "<right><a href=test.phtml?page=$i> [$i] </a> ";
?>
</body>
<a href="javascript:scroll(0,0)">Top</a>
</form>


thanks alot

DaiWelsh
09-16-2003, 03:11 PM
To not show the [1] when there are 3 or less records just enclose the for statement in an if e.g

if($num_pages > 1)
{
for ($i=1; $i<=$num_pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";
}

I am not sure what you mean in the next part as I can't see the page, but if you want the 'top' link on the left and the numbered links on the right then you can either put them in a table like

<table width="100%">
<tr>
<td align="left">Top link here</td>
<td align="right">Number links here</td>
</tr>
</table>

or there is an equivalent way to do it with CSS but I forget the syntax.

Dai

pyro
09-16-2003, 03:22 PM
Originally posted by DaiWelsh
or there is an equivalent way to do it with CSS...
<div>
<span style="float: left;">Top link here</span>
<span style="float: right;">Number links here</span>
</div>

zuzupus
09-17-2003, 03:19 AM
if($num_pages > 1)
{
for ($i=1; $i<=$num_pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";
}


sorry by using above piece of code now [1]is gone but i cant see other records on next page
i want something like below

1. Page: 2. Page
------- ---------
aaa ddd
bbb
ccc
1 [2] [1] 2


and regarding 1 [2] i want these on left as
echo "<left><a href=test.phtml?page=$i> [$i] </a> ";
it always show in middle

hope u understand ym problem anyway its appreicable

regards

DaiWelsh
09-17-2003, 03:22 AM
I would need to see the code to guess what is wrong with it.

zuzupus
09-17-2003, 03:28 AM
anyway i used ur code only

$recs_per_page = 3;
$Qrecs=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
$num_records = $Qrec[0];
$num_pages = ceil($num_records / $recs_per_page);
if($num_pages == 0) { $num_pages = 1; }

$page = isset($HTTP_GET_VARS["page"])?$HTTP_GET_VARS["page"]:0;
$page = preg_replace('/\D/','',$page);
if($page < 1) { $page = 1; }
if($page > $num_pages) { $page = $num_pages; }

$offset = ($page - 1) * $recs_per_page;
if($num_pages > 1)
{
for ($i=1; $i<=$num_pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";
}



i wont get any [2] or [3]

thanks

DaiWelsh
09-17-2003, 06:09 AM
Are you sure there are more than three records in the table for the value you are checking. Try adding echo statements to debug it e.g.

$recs_per_page = 3;
$Qrecs=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
$num_records = $Qrec[0];
$num_pages = ceil($num_records / $recs_per_page);
if($num_pages == 0) { $num_pages = 1; }
echo("Records:$num_records Pages:$num_pages<br>");
$page = isset($HTTP_GET_VARS["page"])?$HTTP_GET_VARS["page"]:0;
$page = preg_replace('/D/','',$page);
if($page < 1) { $page = 1; }
if($page > $num_pages) { $page = $num_pages; }
$offset = ($page - 1) * $recs_per_page;
echo("Page:$page Offset:$offset<br>");
if($num_pages > 1)
{
for ($i=1; $i<=$num_pages; $i++) echo "<left><a href=test.phtml?page=$i> [$i] </a> ";
}

zuzupus
09-17-2003, 07:01 AM
< $Qrecs=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM t_emp WHERE user = $session->sys_pk"));
< $num_records = $Qrec[0];

here we done the mistake i mean its my mistake i have to look clearly instead of copying and paste
$Qrec[0] must be $Qrecs[0];

then it works fine but when im in second page it doesnt shows like [1] 2 or when in first page 1[2]
but in my case it always [1][2] once 4 records entered

look in http://server2.vitodesign.com/scripts/test.phtml

thanks alot

DaiWelsh
09-17-2003, 04:26 PM
yes, I missed that one too :)

ok I did not see your new requirement. To get the current page to look different (and also not be a link?) try this

if($num_pages > 1)
{
for ($i=1; $i<=$num_pages; $i++)
{
if($i == $page)
{
echo " $i ";
}
else
{
echo "<a href=test.phtml?page=$i> [$i] </a> ";
}
}
}