Paginate query result with AJAX and PHP - php

I have a fully working pagination with get method. I get the results from my query and the page is changed when the variable pagination changes on URL. I recently changed the site to ajax and now I can't get the clicked page value from URL
I have a form with some inputs that I use to generate the query and a ajax structure that connects to the PHP file and put the result on a div
My php file:
//items per page
$quantidade = 30;
//actual page
$pagina = (isset($_GET['pagina'])) ? (int)$_GET['pagina'] : 1;
$inicio = ($quantidade * $pagina) - $quantidade;
$sql .= " LIMIT " . $inicio . " , " . $quantidade ;
$qr = mysql_query($sql) or die(mysql_error());
echo "<table id='tab_vendas' border='1' width='100%'>";
echo "<tr><td>Data</td><td>Loja</td><td>Total (AKZ)</td><td>Total (USD)</td><td>Multicaixa</td><td>Saidas</td><td>Visa</td></tr>";
$num_rows = mysql_num_rows($qr);
if($num_rows > 0){
while($ln = mysql_fetch_assoc($qr)){
echo "<tr><td>" . $ln['data']."</td>";
echo "<td>" . $ln['loja']."</td>";
echo "<td>" . $ln['totalkz']."</td>";
echo "<td>" . $ln['totaldollar']."</td>";
echo "<td>" . $ln['multicaixa']."</td>";
echo "<td>" . $ln['saidas']."</td>";
echo "<td>" . $ln['visa']."</td></tr>";
}
}else{
echo "Não foram encontrados registos";
}
echo"</table></div>";
//total
$sqlTotal = "SELECT id FROM vendas";
$qrTotal = mysql_query($sqlTotal) or die(mysql_error());
$numTotal = mysql_num_rows($qrTotal);
$totalPagina= ceil($numTotal/$quantidade);
$exibir = 3;
$anterior = (($pagina - 1) == 0) ? 1 : $pagina - 1;
$posterior = (($pagina+1) >= $totalPagina) ? $totalPagina : $pagina+1;
echo "<div id='paginacao'><a href='?pagina=1'>Primeira</a> | ";
echo "<< | ";
for($i = $pagina-$exibir; $i <= $pagina-1; $i++){
if($i > 0)
echo ' '.$i.' ';
}
echo '<strong>['.$pagina.']</strong>';
for($i = $pagina+1; $i < $pagina+$exibir; $i++){
if($i <= $totalPagina)
echo ' '.$i.' ';
}
echo " | >> | ";
echo " Ultima</div>";
My big question is how can I get the actual page value and how to know what button the user clicked.
I tried to change the link's to a submit button with form attribute that send the form again and run all code again but I can't figured out how to pass the clicked button value.
echo " | <input type='submit' form='filtros' name='$posterior' value='>>'>";
echo " <input type='submit' form='filtros' name='$totalPagina' value='Ultima'></div>";

You can simply modify your code to make this work as it was when the pages where links.
<a class="page" href="url.com?parameter=value&parameter2=value2">pageNumber</a>
javscript code is:
$('.a.page').on('click', function() {
var url = $(this).attr(href);
$.ajax({
type: 'GET',
url: url,
success: function(response) {
$('selector to the container you wish to put the data').html(response.data)
}
});
})
In your php yous should echo json_encode your data
echo json_encode(array('data' => $data));

Related

Pagination in Php SQLITE

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.

"Next" button doesn't refresh page with next 25 results

