...

View Full Version : Combining 2 scripts into one.



bucket
08-27-2009, 03:33 PM
Great thanks.

I am now trying to integrate it with my delete row script.

Can you help me out?

Basically combine these scripts into one, pagination delete rows script.

Here are both.

PHP Pagination:



<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example ;)
*/

require("inc/config.php");
/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed :)
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die("mysql_error()");

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}

mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links ;)

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?>






<?php
for($i = 0; $i < $num_per_page; $i++) {

$row = mysql_fetch_assoc($result)or die(mysql_error());

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}
mysql_close($link); //Close the link to the MySQL database.

?>




PHP Delete Rows Script:


<?php
error_reporting(E_ALL);

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id=\"mytable\">
<thead>

<tr>
<th align=\"center\" scope=\"col\">Delete</th>
<th align=\"center\" scope=\"col\">First Name</th>
<th align=\"center\" scope=\"col\">Last Name</th>
<th align=\"center\" scope=\"col\">Profile</th>
<th align=\"center\" scope=\"col\">Date of Entry</th>
<th align=\"center\" scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>";


echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr align=\"center\">";
echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<td>" . $row['AddedDate'] . "</td>";
echo "<td>" . $row['Ip'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";

}

else{
// No rows were found ...

echo "No members found";
}

mysql_close($con);
?>

Phil Jackson
08-27-2009, 04:11 PM
what is $_POST[delchk]? what is it used for?

bucket
08-27-2009, 04:22 PM
Its to delete a row from the html table that is shown.

Basicly the html table is to list all the rows of the table named 'persons' and so you can easily delete a member.

I would like to combine the two scripts so I can have like 10 people each table then go to the next page.

bucket
08-27-2009, 04:40 PM
Any luck phil? Talk on msn.

Phil Jackson
08-27-2009, 06:47 PM
one other thing.

why is this piece of code repeated twice:



for($i = 0; $i < $num_per_page; $i++)
{
$row = mysql_fetch_assoc($result)or die(mysql_error());
echo $row['FirstName'];
echo "<br/>";
echo $row['LastName'];
echo "<br/>";
echo $row['Ip'];
echo "<br/>";
echo $row['AddedDate'];
echo "<br/>";
echo $row['id'];
echo "<br/>";
}

Phil Jackson
08-27-2009, 07:03 PM
give this a whirl:



<?php
error_reporting(E_ALL);
require("inc/config.php");


// BELOW IS ONLY IN PLAY WHEN DELETE IS SELECTED
if (isset($_POST['del']))
{
for($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}

// END
// BELOW CHECKS FOR VALID VARIABLES
if(empty($_GET['page']))
{
die('Page number is not defined!');
}
elseif(!preg_match("/^([0-9])+$/",$_GET['page']))
{
die('Page is not a valid value!');
}
//END

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page))
{
die("Num is not a valid value!");
}

$page = $_GET['page'];
$qPage = (($page-1)*$num_per_page);
$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);
if($num == 0)
{
die('No more results!');
}

$result = mysql_query("SELECT * FROM persons");
if(mysql_num_rows($result) > 0)
{
echo "<table id=\"mytable\">
<thead>
<tr>
<th align=\"center\" scope=\"col\">Delete</th>
<th align=\"center\" scope=\"col\">First Name</th>
<th align=\"center\" scope=\"col\">Last Name</th>
<th align=\"center\" scope=\"col\">Profile</th>
<th align=\"center\" scope=\"col\">Date of Entry</th>
<th align=\"center\" scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>
<form name = 'myform' action='index.php' method='post'>";//<<<<<<<<<<<<<<<<<<<< here needs changing to the name on the page
for($i = 0; $i < $num_per_page; $i++)
{
echo "<tr align=\"center\">";
echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<td>" . $row['AddedDate'] . "</td>";
echo "<td>" . $row['Ip'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";
}
else
{
echo "No members found";
}

// GUESSING THAT BELOW IS ANCHORS FOR NEXT SET OF 10 MEMBERS
if($num > $num_per_page)
{
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>';
if($page > 1)
{
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}
//END
mysql_close($con);
mysql_close($link);

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum