PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2009, 06:27 AM
liamdawe's Avatar
liamdawe liamdawe is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2006
Location: England
Posts: 514 liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 1 m 24 sec
Reputation Power: 16
Send a message via MSN to liamdawe Send a message via Google Talk to liamdawe
PHP-General - Need help with the maths on this

Basically each page i have is split up into 7 sections 1 for each post.

Each post has an anchor link on it with "#1, #2" etc. So people can link right to that post.

Which is fine until i get onto a second page. I am not sure how to work out which post to go to in the header redirect.

For example if there are 8 posts there will be two pages right? Page two will only have one post.

How will i go about working that out for each page based on how many posts?

Reply With Quote
  #2  
Old July 2nd, 2009, 06:40 AM
Mark Baker Mark Baker is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Nov 2001
Location: North West UK
Posts: 1,137 Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 18 h 43 m 39 sec
Reputation Power: 391
PHP Code:
 $post 8;
$postPage floor($post 7) + 1;
$postNumber = ($post 7) + 1;
echo 
$post.' is posting number '.$postNumber.' on page '.$postPage


Do a search on pagination
__________________
9 out of 10 PHP problems can be resolved by setting
PHP Code:
 error_reporting(E_ALL);
ini_set('display_errors'1); 
php -l <filename> will identify 9 out of the remaining 10 problems
Remember, the command line is your friend

Development Projects:
PHPExcel
PHPPowerPoint

Reply With Quote
  #3  
Old July 2nd, 2009, 06:48 AM
liamdawe's Avatar
liamdawe liamdawe is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2006
Location: England
Posts: 514 liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 1 m 24 sec
Reputation Power: 16
Send a message via MSN to liamdawe Send a message via Google Talk to liamdawe
I honestly don't get what you just posted at all it doesn't seem to be right?

I take it $pageNumber is 7 percent of $post or what?

Reply With Quote
  #4  
Old July 2nd, 2009, 07:05 AM
Mark Baker Mark Baker is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Nov 2001
Location: North West UK
Posts: 1,137 Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 18 h 43 m 39 sec
Reputation Power: 391
Quote:
Originally Posted by liamdawe
I take it $pageNumber is 7 percent of $post or what?
The % in my code snippet has nothing to do with percentages: it's the modulus operator in PHP.

You obviously haven't actually tried to see what the code snippet does, or tried reading about it in the PHP manual, or tried googling or searching the forums for pagination.

Try doing those things before telling me I'm not right; and then, if I really am not right, try explaining what you want in more detail. I did make one mistake, but the general principle is still correct

PHP Code:
 $post 8
$postPage floor(($post -1) / 7) + 1
$postNumber = (($post -1) % 7) + 1
echo 
$post.' is posting number '.$postNumber.' on page '.$postPage
Comments on this post
srisa agrees: Quite a polite reply.
requinix agrees: needs more "politeness"

Last edited by Mark Baker : July 2nd, 2009 at 08:00 AM.

Reply With Quote
  #5  
Old July 2nd, 2009, 07:33 AM
liamdawe's Avatar
liamdawe liamdawe is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2006
Location: England
Posts: 514 liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 1 m 24 sec
Reputation Power: 16
Send a message via MSN to liamdawe Send a message via Google Talk to liamdawe
I tried the code and it simply doesn't work, $postNumber doesn't give me the right value.

Also i don't see how exactly i was supposed to know a percentage symbol didn't mean that, the whole point of me posting here is to get help.

For this code does $post mean the actual post number, or the amount of posts?

Reply With Quote
  #6  
Old July 2nd, 2009, 07:40 AM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,178 srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 15 h 46 m 15 sec
Reputation Power: 229
$post = total number of posts.
$postPage = page number
$postNumber = value that goes into the anchor tag #1, #2 etc.
__________________
Why do we always seek someone, something or some thought, are we afraid of ourselves?
"The love of one's country is a splendid thing. But why should love stop at the border?" - Pablo Casals

Reply With Quote
  #7  
Old July 2nd, 2009, 07:46 AM
liamdawe's Avatar
liamdawe liamdawe is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2006
Location: England
Posts: 514 liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 1 m 24 sec
Reputation Power: 16
Send a message via MSN to liamdawe Send a message via Google Talk to liamdawe
Thought it might be that, altho the postNumber gives no value.