I have the code below and am trying to get the next 25 results from my sql table to appear on page. However, whenever I click the next button, no information is displayed. I have my offset = ($page - 1) * $items_per_page......I'm struggling to figure this out as it seems so simple compared the other code I've written, but is proving to be very elusive to me....any assistance would be greatly appreciated. My primary issue is that the next link does not provide the next 25 results and I'm unable to determine why and how to correct.
echo "<h3 style='text-align:center;'>Welcome to the Exchange Portal, " . $row['name'] . "! </h3>";
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if(false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page-1)*$items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
//Table for displaying query results
echo "<table class='verify'>";
echo "<tr >";
echo "<td><h3>Name</h3></td><td> </td><td><h3>E-mail</h3></td><td><h3>Phone</h3></td>";
echo "</tr>";
for($i = 1; $i<= $page_count; $i++) {
if ($result_query->num_rows > 0) {
// output data of each row
while($row3 = mysqli_fetch_array($result_query)) {
echo "<tr>";
echo "<td class='dltd2 dlcl'>" . $row3["title"] . "</td><td>" . $row3["title2"] . "</td><td><a href='mailto:" . $row3['email'] . "'>" . $row3["email"] . "</a> </td><td>" . $row3["phone"] . " </td>";
echo "</tr>";
}
} else {
echo "0 results";
}
}
echo "<tr></tr>";
$next_page = $page + 1;
$last_page = $page - 1;
if($paging_info['curr_page'] <= 1) {
echo "<tr>";
echo "<td></td><td colspan='2'><a class='loadlink' href='" . $_PHP_SELF . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] < $page_count) {
echo "<tr>";
echo "<td></td><td><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td><a href='" . $_PHP_SELF . "?page=" . $next_page . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] === $page_count) {
echo "<tr>";
echo "<td></td><td colspan='2'><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td></td>";
echo "</tr>";
}
echo "</table>";
}
}
}
Have you tried to run the rendered SQL.
Output to browser:
"SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page"
try this... and change $page for different values (2,3,...,etc)
<?php
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if (false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if ($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page - 1) * $items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
echo ("RESULTS: ".$result_query->num_rows());
?>

search results in table with rows limit php

I created a web site and i added search option which show search results in table, i want to limit rows number to 5 by page please help me to do that:
i would like also to make search results in random sort for each search operation, not the same results are shown on the first page each search :
this is my search code :
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity'");
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
thank you.
Change your query first into
$query = mysqli_query($con,"SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND()");
We can use a counter to limit the fetching to 5.
$counter=1;
while($row = mysqli_fetch_array($query)){
if($counter<6){
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
}
else {
/* NOTHING TO DO */
}
$counter=$counter+1;
} /* END OF WHILE LOOP */
$anymatches=mysqli_num_rows($result);
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
OR you can try this, just changing your query into:
$query = mysqli_query("SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND() LIMIT 5");
Please make it clear, if you want to show only 5 results or a pagination with 5 rows per page.
If pagination is what you're looking for:
We should start by storing the data first into a table storage. For example we have a table named search with even just one field, searchfield.
$searchword=mysqli_real_escape_string($con,$_POST['searchword']);
mysqli_query($con,"UPDATE search SET searchfield='$searchword'");
/* this is where you store your search text for later purposes */
And we'll fetch it right away (Don't get me wrong, this is for the pagination purposes)
$selectsearch = "SELECT * FROM search";
$querysearch = mysqli_query($con, $selectsearch) or die(mysqli_error($selectsearch));
while($rows = mysqli_fetch_array($querysearch)){
$search = $rows['searchfield'];
}
$query = "SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%'"; /* do your query search */
$result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
echo '<b> '.$count.' result/s found for "'.$search.'"</b>';
$rowsperpage = 5;
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$select="SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%' LIMIT $offset, $rowsperpage"; /* do the query again but with limit */
$result=mysqli_query($con, $select);
/*start of the table*/
{
echo "<table border='1'>
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
}
echo '<table border="0"><tr><td>';
/* ***** build the pagination links ***** */
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <font color='#546f3e'><b>$x</b></font> ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
} // end if
/* ***** end build pagination links ***** */
echo '</td></tr></table>';
You haven't written any pagination code, use MySQL LIMIT , clause.
For ordering randomly, try this:
SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' ORDER BY RAND()
Start with this. In this you need to pass the page number in URL (Eg yourwebsite/your-php.php?page=0)
$page = $_GET['page'];
$perPage = 5;
$start = $page * $perPage;
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' limit " . $start . ',' . $perPage);
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "< /tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
You need to implement pagination in to this by giving link to different page numbers.
I suggest to use some MVC framework rather than having all data retrieval and view in one file.

PHP pagination add ID

