how to paginate the retrieved data from mysql database by using php? - javascript

Here i am collecting the category and area value from the search-form using get method. So as i m having 7 records into the database and i want to display only 4 records per page and when user clicks on Next button then the remaining records need to get displayed but here my code is displaying only first four records from the database but when i click on next button then the remaining 3 records are not getting displayed.
Consider area=Mumbai and category=lawn. and if there is only 7 records into the database then i want first four to get display in first page and remaining 3 into the same page on clicking the next button..
Here is my code:
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("celebration_db",$link);
$q="select count(*) \"total\" from hallinfo";
$ros=mysql_query($q,$link);
$row=(mysql_fetch_array($ros));
$total=$row['total'];
$dis=4;
$total_page=ceil($total/$dis);
$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;
$category = $_GET['category'];
$area = $_GET['area'];
//$category = 'hall';
//$area = 'nagpur';
echo $category;
echo $area;
$q="select * from hallinfo where category = '$category' AND area = '$area' limit $k,$dis";
//$q="select * from hallinfo limit $k,$dis";
$ros=mysql_query($q,$link);
while($row=mysql_fetch_array($ros))
{
$imageId = $row['id'];
echo '<div id="pattern">';
echo '<table>';
echo '<tr>';
echo '<td colspan="2" width="300px" height="200px">
<img src="image-show.php?id='.$imageId.'">';
echo '</td>';
echo '<td width="150px" height="200px">'.$row['description'];
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.$row['rent'];
echo '</td>';
echo '<td>'.$row['capacity'];
echo '</td>';
echo '<td>'.$row['area'];
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
//echo '</table>';
//echo '<br/>';
if($page_cur>1)
{
echo '';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Previous ">';
}
for($i=1;$i<$total_page;$i++)
{
if($page_cur==$i)
{
echo ' <input style="background-color:green;border:2px black solid;border-radius:5px;width:30px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
else
{
echo ' <input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:30px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
}
if($page_cur<$total_page)
{
echo '<input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}
?>

It would seem your primary issue is in your count. While you are selecting only specific rows (in the above example rows with category = .. and area=.., you're initial count of the rows is returning the count of ALL rows in the database. So while there may be 7 rows in total, perhaps there are only 4 rows with the given filter.
As an aside, you should really be using mysqli: http://www.php.net/mysqli

Related

Option in dropdown box render outside

English is not my first language so i'm trying to be as specific as possible.
I have a table stored , all of the signed up account to my website. Inside of it i have a dropdown box automatically generated by php. So the problem is, when i click to changed the option in the dropdown menu, all of my option appeared outside of the box and was duplicated . Just like this error dropdown box. Here is the code
$sql1="Select ID, user_login,user_email, display_name from wp_users ";
$result1=mysqli_query($conn,$sql1);
echo '<div id="div_package" style="display: none" >';
echo '<div style="text-align:center"><h1>Đổi gói thành viên</h1><input type="text" id="myInput_pack" onkeyup="FilterUsers()" placeholder="Nhập thông tin người dùng cần tìm" title="Type in a user" style="width: 20%;height:35px; font-size: 16px;border: 1px solid black; margin-bottom: 12px;text-align:center"></div>';
echo "<table id='tb_user' class='tbadmin' border='1' cellpadding='10' align='center' style='border-collapse:collapse'>";
echo "<tr><th>ID người dùng</th><th>Tên đăng nhập</th><th>Tên hiển thị</th><th>Email</th><th>Gói hiện tại</th><th></th></tr>";
while($row= mysqli_fetch_array($result1))
{
$userid=$row['ID'];
echo '<tr>';
echo '<td>'.$userid.'</td>';
echo '<td>'.$row['user_login'].'</td>';
echo '<td>'.$row['display_name'].'</td>';
echo '<td>'.$row['user_email'].'</td>';
echo "<td><select name='package_$userid' id='package'>";
$sqlcheck="SELECT 1 FROM wp_usermeta WHERE user_id=".$userid." AND meta_key='package_id' LIMIT 1";
$result_check= mysqli_query($conn,$sqlcheck);
if( mysqli_num_rows($result_check)!=0)
{
$sql_metaval="Select meta_value from wp_usermeta where user_id=".$userid." and meta_key='package_id'";
$result_metaval= mysqli_query($conn,$sql_metaval);
while($metaval= mysqli_fetch_array($result_metaval))
{
$val_package=$metaval['meta_value'];
$test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
$sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
$result_test=mysqli_query($conn,$test);
if(mysqli_num_rows($result_test)==0)
{
echo '<option value="">FREE</option>';
$sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
$packages= mysqli_query($conn,$sql3);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '</select>';
}
else
{
while($row= mysqli_fetch_array($result_test))
{
echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
}
$packages= mysqli_query($conn,$sql2);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '<option value="">FREE</option>';
echo '</select>';
}
}
}
else
{
echo '<option value="">FREE</option>';
$sql3="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>''";
$packages= mysqli_query($conn,$sql3);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}
echo '</select>';
}
echo "<td><a onclick='return confirmEdit();' href='#' id='update_$userid' class='update_user'>Cập nhật</a></td>";
echo '</tr>';
}
echo "</table></br></div>";
It's appear that some kind of loop has happened in the first else section. I tried to delete the select tag in side that section, all the option appeared inside but still be duplicated like this one
Can someone show me how to solve the duplicate option? This has trouble me for days.
I think the problem because you loop the selected option and not selected options in same select without exclude the select option from query of get not selected options ($sql2).
$test="Select post_title, ID from wp_posts where post_type='membership_package' and ID=".$val_package ;
$sql2="Select post_title, ID from wp_posts where post_type='membership_package' and ID<>".$val_package ;
-------------------------------------------------------
while($row= mysqli_fetch_array($result_test))
{
echo '<option value="'.$val_package.'">'.$row['post_title'].'</option>';
}
$packages= mysqli_query($conn,$sql2);
while($pack = mysqli_fetch_assoc($packages)){
echo '<option value="'.$pack['ID'].'">'.$pack['post_title'].'</option>';
}

Get ID automatically in input-field after <SELECT> without button click

I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!

Sortable and hidden information through nested accordion in HTML/PHP page?

So i am trying to display data in sql server using PHP. I am using a sortable library i found online and this is my code:
echo "<table id='report' class='sortable'>";
echo "<thead><tr>";
echo "<th>A</th>";
echo "<th>B</th>";
echo "<th>C</th>";
echo "<th>D</th>";
echo "<th>E</th>";
echo "<th>F</th>";
echo "<th>G</th>";
echo "<th>H</th>";
echo "<th>I</th>";
echo "</tr></thead>";
echo "<tbody>";
for($i=0; $i<$tablerows; $i++)
{
echo "<tr>";
echo "<td>".$result[$i]['A']."</td>";
echo "<td>".$result[$i]['B']."</td>";
echo "<td>".$result[$i]['C']."</td>";
echo "<td>".$result[$i]['D']."</td>";
echo "<td>".$result[$i]['E']."</td>";
echo "<td>".$result[$i]['F']."</td>";
echo "<td>".$result[$i]['G']."</td>";
echo "<td>".$result[$i]['H']."</td>";
echo "<td>".$result[$i]['I']."</td>";
echo "<td><div class='arrow'></div></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='5'>";
echo "<div id='nestedAccordion'>";
echo "<h2>COLUMN 1 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 2 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 3 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 4 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "<h2>COLUMN 5 ELABORATE</h2>";
echo "<div>";
echo "</div>";
echo "</div>";
echo "</td>";
echo "</tr>";
When my page is loaded, it can be sorted by the A-I headings in <th> tag. As you can see, in my 'for' loop my last tag has an arrow. I want that so if the user wants more information about the row, he can view that by clicking that arrow, and information that will be displayed is in: <td colspan='5'>
Here is the javascript/jquery:
$(document).ready(function () {
$("#report tr:odd").addClass("odd");
$("#report tr:not(.odd)").hide();
$("#report tr:first-child").show();
$(".arrow").click(function(){
$(this).parents("tr").next("tr").toggle();
$(this).toggleClass("up");
});
var parentDivs = $('#nestedAccordion div'),
childDivs = $('#nestedAccordion h3').siblings('div');
$('#nestedAccordion h2').click(function () {
parentDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
$('#nestedAccordion h3').click(function () {
childDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
});
The question and the problem i have is only either sortable for table or arrow will work. When i load the page, and i click on the arrow to view more information i can view it, but if i sort afterward, the data under the arrow is taken as its own row rather than part of the row it was selected from.
If i sort at start, and then click on arrow, the row below is hidden. I think the problem is in this chunk of code:
$(".arrow").click(function(){
$(this).parents("tr").next("tr").toggle();
$(this).toggleClass("up");
});
Since the next tr is for colspan? But how do i fix it?

How do I get button's ID from a list of buttons?

I haven't worked much with php and javascript and was hoping to get some help. I want to be able to detect which button was clicked exactly. I'm creating one button per row but I don't know how to figure out what row the click was made in. This is what I have so far...
# Get all the table data from the database
$getData = mysqli_query($conn, "SELECT * FROM `query_table`") or die ("Error occured while selecting!");
// echo '<form method="post" action="index.php">';
echo '<table border="1">';
echo '<tr>';
echo '<td>Questions</td>';
echo '<td>Likes</td>';
echo '<td></td>';
echo '</tr>';
# Loop through each row and print out the question
while ($row = mysqli_fetch_array($getData)) {
echo '<tr>';
echo '<td>';
echo $row['Question'];
echo '</td>';
echo '<td>';
echo $row['Likes'];
echo '</td>';
echo '<td>';
?>
<input type="button" id="myButton" value="Like" onclick="myFunc(<?=$row['ID']?>)" />
<?php
echo '</td>';
echo '</tr>';
}
echo '</table>';
// echo '</form>';
echo '<p id="text"></p>';
mysqli_close($conn);
?>
<script type="text/javascript">
function myFunc(id) {
document.getElementById("text").innerHTML = id;
}
</script>
So far this prints out the row id where the button was clicked, but I don't suppose I can assign that parameter value directly to a php variable that I can then use for mysql queries. How is stuff like this normally done, please help.
<input type="button" data-id="<?=$row['ID']?>" value="Like" onclick="myFunc(this)">
and
<script type="text/javascript">
function myFunc(button) {
document.getElementById("text").innerHTML = button.getAttribute('data-id');
}
</script>
In JavaScript the token "this" refers to the context in which the code is running which in this case is the button. Store custom data in attributes prefixed with data- to ensure they are not disturbed - standard practise.

two different submit button on a form codeigniter acting weird

I have two buttons on one submit form but when I post the submit button it returns nothing;"ABOUT THE BUTTON"; the other data is retrieved WELL.
I have been using different names for the submit button but nothing works.. Could someone help me? thank you.
The FORM data is retrieved well but not the submit button value? i need the value to differ the condition
controller: admin.php
function submit_unitgroupwd(){
$this->access_chk(); //CHECK USER ACCESS
$sbm=$this->input->post('sbm'); // returns NOTHING
echo $_POST["sbm"]; // returns undefined index sbm
echo $_POST['sbm']; // returns undefined index sbm
var_dump($_POST); // returns all the post data bt the submit button
//echo var_dump($this->input->post("sbm"));
exit();
if($this->input->post('sbm') == "Update") {
// do update
} else {
// do delete
}
}
view:
echo "<form action=".site_url("admin/submit_unitgroupwd")." method=post id=form>";
echo "<div style='float:left;color:red;' id='res'></div>";
?><?php
foreach($q1->result_array() as $r1){
$bankname= substr($r1["bank"],0,3);
//echo $bankname;
echo "<input type=hidden name=dgroup value='".$dgroup."'>";
echo "<tr>";
echo "<td width=20% colspan=3 style='font-size: 16px;'>".strtoupper($r1["bank"])."</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=100>".$group_[0]."</td>";
echo "<td><input type=text name='$bankname-utama' value='".str_replace(" ","",$r1["utama"])."' style='width: 100%;'></td>";
if ($r1["bank"] == 'ALL'){
echo "<td rowspan='1'><br><input type='checkbox' name='cek[]' id='cek[]' value='$bankname' ></td>";
echo "<input type=hidden name=_code$bankname value='".$r1["bank"]."'>";
echo "</tr>";
echo "</tr>";
continue;
}
echo "<td rowspan=".($dgroup + 1)."><br><input type='checkbox' name='cek[]' id='cek[]' value='$bankname' ><br><br><br><br><br><br><br>";
echo "<input type=hidden name=_code$bankname value='".$r1["bank"]."'>";
echo "</tr>";
$_alp = "a";
for ($_g=0; $_g<$dgroup; $_g++){
echo "<tr>";
echo "<td width=100>".$group_[($_g + 1)]."</td>";
echo "<td width=80%><input type=text name='$bankname-wd$_alp' value='".str_replace(" ","",$r1["wd$_alp"])."' style='width: 100%;'></td>";
echo "</tr>";
$_alp++;
echo "</tr>";
}
}
?>
>
<input type="submit" class="button" value='Update' name="sbm"/>
<input type="submit" class="button" value='Delete' name="sbm"/>
I'm not completely clear on your description of the problem, but you can't name both submit buttons the same.
You seem to think that push one or the other button will only submit THAT button's value, but in fact the second one is always submitted, because it is the last on the form.
So change your button names to "update" & "delete", and then you can check with
if(isset($_POST['update']))....
for example.
However, you might want to look around the internet for some cleaner examples of how to code what you are after.
You could simply use only the
<input type="submit" class="button" value='Update' name="sbm"/>
and use an anchor for the delete action,
echo "<a href ='controller_delete/$item_id' class='button'>Delete</a>
since you normally wont need other inputs on the delete action if you get my point.

Categories

Resources