Folks,
I am not killing the script flow, before the CONDITION, with
die();
And so, one of the 2 should trigger:
IF
ELSE
But they don't.
if(!$result_2)
{
echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
die("Fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
echo __LINE__; echo "<br>";//LINE 98: THIS LINE IS LIKE AN 'ELSE'. IT DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
Either Line 92 (IF) should trigger or Line 98 (which is like the ELSE).
Do notice the comments in the code to understand where the script is ending the flow without any reason.
<?php
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
{
echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. LAST LINE THAT GETS ECHOED.
mysqli_stmt_bind_param($stmt_2,'s',$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
if(!$result_2)
{
echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'ELSE' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
die("Fetching Error");
}
What is killing the script flow ?
Context:
<?php
echo __LINE__; echo "<br>"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "<br>";
die("Type your keywords");
}
$keyword = $_GET['keywords'];
//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
$page = ISSET($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
$limit = ISSET($_GET['limit']) && is_numeric($_GET['limit']) ? $_GET['limit'] : 1;
$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
echo __LINE__; echo "<br>";//LINE 57: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. BUT WHY SCRIPT FLOW DOES NOT GO BEYOND THIS LINE ?
mysqli_stmt_bind_param($stmt_1,'s',$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
}
else
{
echo __LINE__; echo "<br>"; //LINE 67
printf("Error: %s.\n", mysqli_stmt_error($stmt_1));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_1));
die("A. Prepare failed!");
}
mysqli_stmt_close($stmt_1);
//$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
$offset = (($page * $limit) - $limit);
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
{
echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND ECHOED IN TEST. LAST LINE THAT GETS ECHOED.
mysqli_stmt_bind_param($stmt_2,'s',$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
if(!$result_2)
{
echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
die("Fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
echo __LINE__; echo "<br>";//LINE 98: THIS LINE IS LIKE AN 'ELSE'. IT DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
echo "LIMIT: $limit<br>";
echo "ROW COUNT: $row_count<br><br>";
echo "TOTAL PAGES: $total_pages<br><br>";
//Retrieve Values.
$id = $row["id"];
$page_url = $row["page_url"];
$link_anchor_text = $row["link_anchor_text"];
$page_description = $row["page_description"];
$keyphrases= $row["keyphrases"];
$keywords = $row["keywords"];
echo "Id: $id<br>";
echo "Page Url: $page_url<br>";
echo "Link Anchor Text: $link_anchor_text<br>";
echo "Page Description: $page_description<br>";
echo "Keyphrases: $keyphrases<br>";
echo "Keywords: $keywords<br>";
echo "<br>";
echo "<br>";
}
}
else
{
echo __LINE__; echo "<br>";//Line 124
printf("Error: %s.\n", mysqli_stmt_error($stmt_2));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_2));
die("B. Prepare failed!");
}
Also, out of these 2, which one is correct ?
$total_pages = ceil($result_1/$limit); //SHOULD I KEEP THIS LINE OR THE ONE BELOW THIS ONE ?
$total_pages = ceil($row_count/$limit); //SHOULD I KEEP THIS LINE OR THE ONE ABOVE THIS ONE ?
UPDATE
My code now looks like this.
My issues are mentioned clearly in the code comments in CAPITALS.
CURRENT ISSUES:
ISSUE 1:
Pagination Section only displays link to PAGE 1 from PAGE 1. Fails to display link to PAGE 2. Code configured to display 1 row per page. Since there are 2 matching rows then naturally 2 pages should display all the results. Why pagination section fails to display link to PAGE 2 from PAGE 1 ?
error_reporting.php
<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
ini_set('error_reporting',E_ALL);
?>
<?php
require 'conn.php';
require 'error_reporting.php';
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<form method='GET' action="<?php echo $_SERVER['PHP_SELF'];?>?keywords=$keywords&limit=$limit&page=1">
<label for="keywords">Keywords:*</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required>
<br>
<label for="limit">Results per Page</label>
<select name="limit" id="limit">
<option value="1">1</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<br>
<button name="search" id="search" value=" ">Search</button><br>
<button type="submit" name="search" id="search" value="search">Search</button>
<br>
<input type="reset">
<br>
</form>
<?php
echo __LINE__; echo "<br>"; //LINE 16: THIS LINE GETS TRIGGERED AND ECHOED IN TEST
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "<br>";
die("Type your keywords");
}
$keywords = $_GET['keywords'];
//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
$page = ISSET($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
$limit = ISSET($_GET['limit']) && is_numeric($_GET['limit']) ? $_GET['limit'] : 1;
$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
echo __LINE__; echo "<br>";//LINE 57: THIS LINE GETS TRIGGERED AND ECHOED IN TEST.
mysqli_stmt_bind_param($stmt_1,'s',$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
echo "ROW COUNT: $row_count<br><br>";
}
else
{
echo __LINE__; echo "<br>"; //LINE 67
printf("Error: %s.\n", mysqli_stmt_error($stmt_1));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_1));
die("A. Prepare failed!");
}
mysqli_stmt_close($stmt_1);
$total_pages = ceil($row_count/$limit);
$offset = (($page * $limit) - $limit);
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2)) //LINE 82:
{
echo __LINE__; echo "<br>";//LINE 84: THIS LINE GETS TRIGGERED AND SO PREPARED STATEMENT DID NOT FAIL AT LINE 82!
mysqli_stmt_bind_param($stmt_2,'s',$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
if(!$result_2)
{
echo __LINE__; echo "<br>"; //LINE 92: THIS LINE IN 'IF' DOES NOT TRIGGER. NEITHER THE LINE IN THE 'ELSE'. WHY ?
die("Fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))//LINE 96: THIS LINE IS LIKE AN 'ELSE'. IT DOES NOT TRIGGER. NEITHER THE LINE IN THE 'IF'. WHY ?
{
echo __LINE__; echo "<br>";
echo "LIMIT: $limit<br>";
echo "ROW COUNT: $row_count<br><br>";
echo "TOTAL PAGES: $total_pages<br><br>";
//Retrieve Values.
$id = $row["id"];
$page_url = $row["page_url"];
$link_anchor_text = $row["link_anchor_text"];
$page_description = $row["page_description"];
$keyphrases = $row["keyphrases"];
$keywords = $row["keywords"];
echo "Id: $id<br>";
echo "Page Url: $page_url<br>";
echo "Link Anchor Text: $link_anchor_text<br>";
echo "Page Description: $page_description<br>";
echo "Keyphrases: $keyphrases<br>";
echo "Keywords: $keywords<br>";
echo "<br>";
echo "<br>";
}
}
else
{
echo __LINE__; echo "<br>";//LINE 124
printf("Error: %s.\n", mysqli_stmt_error($stmt_2));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_2));
die("B. Prepare failed!");
}
mysqli_stmt_close($stmt_2);
//PAGINATION SECTION STARTS FROM HERE:
//WHY PAGINATION SECTION FAILS TO DISPLAY ? ONLY A LINK TO PAGE 1 IS SHOWN WHEN YOU LOAD PAGE 1. NO LINK TO PAGE 2. THERE ARE 2 MATCHING ROWS. SCRIPT FIXED TO SHOW 1 ROW PER PAGE. AND SO, 2 PAGES SHOULD DISPLAY ALL RESULTS. HENCE, PAGINATION SECTION SHOULD SHOW A LINK TO PAGE 2 FROM PAGE 1. BUT PAGE 1 ONLY LINKS TO ITSELF IN THE PAGINATION SECTION.
if($page>$total_pages) //If Page Number is greater than Total Pages, show only a link to FINAL PAGE.
{
echo "?>";?><?php echo "<b> Final Page </b>";?><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages)
{
echo "";?><?php echo " $i ";?><?php
}
elseif($i==$page) //Bold the Current Page Number.
{
echo "";?><?php echo "<b> $i </b>";?><?php
}
$i++;
}
}
?>
UPDATE AGAIN:
ISSUE: Missing in Url "&page=".
Look at this html:
<form method='GET' action="<?php echo $_SERVER['PHP_SELF'];?>?keywords=$keywords&limit=$limit&page=1">
As you can see, after clicking the form button, you should be sent to:
pagination_test_2.php?keywords=$keywords&limit=$limit&page=1"
But guess what ? you are sent to:
?keywords=$keywords&limit=$limit&search=search"
Where is the "&page=1" part in the url ?
Also, how not to have "search=search" added to the url ?
<?php
require 'conn.php';
require 'error_reporting.php';
?>
<!DOCTYPE HTML">
<html>
<head>
<meta name="viewport" content="width-device=width, initial-scale=1">
</head>
<body>
<form method='GET' action="<?php echo $_SERVER['PHP_SELF'];?>?keywords=$keywords&limit=$limit&page=1">
<label for="keywords">Keywords:*</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required>
<br>
<label for="limit">Results per Page</label>
<select name="limit" id="limit">
<option value="1">1</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<br>
<button name="search" id="search" value=" ">Search</button><br>
<button type="submit" name="search" id="search" value="search">Search</button>
<br>
<input type="reset">
<br>
</form>
<?php
echo __LINE__; echo "<br>";
if(!ISSET($_GET['keywords']))
{
echo __LINE__; echo "<br>";
die("Type your keywords");
}
$keywords = $_GET['keywords'];
//Check if the PAGE NUMBER is specified or not and if it's a numer or not. If not, return the default: 1.
$page = ISSET($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
//Check if the PAGE RESULT LIMIT is specified or not and if it's a numer or not. If not, return the default: 1.
$limit = ISSET($_GET['limit']) && is_numeric($_GET['limit']) ? $_GET['limit'] : 1;
$query_1 = "SELECT COUNT(id) FROM links WHERE keywords = ?";
$stmt_1 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_1,$query_1))
{
echo __LINE__; echo "<br>";
mysqli_stmt_bind_param($stmt_1,'s',$keywords);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
echo "ROW COUNT: $row_count<br><br>";
}
else
{
echo __LINE__; echo "<br>";
printf("Error: %s.\n", mysqli_stmt_error($stmt_1));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_1));
die("A. Prepare failed!");
}
mysqli_stmt_close($stmt_1);
$total_pages = ceil($row_count/$limit);
$offset = (($page * $limit) - $limit);
$query_2 = "SELECT id,page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ? ORDER by id LIMIT $offset,$limit";
$stmt_2 = mysqli_stmt_init($conn);
if(mysqli_stmt_prepare($stmt_2,$query_2))
{
echo __LINE__; echo "<br>";
mysqli_stmt_bind_param($stmt_2,'s',$keywords);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_get_result($stmt_2);
if(!$result_2)
{
echo __LINE__; echo "<br>";
die("Fetching Error");
}
while($row = mysqli_fetch_array($result_2,MYSQLI_ASSOC))
{
echo __LINE__; echo "<br>";
echo "LIMIT: $limit<br>";
echo "ROW COUNT: $row_count<br><br>";
echo "TOTAL PAGES: $total_pages<br><br>";
//Retrieve Values.
$id = $row["id"];
$page_url = $row["page_url"];
$link_anchor_text = $row["link_anchor_text"];
$page_description = $row["page_description"];
$keyphrases = $row["keyphrases"];
$keywords = $row["keywords"];
echo "Id: $id<br>";
echo "Page Url: $page_url<br>";
echo "Link Anchor Text: $link_anchor_text<br>";
echo "Page Description: $page_description<br>";
echo "Keyphrases: $keyphrases<br>";
echo "Keywords: $keywords<br>";
echo "<br>";
echo "<br>";
}
}
else
{
echo __LINE__; echo "<br>";
printf("Error: %s.\n", mysqli_stmt_error($stmt_2));
printf("Error: %d.\n", mysqli_stmt_errno($stmt_2));
die("B. Prepare failed!");
}
mysqli_stmt_close($stmt_2);
if($page>$total_pages) //If Page Number is greater than Total Pages, show only a link to FINAL PAGE.
{
echo ""; echo "<b> Final Page </b>";?><?php
}
else
{
$i = 1;
while($i<=$total_pages)
{
if($i<$total_pages && $i!=$page)
{
echo ""; echo " $i ";?><?php
}
elseif($i==$page) //Bold the Current Page Number.
{
echo ""; echo "<b> $i </b>";?><?php
}
else
{
echo ""; echo " $i ";?><?php
}
$i++;
}
}
?>
Typos were on the urls in the pagination section. Now, I fixed them and so pagination section working fine.
Can someone be kind enough to checkout my code and let me know which lines to weedout if they are not really necessary.
I think neither lines are reached because the statement is valid but returns zero rows. Before $query_2 = ... could you add a check on $row_count to see if there are any rows?
To answer your second question, $total_pages = ceil($row_count/$limit); is the correct one to use because mysqli_stmt_bind_result returns a boolean so $result_1 would be true or false.
Personally I prefer object oriented style rather than procedural style as you have used because I find it easier to read but the choice is yours.
I'm quite new to PHP. I'm playing around with connecting to a sqlite database and I've done that successfully. For some reason my pagination is not working.
The value of $page won't go beyond 2. Can someone help me out, I'm sure its probably a simple mistake. (So currently it does change from the first page to the next.
<?php
try
{
//open the database
$db = new PDO('sqlite:client.db');
//create the database
$db->exec("CREATE TABLE IF NOT EXISTS Client (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(50), gender VARCHAR(50))");
$page = 1;
if(!empty($_GET['page'])) {
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
if(false === $page) {
$page = 1;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['Previous'])) {
print 'current value of $page = ' . $page;
print "<br>";
if($page <= 0) {
$page = 1;
}else {
$page = $page - 1;
}
} else if(isset($_POST['Next'])) {
print 'current value of $page = ' . $page;
print "<br>";
$page = $page + 1;
}
}
// set the number of items to display per page
$limit = 10;
// build query
$offset = ($page - 1) * $limit;
if($offset <= 0) {
$offset = 0;
}
print '$page = ' . $page;
print "<br>";
print '$offset = ' . $offset;
//now output the data to a simple html table...
print "<table border=1>";
print "<tr><td>Id</td><td>First Name</td><td>Last Name</td><td>Age</td><td>Gender</td></tr>";
$sql = "SELECT * FROM Client LIMIT " . $offset . "," . $limit;
$result = $db->query($sql);
//$rows = count($result);
//print $rows;
//checks if table has data
//$count = $result->fetchColumn();
foreach($result as $row)
{
print "<tr><td>".$row['id']."</td>";
print "<td>".$row['first_name']."</td>";
print "<td>".$row['last_name']."</td>";
print "<td>".$row['email']."</td>";
print "<td>".$row['gender']."</td></tr>";
}
print "</table>";
print "<br>";
//print "<button type=\"button\" name=\"button\"><< Previous </button>";
//print "<button type=\"button\" name=\"button\">Next >></button>";
print "<form class=\"\" action=\"\" method=\"post\">";
print "<button type=\"submit\" name=\"Previous\">Previous</button>";
print "<br><br><button type=\"submit\" name=\"Next\">Next</button>";
print "</form>";
// close the database connection
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
All credit goes to Ryan-Vincent for helping me solve this.
Basically, I had everything working, but had the incorrect attribute for the form action.
This is the only thing I changed and it worked fine (this is the opening form tag in html, notice the page url parameter gets its value from the php page variable.
print "<form class=\"\" action=\"?page=$page \" method=\"POST\">";
Hope this helps other php newbies.
I was actually working on an answer for someone elses question untill I run into something odd. The question was about writing a pagination system. The user wanted something extra added to his current system. So I've written the following code:
<?php
// Database Settings
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
// Establish Connection to the Database
$dbh = new PDO('mysql:host='. $dbhost .';dbname='. $dbname, $dbuser, $dbpass, array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Selecting the data from table but with limit
$query = 'SELECT * FROM table_name ORDER BY column_name ASC LIMIT :start, :page';
// Prepare query
$pre = $dbh->prepare($query);
// Binding values
$pre->bindParam(':start', $start_from);
$pre->bindParam(':page', $per_page);
// Results per page
$per_page=2;
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
?>
<!-- Start building HTML table -->
<table>
<?php
// Execute query
try {
$pre->execute();
// Fetch all results
$results = $pre->fetchAll(PDO::FETCH_ASSOC);
// Loop through results
foreach($results as $data){
// Display results in HTML table
echo "<tr>";
// Add/Remove your column names here
echo "<td>". $data['column_name'] ."</td>";
echo "<td>". $data['column_name'] ."</td>";
echo "<td>". $data['column_name'] ."</td>";
// Close HTML table row
echo "</tr>";
}
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
?>
<!-- End building HTML table -->
</table>
<div>
<?php
// Now select all data from table
$query = 'SELECT * FROM users';
// Prepare the query
$pre = $dbh->prepare($query);
// Execute the query
try {
$pre->execute();
// Count the results
$total_records = $pre->rowCount();
// Keep a record of total number of rows
$total_rows = $total_records;
// Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
// Going to first page
echo "<center><a href='pagination.php?page=1'>First Page</a> ";
// Showing number of pages in between last page
for ($i=1; $i<=$total_pages; $i++){
echo "<a href='pagination.php?page=". $i ."'>". $i ."</a> ";
}
// Going to last page
echo "Last Page</center> ";
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
// Calculate first and last item on current page
$first = $page * $per_page - $per_page;
$last = $page * $per_page;
// Showing the results
echo "<br />";
echo "Showing ". $first ." to ". $last ." in total record of ". $total_rows;
?>
</div>
The code returns no errors and seems to display the data just fine. However, everything echo'd from line 112 (underneath // Going to last page) won't display on the page. It does display in the HTML source code with the correct values. But it's completely marked red in both Firefox and Chrome.
I've never encountered this before and I'm wandering what's causing this?
This kind of problem generally means that your HTML is not well formed.
Look at that line :
echo "Last Page</center> ";
A simple quote is missing before >Last Page ;)
it should be
echo "<a href='pagination.php?page=". $total_pages ."'>Last Page</a></center> ";
I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";
If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>
You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>
I currently have an index.php page which serves as an index for a "My Bookmarks" page. The page returns the results of a MySQL Database Query, into a table, which is automatically generated. The code is only supposed to allow five records per page, before generating an additional page (thus inserting page numbers/links at the bottom) - For some reason, my code just stopped working/producing the page numbers.
What is wrong with my code? I can't seem to find the problem and I don't know what to search on the forum; any guidance is much appreciated.
Here is my index.php code:
<?php
session_start();
//check session first
if (!isset($_SESSION['email'])){
echo "You are not logged in!";
exit();
}else{
//include the header
include ("../includes/header.php");
require_once ('../../mysql_connect.php');
echo ("<center>");
echo ("<div class='bookmarkMenu'><h1 style='text-decoration:underline;'>Q & A Database</h2><p>");
echo ("<p><a class='bookmarkAdd' href=add.php>Add Record</a> ");
echo ("<a class='bookmarkSearch' href=searchform.php>Search Records</a></p><hr /></div><br />");
//Set the number of records to display per page
$display = 5;
//Check if the number of required pages has been determined
if(isset($_GET['p'])&&is_numeric($_GET['p'])){//Already been determined
$pages = $_GET['p'];
}else{//Need to determine
//Count the number of records;
$query = "SELECT COUNT(ID) FROM bookmark";
$result = #mysql_query($query);
$row = #mysql_fetch_array($result, MYSQL_NUM);
$records = $row[0]; //get the number of records
//Calculate the number of pages ...
if($records > $display){//More than 1 page is needed
$pages = ceil($records/$display);
}else{
$pages = 1;
}
}// End of p IF.
//Determine where in the database to start returning results ...
if(isset($_GET['s'])&&is_numeric($_GET['s'])){
$start = $_GET['s'];
}else{
$start = 0;
}
//Make the paginated query;
$query = "SELECT * FROM answers LIMIT $start, $display";
$result = #mysql_query ($query);
//Table header:
echo "<table class='bookmarksTable' cellpadding=5 cellspacing=5 border=1><tr>
<th>ID</th><th>Question</th><th>Answer</th><th>Comment</th><th>*</th><th>*</th></tr>";
//Fetch and print all the records...
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td class='bookmarkInfo'>".$row['ID']."</td>";
echo "<td class='bookmarkInfo'>".$row['Question']."</td>";
echo "<td class='bookmarkInfo'>".$row['Answer']."</td>";
echo "<td class='bookmarkInfo'>".$row['Comment']."</td>";
echo "<td class='bookmarkInfo'>Delete</td>";
echo "<td class='bookmarkInfo'>Update</td></tr>";
} // End of While statement
echo "</table>";
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
//Make the links to other pages if necessary.
if($pages>1){
echo '<br/><table><tr>';
//Determine what page the script is on:
$current_page = ($start/$display) + 1;
//If it is not the first page, make a Previous button:
if($current_page != 1){
echo '<td> Previous </td>';
}
//Make all the numbered pages:
for($i = 1; $i <= $pages; $i++){
if($i != $current_page){ // if not the current pages, generates links to that page
echo '<td> ' . $i . ' </td>';
}else{ // if current page, print the page number
echo '<td>'. $i. '</td>';
}
} //End of FOR loop
//If it is not the last page, make a Next button:
if($current_page != $pages){
echo '<td> Next </td>';
}
echo '</tr></table>'; //Close the table.
}//End of pages links
//include the footer
include ("../includes/footer.php");
}
?>
Much thanks,
-Rockmandew
you should be counting from answers table and do page logics based on that then your query on answers table at the bottom would fit