I have a kind of problem, with following php code:
$host="localhost";
$user_name="";
$pwd="";
$database_name="";
$conexiune = mysql_connect($host,$user_name,$pwd) or die("Nu ma pot conecta la MySQL!");
mysql_select_db($database_name, $conexiune) or die("Nu gasesc baza de date");
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM citate ORDER BY id DESC LIMIT $start_from, 1";
$rs_result = mysql_query ($sql,$conexiune);
while ($row = mysql_fetch_assoc($rs_result))
echo "<img src='" . $row['poza'] . "' />
<br />
" . $row['titlu'] . "
<br />
" . $row['descriere'] . "
<br />
" . $row['data'] . "
";
$sql = "SELECT COUNT(id) FROM citate";
$rs_result = mysql_query($sql,$conexiune);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
$pagelink ='<< ';
$pagelink_2='>> ';
if($page>1)
echo $pagelink;
if($page<2)
echo "";
for ($i=1; $i<=$total_pages; $i++) {
if ($i != $page)
echo "<a href='lista.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
if ($i==$page)
echo " <strong>". $i . "</strong> "; // defining class in the style sheet you can add colour or border to the displayed number
};
if($page<$total_pages)
echo $pagelink_2;
that code offer me pagination (u allready know that) , and the url bar adress look's like following:
http://www.site.ro/folder/lista.php?page=PAGE-NUMBER
i want to look like following:
http://www.site.ro/folder/lista.php?citat=SOME-NUMBERS&page=PAGE-NUMBER
my database table its populated like that:
--------------------------------------------------------------
| id | poza | titlu | descriere | citat | data | accesari |
--------------------------------------------------------------
i want to extract data from "citat" column , so link from url bar will look like:
http://www.site.ro/folder/lista.php?citat=EXTRACTED-FROM-CITAT&page=PAGE-NUMBER
every time when i press on next page buton, will look like:
http://www.site.ro/folder/lista.php?citat=2748925&page=1
http://www.site.ro/folder/lista.php?citat=2840194&page=2
etcetera..
how can i modify that code?
Thank in advance !
I am ignoring all your security issues.
This will work as long you display only one item per page:
$last_citat = 0;
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<img src='" . $row['poza'] . "' /><br />" . $row['titlu'] . "<br />" . $row['descriere'] . " <br />" . $row['data'] . "";
$last_citat = $row['citat'];
}
and later:
$pagelink ='<< ';
$pagelink_2='>> ';
if($page>1) { echo $pagelink; }
if($page<2) { echo ""; }
for ($i=1; $i<=$total_pages; $i++) {
if ($i != $page) {
echo "<a href='lista.php?citat=".$last_citat."&page=".$i."'>".$i."</a> ";
}
if ($i==$page) {
echo " <strong>". $i . "</strong> ";
}
}
Do you want to filter database results by 'citat' value?
If so, then you have to build links with get parameter 'citat' in them, like:
<<
and later take GET['citat'] and add it in the sql query where part so that only results with certain citat value would be returned, like:
$sql = "SELECT * FROM citate WHERE citat = '".GET['citat']."' ORDER BY id DESC LIMIT $start_from, 1";
NOTE: This is not real example and it is WRONG TO USE LIKE WRITTEN: you must escape GET['citat'] because otherwise your database will be hacked very easily and very soon!
Get value of citat from while loop:
$citat = $row['citat'];
Then you can use $citat wherever and however you want, and to check the citat parameter in url you can do it by:
if (isset($_GET['citat'])) {
//it's there do something
} else {
//it's absent
}

Passing html attribute value to the next script in php

