Programmers,
Do you think I used the http_build_query() and the ternary correctly or not ?
Any bad or wrong coding on this pagination script below ?
It gets it’s values from $_GET. Will add html form and $
NOTE:
I did not use mysqli_stmt_num_rows() or mysqli_num_rows() on this one as will use on another project/s.
Here, I used the Sql COUNT to count the matching rows found. I did that, to make use of the mysqli_stmt_bind_result().
Not into pdo yet.
[code]
//SQL COUNT.
//mysqli_stmt_bind_result();
//mysqli_stmt_get_result();
//http_build_query()
//Report Error.
ini_set(‘display_errors’,1);
ini_set(‘display_startup_errors’,1);
error_reporting(E_ALL);
//Valid $_GET Items.
$tables = array(‘admin’,’admin_settings’,’links’,’taggings’,’affiliates’,’affiliates_settings’,’partners’,’partners_settings’,’sponsors’,’sponsors_settings’,’advertisers’,’advertisers_settings’,’members’,’members_settings’,’searchers’,’searchers_settings’,’users’,’users_settings’);
$links_table_columns = array(‘id’,’date_and_time’,’domain’,’domain_email’,’word’,’phrase’);
//Extract $_GETs.
$tbl = ISSET($_GET[‘tbl’])?strtolower($_GET[‘tbl’]):die(‘Insert Table!’);
$input_1 = !EMPTY($_GET[‘input_1’])?strtolower($_GET[‘input_1’]):die(‘Make your input for us to search!’);
$input_2 = !EMPTY($_GET[‘input_2’])?strtolower($_GET[‘input_2’]):null;
$col_1 = !EMPTY($_GET[‘col_1’])?strtolower($_GET[‘col_1’]):die(‘Input MySql Column to search!’);
$col_2 = !EMPTY($_GET[‘col_2’])?strtolower($_GET[‘col_2’]):null;
$bool = !EMPTY($_GET[‘bool’])?strtolower($_GET[‘bool’]):null;
$page = !EMPTY($_GET[‘pg’])?intval($_GET[‘pg’]):1;
$limit = !EMPTY($_GET[‘lmt’])?intval($_GET[‘lmt’]):1;
$offset = ($page*$limit)-$limit;
if(ISSET($col_2))
{
if(!in_array($col_2,$links_table_columns))
{
die(‘Invalid Mysql Table!’);
}
}
if(!in_array($col_1,$links_table_columns))
{
die(‘Invalid Mysql Table!’);
}
//Query DB.
$conn = mysqli_connect(“localhost”,”root”,””,”gulf”); //mysqli_connect(“server”,”user”,”password”,”db”);
$stmt = mysqli_stmt_init($conn);
if($bool==’and’)
{
$input_1 = $_GET[‘input_1’];
$input_2 = $_GET[‘input_2′];
$sql_count = “SELECT COUNT(id) from $tbl WHERE $col_1 = ? AND $col_2 = ?”;
$sql = “SELECT id,domain,word,phrase from $tbl WHERE $col_1 = ? AND $col_2 = ? LIMIT $limit OFFSET $offset”;
}
elseif($bool==’or’)
{
$input_1 = $_GET[‘input_1’];
$input_2 = $_GET[‘input_2′];
$sql_count = “SELECT COUNT(id) from $tbl WHERE $col_1 = ? OR $col_2 = ?”;
$sql = “SELECT id,domain,word,phrase from $tbl WHERE $col_1 = ? AND $col_2 = ? LIMIT $limit OFFSET $offset”;
}
else//if($bool==’null’),if(!ISSET($bool)
{
$input_1 = $_GET[‘input_1’];
$sql_count = “SELECT COUNT(id) from $tbl WHERE $col_1 = ?”;
$sql = “SELECT id,domain,word,phrase from $tbl WHERE $col_1 = ? LIMIT $limit OFFSET $offset”;
}
if(!mysqli_stmt_prepare($stmt,$sql_count))
{
echo ‘Mysqli Error: ‘ .mysqli_stmt_error($stmt);
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_stmt_errno($stmt);
}
else
{
if($bool==’and’ || $bool==’or’)
{
mysqli_stmt_bind_param($stmt,”ss”,$input_1,$input_2);
}
else
{
mysqli_stmt_bind_param($stmt,”s”,$input_1);
}
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_bind_result($stmt,$rows_count);
mysqli_stmt_fetch($stmt);
echo ‘Total Result: ‘ .$rows_count; echo ‘<br><br>’;
}
if(!mysqli_stmt_prepare($stmt,$sql))
{
echo ‘Mysqli Error: ‘ .mysqli_stmt_error($stmt);
echo ‘<br>’;
echo ‘Mysqli Error No: ‘ .mysqli_stmt_errno($stmt);
}
else
{
if($bool==’and’ || $bool==’or’)
{
mysqli_stmt_bind_param($stmt,”ss”,$input_1,$input_2);
}
else
{
mysqli_stmt_bind_param($stmt,”s”,$input_1);
}
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_array($result))
{
$id = $row[‘id’];
$domain = $row[‘domain’];
$word = $row[‘word’];
$phrase = $row[‘phrase’];
echo “$id<br>”;
echo “$domain<br>”;
echo “$word<br>”;
echo “$phrase<br>”;
echo “<br>”;
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
echo ‘Total Pages: ‘ .$total_pages = ceil($rows_count/$limit);
echo ‘<br><br>’;
$i = 0;
while($i<$total_pages)
{
$i++;
if($_GET[‘bool’]==’null’)
{
$array = array(“tbl”=>”$tbl”,”col_1″=>”$col_1″,”bool”=>”$bool”,”input_1″=>”$input_1″,”pg”=>”$i”);
$serps_url = $_SERVER[‘PHP_SELF’].’?’.http_build_query($array);
echo “<a href=”$serps_url”>$i</a>”;
}
else
{
$array = array(“tbl”=>”$tbl”,”col_1″=>”$col_1″,”col_2″=>”$col_2″,”bool”=>”$bool”,”input_1″=>”$input_1″,”input_2″=>”$input_2″,”pg”=>”$i”);
$serps_url = $_SERVER[‘PHP_SELF’].’?’.http_build_query($array);
echo “<a href=”$serps_url”>$i</a>”;
}
}
echo ‘<br>’;
A typical pagination url look like these:
I do not why the forum messes up my line alignments here. Looks fine on NotePad++.