Solved

Need help with Popup and pagination

Posted on 2006-06-28
4
286 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now