Reply With Quote
  #8  
Old July 2nd, 2009, 07:59 AM
Mark Baker Mark Baker is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Nov 2001
Location: North West UK
Posts: 1,137 Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level)Mark Baker User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 18 h 43 m 39 sec
Reputation Power: 391
PHP Code:
for ($post 1$post 25$post++)   {
    
$postPage floor(($post -1) / 7) + 1;
    
$postNumber = (($post -1) % 7) + 1;
    echo 
$post.' is posting number #'.$postNumber.' on page '.$postPage.'<br />';


gives
Code:
1 is posting number #1 on page 1
2 is posting number #2 on page 1
3 is posting number #3 on page 1
4 is posting number #4 on page 1
5 is posting number #5 on page 1
6 is posting number #6 on page 1
7 is posting number #7 on page 1
8 is posting number #1 on page 2
9 is posting number #2 on page 2
10 is posting number #3 on page 2
11 is posting number #4 on page 2
12 is posting number #5 on page 2
13 is posting number #6 on page 2
14 is posting number #7 on page 2
15 is posting number #1 on page 3
16 is posting number #2 on page 3
17 is posting number #3 on page 3
18 is posting number #4 on page 3
19 is posting number #5 on page 3
20 is posting number #6 on page 3
21 is posting number #7 on page 3
22 is posting number #1 on page 4
23 is posting number #2 on page 4
24 is posting number #3 on page 4

Reply With Quote
  #9  
Old July 2nd, 2009, 08:06 AM
liamdawe's Avatar
liamdawe liamdawe is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2006
Location: England
Posts: 514 liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level)liamdawe User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 1 m 24 sec
Reputation Power: 16
Send a message via MSN to liamdawe Send a message via Google Talk to liamdawe
That is strange i currently have this:
PHP Code:
// if we have less than 7 replies its simple
        
if ($post_count <= 7)
        {
            
// it will be the first page
            
$postPage 1;
            
            
// if we have no replies yet then the postcount is 2 (topic + this post)
            
if ($post_count == 0)
            {
                
$postNumber 2;
            }

            
// unless we already have a reply then it is 2 + this one (topic and first post (which is 2) + this one)
            
else
            {
                
$postNumber $post_count 2;
            }
        }

        
// now if the reply count is bigger than 7 then we have more than one page, a little more tricky
        
if ($post_count 7)
        {
            
// find out the page we are on first
            
$rows_per_page 7;

            
$actual_posts_count $post_count 2;

            
$postPage floor($actual_posts_count $rows_per_page) + 1
            
$postNumber = (($actual_posts_count -1) % $rows_per_page) + 1;  
        }



        
header("Location: viewtopic.php?tid={$topic}&pageno={$postPage}#post{$postNumber}"); 

Where $post_count is set via the replies field in the database table.

It seems to go up until 9 then go to a second page, and then go to #3 this is strange.

Last edited by liamdawe : July 2nd, 2009 at 08:13 AM.

Reply With Quote
  #10  
Old July 2nd, 2009, 12:47 PM
srisa srisa is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2006
Location: I'm sneaking up behind you.
Posts: 1,178 srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level)srisa User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 15 h 46 m 15 sec
Reputation Power: 229
Try this.
php Code:
Original - php Code
  1.  
  2.         $rows_per_page = 7;
  3.         $actual_posts_count = $post_count + 2;
  4.         $postPage = floor(($actual_posts_count-1) / $rows_per_page) + 1;
  5.         $postNumber = (($actual_posts_count -1) % $rows_per_page) + 1
  6.  
  7.         header("Location: viewtopic.php?tid={$topic}&pageno={$postPage}#post{$postNumber}");   

Reply With Quote
  #11  
Old July 2nd, 2009, 04:05 PM
alleyOOPs alleyOOPs is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 341 alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level)alleyOOPs User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Days 20 h 47 m 47 sec
Reputation Power: 155
in just the pure number of hours you will be spending on little items like this over the next few months I highly suggest buying a book on php/mysql.

I personally think a great starter is the book by ullman and it handles things like pagination (which is what you are trying to do).

It may seem rude, but meant in jest.....RTFM or buy a good starter book. It will save you hundreds of wasted hours.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Need help with the maths on this


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
Stay green...Green IT