PHP - Quick Question

Code:
				<?php
					// ============
					// Show entries
					// ============

					// how many entries to show per page
					$rowsPerPage = 6;

					// by default we show first page
					$pageNum = 1;

					// if $_GET['page'] defined, use the value as page number
					if(isset($_GET['page']))
					{
						$pageNum = $_GET['page'];
					}

					// counting the offset ( where to start fetching the entries )
					$offset = ($pageNum - 1) * $rowsPerPage;

					// prepare the query string
					$query = "SELECT id, title, post, date ".
         				"FROM jcb_mainMenu ".
					"WHERE sectionID = 'About Me'".
		 			"ORDER BY id DESC ".            // using ORDER BY to show the most current entry first
		 			"LIMIT $offset, $rowsPerPage";  // LIMIT is the core of paging

					// execute the query 
					$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());

					// if the page is empty show a message
					if(mysql_num_rows($result) == 0)
					{
					?>
        				<p>This Page Is Empty!</p>
    				<?php
					}
					else
					{
						// get all entries
						while($row = mysql_fetch_array($result))
					{
					// list() is a convenient way of assign a list of variables
					// from an array values 
					list($id, $title, $post, $date) = $row;
				?>     										
					<span class="mainMenu"><?=$post;?><br /><br /></span>					
					<hr/>
				<?php
					} // end while

					// below is the code needed to show page numbers

					// count how many rows we have in database
					$query   = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
					$result  = mysql_query($query) or die('Error, query failed. ' . mysql_error());
					$row     = mysql_fetch_array($result, MYSQL_ASSOC);
					$numrows = $row['numrows'];

					// how many pages we have when using paging?
					$maxPage  = ceil($numrows/$rowsPerPage);
					$nextLink = '';

					// show the link to more pages ONLY IF there are 
					// more than one page
					if($maxPage > 1)
					{
						// this page's path
						$self     = $_SERVER['PHP_SELF'];
	
						// we save each link in this array
						$nextLink = array();
	
						// create the link to browse from page 1 to page $maxPage
						for($page = 1; $page <= $maxPage; $page++)
						{
							$nextLink[] =  "<a href=\"$self?page=$page\">$page</a>";
						}
	
						// join all the link using implode() 
						$nextLink = "Page : " . implode(' &raquo; ', $nextLink);
					}

					// close the database connection since
					// we no longer need it
					?>
    					<p align="center"><?=$nextLink;?></p>
    				<?php
					}
				?>

The above makes it so if there are to many posts, then a new page is created, but its creating a second page no matter if there are no posts... Anyway to stop this?

*EDIT* Actualy it is creating numbers of pages in relation to how many posts are in the table, not how many posts are in the section e.g. blog, main menu etc...
 
Last edited:
// count how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


mysql_num_rows will do that, iirc.
 
Clarkey said:
// count how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


mysql_num_rows will do that, iirc.
Is there anyway to make it only count the number of things in a particular section?
 
robmiller said:
Code:
SELECT COUNT(*) FROM table WHERE [conditions]
Thanks :)

Code:
$query   = "SELECT COUNT(*) AS numrows FROM jcb_mainMenu WHERE sectionID = 'Main Menu'";

That leaves me with 2 more problems I need to get sorted

1) I want my guestbook to set a cookie to a user to stop them posting again, it does this with my js, however as soon as I add my style switcher js to the page it breaks...

2) I need to be able to set it so if a word is over 60 characters in lenght a space is added, e.g "AAAAAAAAAAAAAAAAAAAAAAAAAA" could become "AAAAAAAAA AAAAAAAAAA" but AAAAAAA AAAAAAA would remain as it is, with no extra space...
 
Last edited:
http://jcb33.co.uk/guestbook.php?page=1

When I enter text to long it breaks my page, is there anyway to make it so it spaces, but so that it does not count spaces as characters?

Code:
					//Adds Spacing To Stop Page Breaking	
					$title = chunk_split($title,40," ");
					$user = chunk_split($user,40," ");
					$message = chunk_split($message,40," ");

Putting the above works, but it counts spaces so you can end up with a random space in the middle of a word e.g the frog went to the zoo on mo nday
 
Last edited:
Back
Top Bottom