Solved

Need help with Popup and pagination

Posted on 2006-06-28
4
295 Views
Last Modified: 2008-03-17
Hi everyone,

I am having a go at redesigning my website and implementing PHP & MySQL.  I have set up the database and added a few records.  I have queried the DB and can get the results to display fine.  When someone clicks on a link on the navigation bar it loads a PHP page, e.g. wdtutorials.php which includes the header, footer, navigation links and the code below:

<?php
$hostname="localhost";
$dbname="pagelinks";
$user="********";
$pass="********";

$db = mysql_connect($hostname, $user, $pass);
mysql_select_db($dbname, $db);

$query="SELECT * FROM links WHERE category_code = 20 ORDER BY link_text ASC";
$result=mysql_query($query);

mysql_close();

$i=0;
while ($i < 5) {

$link_text=mysql_result($result,$i,"link_text");
$description=mysql_result($result,$i,"description");
$url=mysql_result($result,$i,"url");
$newdes = stripslashes($description);

echo "$link_text<br>$newdes<br><a href=\"$url\">$url</a><br><br>";

$i++;
}
?>


How would I add page numbers at the bottom of the page automatically?  I don't know if this is even possible.  How would I say that after 5 links are displayed on a page I want to move to page 2.  Is it possible to reload the original page with the next lot of results?  Or do I need to create another page e.g. wdtutorials2.php which would be a pain.


The table for the database is below:

CREATE TABLE `links` (
  `link_id` int(11) NOT NULL auto_increment,
  `category_code` varchar(10) NOT NULL default '0',
  `link_text` varchar(254) NOT NULL default '',
  `url` varchar(254) NOT NULL default '',
  `description` text NOT NULL,
  `time_added` timestamp(14) NOT NULL,
  `new_window` tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (`link_id`)
) TYPE=MyISAM AUTO_INCREMENT=226 ;


The new_window row is for whether the link will open in a new window or open in the current one.  0 = Current, 1 = New.  I have a JavaScript popup script I thought I might be able to use but not sure how to get the code to use it.


Here is the popup script (popup.js):

function popup(mylink, windowname)
      {
      if (! window.focus)return true;
            var href;
      if (typeof(mylink) == 'string')
            href=mylink;
      else
            href=mylink.href;
            window.open(href, windowname, 'width=750,height=550,toolbar=1,scrollbars=yes,status=1,resizable=yes');
      return false;
}

You can see the website here: http://www.iteachict.net/wdtutorials.php

Thanks

John
0
Comment
Question by:john-formby
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16999811
You could have:

<?php
$pageSize = 5;

$pageNumber = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1

Then you can change your query to:

$query="SELECT * FROM links WHERE category_code = 20 ORDER BY link_text ASC LIMIT ".(($page-1) * $pageSize).','.$pageSize;

Then for the navigation thing you can use:
$numberOfRecords = mysql_result(mysql_query('SELECT count(*) FROM links WHERE category_code=20'),0,0);
for($i = 0; $i < $numberOfRecords; $i += $pageSize) {
  $page = 1 + floor($i/$pageSize);
  echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'">'.$page.'</a>';
}

-r-
0
 
LVL 14

Author Comment

by:john-formby
ID: 16999869
Cool :-)  Thanks for that.  It is now working great.  Do know how I could get the popups working?

Thanks
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 16999900
I think you have to start of by moving the mysql_close(); call to be below your last mysql_** function call.

Then you could change the while loop to:
$result=mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

$link_text=$row['link_text'];
$description=$row['description'];
$url=$row['url'];
$popup = ($row['new_window'] == 1);
$newdes = stripslashes($description);

if($popup) {
  $link = '<a href="'.$url.'" onclick="window.open(this.href);return false">';
} else {
  $link = '<a href="'.$url.'" >';
}
echo "$link_text<br>$newdes<br/>".$link."$url</a><br><br>";
}

mysql_close();
?>

-r-
0
 
LVL 14

Author Comment

by:john-formby
ID: 16999981
Thanks a lot, it is now working exactly as I wanted :-)

John
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
website maintenance mode 1 24
PHP Query return divisible by 3 3 25
Is there an elegant way to view sql statements written in PDO? 2 22
php56-php-mcrypt for rhel7 php56 1 43
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question