Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help with Popup and pagination

Posted on 2006-06-28
4
Medium Priority
?
309 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 2000 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

578 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