Php Folks,
What is wrong with this line ?
[code]
$total_pages = ceil($result_row_count/$result_per_page);
I get error:
**Notice: Object of class mysqli_result could not be converted to int in C:xampphtdocspower.pagepagination_test_SIMPLE.php on line …
You see, I building a pagination page.
Look how I am doing it ….
[code]
<?php
error_reporting(E_ALL);
?>
<!DOCTYPE HTML”>
<html>
<head>
<meta name=”viewport” content=”width-device=width, initial-scale=1″>
</head>
<body>
<?php
if(!session_id())
{
session_start();
$_SESSION[‘form_step’] = ‘start’;
}
if($_SESSION[‘form_step’] != ‘end’);
{
if(!isset($_GET[‘form_type’]) && empty($_GET[‘form_type’]))
{
die(“Invalid Form!”);
}
else
{
$_SESSION[‘form_type’] = $_GET[‘form_type’];
if(!isset($_GET[‘query_type’]) && empty($_GET[‘query_type’]))
{
die(“Invalid Query!”);
}
else
{
$_SESSION[‘query_type’] = $_GET[‘query_type’];
if(!function_exists($_SESSION[‘form_type’]))
{
die(“Invalid Form!”);
}
else
{
$_SESSION[‘form_type’]();
}
}
}
function search()
{
?>
<form action=”<?php echo $_SERVER[‘PHP_SELF’];?>?form_type=<?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=1″ method=’post’ enctype=’plain/text’>
<?php
//Added ‘*’ (asterisk) to indicate the ‘Text Field’ is a ‘required’ one.
echo “<label for=”first_name”>First Name *:</label>
<input type=”text” name=”first_name” placeholder=”First Name” value = “”>”;?>
<br>
<?php
echo “<label for=”marital_status”>Marital Status *:</label>”;
echo “<select name=”marital_status”>”;
echo “<option value=”single”>Single</option>”;
echo “<option value=”married”>Married</option>”;
echo “</select>”;
echo “<br>”;
?>
<input type=”submit” name=”search” value=”Search”>
<?php
//$current_function = __FUNCTION__;
//echo $current_function;
if($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
{
if(isset($_POST[‘search’]))// && $_SESSION[‘form_step’] != ‘end’)
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.
if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}
$form_step = $_GET[‘form_step’];
$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$row_start = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$row_end = ($page_number * $result_per_page); //Max Result (Row Number that ‘Ends’ on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo “Row Start: $row_start”;
echo “Row End: $row_end”;
$sql_query = “SELECT COUNT(*) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die(“<pre>Statement Execution Failed!n”.mysqli_stmt_error($stmt).”nmysqli_stmt_bind_param</pre>”);
}
else
{
echo “<pre>1. Statement Execution Success!</pre>”; //DELETE THIS LINE
$result_row_count = mysqli_stmt_get_result($stmt);
if(mysqli_fetch_array($result_row_count, MYSQLI_NUM)[0])
{
$sql_query = “SELECT COUNT(*) FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“$place_holder_1”],$_POST[“$place_holder_2”]);
}
else
{
die(“No results!”);
}
}
//Grab total number of pages to paginate.
$total_pages = ceil($result_row_count/$result_per_page);
echo “TOTAL PAGES: $total_pages<br>”;
while($row = mysqli_fetch_array($result_row_count,MYSQLI_ASSOC)) //Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of ‘mysqli_stmt_bind_result($stmt,$email)’.
{
//Retrieve Values.
$first_name = $row[“first_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
echo “First Name: $first_name<br>”;
echo “Middle Name: $middle_name<br>”;
echo “Surname: $surname<br>”;
echo “Gender: $gender<br>”;
echo “Marital Status: $marital_status<br>”;
echo “Working Status: $working_status<br>”;
echo “Total Result Pages: $total_pages<br>”;
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo “<b> $i </b>”;?></a><?php
}
elseif($page_number>$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $previous_page;?>’><?php echo “<b> Previous </b>”;?></a><?php
//session_destroy();
}
$i++;
}
}
}
}
//ABOVE PAGINATION CODE GETS TRIGGERED WHEN CLICKING ‘SEARCH’ BUTTON.
//BELOW PAGINATION CODE GETS TRIGGERED WHEN CLICKING THE PAGE NUMBERS ON THE PAGINATION SECTION.
//BELOW CODE A COPY OF ABOVE PAGINATION CODE. BELOW CODE MUST RUN WHEN PAGE NUMBERS (PAGINATION LINKS) ON PAGINATION SECTION GET CLICKED.
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
$conn = mysqli_connect(“localhost”,”root”,””,”powerpage”);
$conn->set_charset(‘utf8mb4’); //Always set Charset.
if($conn === false)
{
die(“ERROR: Connection Error!. ” . mysqli_connect_error());
}
$form_step = $_GET[‘form_step’];
$page_number = $_GET[‘page’];
$result_per_page = $_GET[‘page_limit’];
$row_start = (($page_number * $result_per_page) – $result_per_page); //Offset (Row Number that ‘Starts’ on page).
$row_end = ($page_number * $result_per_page); //(Row Number that ‘Ends’ on page).
$previous_page = $page_number-1;
$next_page = $page_number+1;
echo “Row Start: $row_start”;
echo “Row End: $row_end”;
$sql_query = “SELECT * FROM users WHERE first_name = ? AND marital_status = ?”;
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,”ss”,$_POST[“first_name”],$_POST[“marital_status”]);
if($stmt == False)
{
//Close Connection.
mysqli_close($conn);
die(“<pre>2. Mysqli Prepare Failed!n”.mysqli_stmt_error($stmt).”n$sql_query</pre>”);
}
else
{
//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die(“<pre>Statement Execution Failed!n”.mysqli_stmt_error($stmt).”nmysqli_stmt_bind_param</pre>”);
}
else
{
echo “<pre>Statement Execution Success!</pre>”;
}
$result_row_count = mysqli_stmt_get_result($stmt);
$total_pages = ceil($result_row_count/$result_per_page);
echo “TOTAL PAGES: $total_pages<br>”;
while($row = mysqli_fetch_array($result_row_count,MYSQLI_ASSOC)) //Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of ‘mysqli_stmt_bind_result($stmt,$email)’.
{
//Retrieve Values.
$first_name = $row[“first_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$middle_name = $row[“middle_name”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$surname = $row[“surname”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$gender = $row[“gender”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$marital_status = $row[“marital_status”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
$working_status = $row[“working_status”];//Use this if you use ‘$result = mysqli_stmt_get_result($stmt)’ instead of //’mysqli_stmt_bind_result($stmt,$email_count)’;
echo “First Name: $first_name<br>”;
echo “Middle Name: $middle_name<br>”;
echo “Surname: $surname<br>”;
echo “Gender: $gender<br>”;
echo “Marital Status: $marital_status<br>”;
echo “Working Status: $working_status<br>”;
echo “Total Result Pages: $total_pages<br>”;
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo ” $i “;?></a><?php
}
elseif($i==$page_number)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $i;?>’><?php echo “<b> $i </b>”;?></a><?php
}
elseif($page_number>$total_pages)
{
echo “<a href=’http://localhost/power.page/search_3.php?form_type=”;?><?php echo $_SESSION[‘form_type’];?>&query_type=<?php echo $_SESSION[‘query_type’];?>&form_step=end&page_limit=1&page=<?php echo $previous_page;?>’><?php echo “<b> Previous </b>”;?></a><?php
}
$i++;
}
}
}
}
}
?>
My problem is in this line:
[code]
$total_pages = ceil($result_row_count/$result_per_page);