By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

New member stuck in write php script

P: 2
Hi friend, I am new in this area.
Hope members can help me on my script below.
I want to create Previous and Next button but it runs until maximum execution time. Help me
[php]
<?php #script view_users.php pg 279

//This script retrieves all the records from the register table.

//This new version allows the results to be sorted in different ways.

//Page header.
echo '<h1 id="mainhead">Registered Projects</h1>';

//cONNECT TO THE DATABASE.
require_once 'Connect/mysql_connect.php';

//Number of records to show per page
$display = 5;


//Determine how many pages there are.
if (isset($_GET['np'])) { //Already been determined.

$num_pages = $_GET['np'];

} else { //Need to determine.

//Count the number of record.
$query = "SELECT serial,date, department, user_id, email, project, description FROM register";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row['0'];

//Calculate the number of pages.
if ($num_records > $display) { //More than 1 page.

$num_pages = ceil ($num_records/$display);

} else {

$num_pages = 1;

}

} //End of np IF.

//Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];

} else {

$start = 0;
}


//Default column links

$link1 = "{$_SERVER['PHP_SELF']}?sort=dt";
$link2 = "{$_SERVER['PHP_SELF']}?sort=dpmt";
$link3 = "{$_SERVER['PHP_SELF']}?sort=usr";
$link4 = "{$_SERVER['PHP_SELF']}?sort=em";
$link5 = "{$_SERVER['PHP_SELF']}?sort=pj";


//Determine the sorting order.
if (isset($_GET['sort'])) {


//Use existing sorting order.

switch ($_GET['sort']) {

case 'dt':
$order_by = 'date ASC';
link1 == "{$_SERVER['PHP_SELF']}?sort=adt";
break;
case 'adt':
$order_by = 'date DESC';
link1 == "{$_SERVER['PHP_SELF']}?sort=dt";
break;

case 'dpmt':
$order_by = 'department ASC';
link2 == "{$_SERVER['PHP_SELF']}?sort=adpmt";
break;
case 'adpmt':
$order_by = 'department DESC';
link2 == "{$_SERVER['PHP_SELF']}?sort=dpmt";
break;

case 'usr':
$order_by = 'user_id ASC';
link3 == "{$_SERVER['PHP_SELF']}?sort=ausr";
break;
case 'ausr':
$order_by = 'user_id DESC';
link3 == "{$_SERVER['PHP_SELF']}?sort=usr";
break;

case 'em':
$order_by = 'email ASC';
link4 == "{$_SERVER['PHP_SELF']}?sort=aem";
break;
case 'aem':
$order_by = 'email DESC';
link4 == "{$_SERVER['PHP_SELF']}?sort=em";
break;

case 'pj':
$order_by = 'project ASC';
link2 == "{$_SERVER['PHP_SELF']}?sort=apj";
break;
case 'apj':
$order_by = 'project DESC';
link2 == "{$_SERVER['PHP_SELF']}?sort=pj";
break;

default:
$order_by = 'email
DESC';
break;

}


//$sort will be appended to the pagination links.

$sort = $_GET['sort'];

} else { //use the default sorting order.

$order_by = 'email ASC';
$sort = 'aem';

}


//Make the query.

$query = "SELECT date, department, user_id, email, project, description FROM register ORDER BY $order_by LIMIT $start, $display";
$result = @mysql_query ($query);


//Table header

echo '<table align="center" cellspacing="0"
cellpadding="5">

<tr>

<td align="left"><b><a href="' .
$link1 . '">Date</a></b></td>

<td align="left"><b><a href="' .
$link2 . '">Department</a></b></td>

<td align="left"><b><a href="' .
$link3 . '">User</a></b></td>

<td align="left"><b><a href="' .
$link4 . '">Email</a></b></td>

<td align="left"><b><a href="' .
$link5 . '">Project</a></b></td>

<td align="left"><b>Edit</b></td>
<td align="left"><b>Delete</b>
</td>
</tr>
';


//Fetch and print all the records.
$bg = '#eeeeee'; //set the background color.

while ($row = mysql_fetch_array($result,
MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' :
'#eeeeee'); //switch the background color.

echo '<tr bgcolor="' . $bg . '">


<td align="left">' . $row['date'].'</td>
<td align="left">' . $row['department'].'</td>
<td align="left">' . $row['user_id'].'</td>
<td align="left">' . $row['email'].'</td>
<td align="left">' . $row['project'].'</td>

<td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
<td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>
</tr>
';
}

echo '</table>';

mysql_free_result ($result); //Free up the resources.


mysql_close(); //Close the database connection.


//Make the link to other pages, if necessary

if ($num_pages > 1) {

echo '<br /><p>';

//Determine what page the script is on.
$current_page = ($start/$display) + 1;

//If it's not the first page, make a Previous button.

if ($current_page != 1) {
echo '<a href="sortable_display.php?s='.
($start - $display) . '&np=' .
$num_pages . '&sort=' . $sort .
'">Previous</a>';

}

//Make all the numbered pages.

for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="sortable_display.php?s='
. (($display * ($i - 1))) .
'&np=' . $num_pages . '&sort=' ,
$sort . '">' . $i .'</a> ';

} else {

echo $i . '';
}

}

//If it's not the last page, make a Next button.

if ($current_page != $num_pages) {
echo '<a href="sortable_display.php?s='
. ($start + $display) . '&np=' .
$num_pages . '&sort=' . $sort .
'">Next</a>';

}

echo '</p>';


} //End of links section

?>[/php]
Dec 13 '06 #1
Share this Question
Share on Google+
2 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
Welcome to thescripts Azmiza.

Please check out our PHP forum.

Mary
Dec 15 '06 #2

ronverdonk
Expert 2.5K+
P: 4,258
What output does your script produce? Anything?

Next time, enclose your code within code, php or html tags. That makes it a lot easier to read for our members and they will be more inclined to help you.
Read the Posting Guidelines at the top of this forum before you post.

Ronald :cool:
Dec 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.