I have three php scripts. main.php questions.php and values.php
Here's the code
main.php
<html>
<head>
<title></title>
</head>
<body>
<h1>Be Prepare for the battle</h1>
<?php
$strTitle = "Begin";
$strLink = "<a href = 'question.php?ques_id=1'>" . $strTitle ."</a>";
echo $strLink;
?>
</body>
</html>
questions.php
<?php
require_once('../connect.php');
$quesSQL = mysql_query("SELECT * FROM `questions` WHERE `ques_id`=". $_GET["ques_id"]);
if(!mysql_num_rows($quesSQL) >= 1)
{
die('Complete.');
}
$next = $_GET["ques_id"];
while($row = mysql_fetch_array($quesSQL)) {
$id = $row['ques_id'];
$strTitle = $row['ques_title'];
echo "<li>" . $strTitle . "</li><br/>";
}
$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE " . $id . "= ques_id");
echo "<form action=\"values.php\" method=\"POST\">";
while($row = mysql_fetch_array($optSQL) ) {
$strOptions = $row['options'];
$strValues = $row['values'];
echo "<input type =\"radio\" name =\"valueIn\" value=" . $strValues . " />" . $strOptions . "<br/>";
}
echo "</form>";
$strTitle = "<input type =\"submit\" value=\"Next\">";
$next = $next + 1;
$strLink = "<a href = 'values.php?ques_id=" . $next . "'>" . $strTitle ."</a>";
echo $strLink;
mysql_close();
?>
values.php
<?php
require_once('../connect.php');
$input = $_POST['valueIn'];
$ansSQL = mysql_query("SELECT `answer` FROM questions WHERE 1-".$_GET["ques_id"]."= ques_id");
$marks = 0;
if($input == $ansSQL)
{
$marks = $marks+1;
}
else
{
$marks = $marks+0;
}
echo $marks;
?>
Now problem is i have to pass one value from second script(questions.php) to third script(values.php).
And it is from the <form> section in radio button's name value "valueIn". But I can't do that. Because I'm sending another value ques_id with $strLink variable at the end of the second script.
So how can i do that?
I'm not sure why your using a link to handle what should probably be in the form. As stated by anusha you should be using a hidden input field for ques_id like so
questions.php
<?php
require_once('../connect.php');
$quesSQL = mysql_query("SELECT * FROM `questions` WHERE `ques_id`=". $_GET["ques_id"]);
if(!mysql_num_rows($quesSQL) >= 1)
{
die('Complete.');
}
$next = $_GET["ques_id"];
while($row = mysql_fetch_array($quesSQL)) {
$id = $row['ques_id'];
$strTitle = $row['ques_title'];
echo "<li>" . $strTitle . "</li><br/>";
}
$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE " . $id . "= ques_id");
echo "<form action=\"values.php\" method=\"POST\">";
while($row = mysql_fetch_array($optSQL) ) {
$strOptions = $row['options'];
$strValues = $row['values'];
echo "<input type =\"radio\" name =\"valueIn\" value=" . $strValues . " />" . $strOptions . "<br/>";
}
$next = $next + 1;
$strLink = '<input type="hidden" name="ques_id" value="'.$next.'">';
echo $strLink;
$strTitle = "<input type =\"submit\" value=\"Next\">";
echo $strTitle;
echo "</form>";
mysql_close();
?>
Both variables when then be available via $_POST on the next step like below
$input = $_POST['valueIn'];
$ques_id = $_POST['ques_id'];
You can use hidden input like Mike's answer, or you can still use GET parameter like this:
questions.php
<?php
// .........
// .........
// .........
// .........
// add / change your code for this part
$next = (int) $next;
$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE ques_id = " . $next);
echo '<form action="values.php?ques_id=' . ($next+1) . '" method="POST">';
while($row = mysql_fetch_array($optSQL) ) {
$strOptions = $row['options'];
$strValues = $row['values'];
echo '<input type="radio" name ="valueIn" value="' . $strValues . '" />' . $strOptions . '<br/>';
}
echo '<input type="submit" value="Next">';
echo "</form>";
mysql_close();
// end change
?>
values.php
<?php
// add / change your code for this part
$_GET["ques_id"] = (int) $_GET["ques_id"];
$ansSQL = mysql_query("SELECT `answer` FROM questions WHERE ques_id = " . ($_GET["ques_id"]-1));
// end change
// .........
// .........
// .........
// .........
You can add multiple parameters with 'a' tag.
Like
"<a href = 'values.php?ques_id=".$next." & ques_id1=1'>" . $strTitle ."</a>"
You can also use a hidden input field for variable $question_id and submit the form

Categories