Solved

Need help with Popup and pagination

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

776 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