Link to home
Start Free TrialLog in
Avatar of john-formby
john-formbyFlag for Ghana

asked on

Need help with Popup and pagination

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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

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-
Avatar of john-formby

ASKER

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

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot, it is now working exactly as I wanted :-)

John