i have a table in php whose data comes from sql database, so i have kept a pagination for the table because the data is long
<?php
require_once "config.php";
$perpage = 5;
if(isset($_GET['page']) & !empty($_GET['page'])){
$curpage = $_GET['page'];
}else{
$curpage = 1;
}
$start = ($curpage * $perpage) - $perpage;
$sql = "SELECT * FROM `registers` LIMIT $start, $perpage";
$PageSql = "SELECT * FROM `registers`";
$pageres = mysqli_query($link, $PageSql);
$totalres = mysqli_num_rows($pageres);
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<thead>";
echo "<tr>";
echo "<th class='serial'>ID</th>";
echo "<th>Name</th>";
echo "<th>Phone Number</th>";
echo "<th>Email</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
?>
<nav aria-label="Page navigation">
<ul class="pagination">
<?php if($curpage != $startpage){ ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $startpage ?>" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">First</span>
</a>
</li>
<?php } ?>
<?php if($curpage >= 2){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $previouspage ?>"><?php echo $previouspage ?></a></li>
<?php } ?>
<li class="page-item active"><a class="page-link" href="?page=<?php echo $curpage ?>"><?php echo $curpage ?></a></li>
<?php if($curpage != $endpage){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $nextpage ?>"><?php echo $nextpage ?></a></li>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $endpage ?>" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Last</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
<?php }?>
the data is correctly added into the database, but the problem is the pagination is nowhere seen in the page. when i just put the html it was coming,but when i added the php code inside it,its is not coming. can anyone tell me what could be the problem please?
Move <nav></nav> code inside
if(mysqli_num_rows($result) > 0){ }
Before or after your table.
Issue is that you now show it only if your if statement fails: if($result = mysqli_query($link, $sql)){ } else { }
Tip:
To Prevent SQL Injection i suggest:
PHP MySQLi Prepared Statements Tutorial
PHP PDO Prepared Statements Tutorial
Related
As for my HTML code is
<div class="right">
<h2>Product Detail</h2>
<div id="righttop"></div>
<button id="Add">Add</button>
</div>
<div class="rightbot">
<h2>Shopping Car</h2>
<div id="rightbot"></div>
</div>
And the PHP is
mysqli_select_db($con,"products");
$sql="SELECT product_id, product_name, unit_price, unit_quantity, in_stock FROM products WHERE product_id = '".$productid."'";
$result = mysqli_query($con,$sql);
echo "<table>";
echo "<th>Product Name</th>";
echo "<th>Unit Price</th>";
echo "<th>Unit Quantity</th>";
echo "<th>In Stock</th>";
while($row = mysqli_fetch_array($result)) {
echo "<tr id='detail'>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['unit_price'] . "</td>";
echo "<td>" . $row['unit_quantity'] . "</td>";
echo "<td>" . $row['in_stock'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo $_POST['detail'];
mysqli_close($con);
?>
div#righttop is changed by another button.
How can I pass the current content from div#righttop to div#rightbot?
If you meant to transfer the contents of righttop to rightbot, here is a way. On click of the button, before changing anything in righttop, do this:
$('#rightbot').html($('#righttop').html());
dbconnect.php
<form method="post" action="a.php">
<select name="taskOption" id="cust-id" onchange="showUser(this.value)">
<?php
include 'orderSelect.php';
echo '<option>View Order</option>';
while($row = mysqli_fetch_array($result)):;?>
<option value='<?php echo $row[0]; ?>'><?php echo $row[1]; echo " ";
echo $row[2]; ?></option>
<?php endwhile; ?>
</select>
</form>
a.php
<?php
include 'connect.php';
$q = intval($_GET['q']);
$sql = "SELECT id, firstname, lastname,productOne, quantity, price
FROM orderlist";
$result = mysqli_query($conn, $sql);
echo "<table >
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
$tquan = $row['quantity'];
$tprice = $row['price'];
$total = $tquan * $tprice;
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['productOne'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $total . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I got it working. IT display the record/ users that I want to see when something is selected from the drop down list, but the problem is it's displaying the entire database data. I only want to get that specific that of the selected person. Any Ideas how to solve this problem? I would appreciate it.
Image Example
I only wanted the first row to be selected when I select from the dropdown list
Your SELECT is of the whole database you need to select something exact.
For example:
$sql = "SELECT 'id' FROM Users WHERE username='$name'";
That is an example of using the WHERE clause.
In addition to what the above said.
You mentioned you only wanted 1 record.
Add " LIMIT 1" to the end of your query
OR
Change the 'while' command to an 'if' command and it will only run once.
I am trying to make it so when a person selects an option from a drop down menu, another drop down menu shows up. So if I have a drop down menu with 3 choices, "Vancouver, "Singapore","New York". When a user selects Vancouver a drop down menu shows up with a couple of options, if they select new york another drown down menu shows up. To make things complicated I am also using php as its going to be running things on the server side eventually.
echo "<script type=\"text/javascript\">";
echo "function getDropDown(sel){";
echo "hideAll();";
echo "document.getElementById(sel.options[sel.selectedIndex].value).style.display ";
echo "= 'block';";
echo "}";
echo "function hideAll(){";
echo "document.getElementById(\"vancouver\").style.display = 'none';";
echo "document.getElementById(\"singapore\").style.display = 'none';";
echo "document.getElementById(\"newyork\").style.display = 'none';";
echo "}";
echo "</script>";
echo "<tr>";
echo "<td align=\"right\">";
echo "<td>";
echo "<select name=\"optionDrop\" onChange=\"getDropDown(this)\">";
echo "<option value=\"\">Please Select</option>";
echo "<option value=\"vancouver\">Vancouver</option>";
echo "<option value=\"singapore\">Singapore</option>";
echo "<option value=\"newyork\">New York</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=\"right\">City</td>";
echo "<td>";
echo "<div id=\"vancouver\" style=\"display: none;\">";
echo "<select name=\"optionDrop\" >";
echo "<option value=\"\">Please Select</option>";
echo "</select>";
echo "</div>";
echo "<div id=\"singapore\" style=\"display: none;\">";
//echo "<select name=\"optionDrop2\">";
// echo "<option value=\"\">Please Select2</option>";
echo "Sing";
echo "</div>";
echo "<div id=\"newyork\" style=\"display: none;\">";
echo "New York";
echo "</div>";
echo "</td>";
echo "</td>";
echo "</tr>";
Right now when I select the option "Vancouver" a drop down menu does shot up, and when selected other options a text shows up. However if I uncomment the lines
//echo "<select name=\"optionDrop2\">";
// echo "<option value=\"\">Please Select2</option>";
Then nothing works anymore. No matter what option I select nothing shows up. I am completely stuck and cant figure out what is wrong with it.
Hum... first of all I'll give you a hint you might have begged for many years ago:
Closing a php tag within a php file won't break your code.
If you want to display only HTML like you are doing in the code you posted, just do the following:
<?php
$variable = "test";
// Some random things blabla.
?>
<span class="test">
<?php echo $variable; ?>
</span>
<?php
$something = "something_else";
// Some other php
?>
About your problem, there you go:
http://codepen.io/anon/pen/GxytE
I have a button within a php foreach statement which loads a modal. How can i pass the particular id ($animalid) to the modal? I have it kind of working but the modal will load the same id in each modal popup. See my php foreach code below and also part of the modal.
$pdo2 = Database::connect();
$sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';
foreach ($pdo2->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['hp'] . '</td>';
echo '<td>'. $row['hpname'] . '</td>';
echo '<td>'. $row['hpage'] . '</td>';
echo '<td>'. $row['hpcolour'] . '</td>';
echo '<td>'. $row['hpmicro'] . '</td>';
echo '<td>';
echo '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
echo ' ';
echo '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
echo '</td>';
echo '</tr>';
$animalid = $row['id'];
}
Database::disconnect();
?>
Modal Code
<div class="modal-body">
<form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">
<span class="form-break">
<?php
// Get Animal id
//$animalid = $data['id'];
echo $animalid;
?>
$animalid is in the loop. If you want to use that value, you can store the data to other array and use it. Hope you get some inspiration.
PHP:
<?php
$pdo2 = Database::connect();
$sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';
$list = array();
foreach ($pdo2->query($sql2) as $row) {
$item = array();
$content = '<tr>';
$content .= '<td>'. $row['hp'] . '</td>';
$content .= '<td>'. $row['hpname'] . '</td>';
$content .= '<td>'. $row['hpage'] . '</td>';
$content .= '<td>'. $row['hpcolour'] . '</td>';
$content .= '<td>'. $row['hpmicro'] . '</td>';
$content .= '<td>';
$content .= '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
$content .= ' ';
$content .= '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
$content .= '</td>';
$content .= '</tr>';
$item['id'] = $row['id'];
$item['content'] = $content;
$list[] = $item;
}
Database::disconnect();
?>
HTML:
<div class="modal-body">
<form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">
<span class="form-break"></span>
<?php foreach($list as $item){ ?>
<?php echo $item['id'];?>
<?php } ?>
or..
<table>
<?php foreach($list as $item){ ?>
<?php echo $item['content'];?>
<?php } ?>
</table>
<!-- ... -->
</form>
</div>
$animalid is being over written each time you iterate through the foreach loop. You would have to build an array to hold all of the animalids, and pass that to your modal or print out the id as you are iterating through the loop.
I have a table in my page that first two columns of it come from a table in database, and third column is for checkbox, if user know the meaning of the words that are displayed in table, check them
I want to update my table in database in this way: for checked checkbox insert 1 to checking column in table and otherwise insert 0
how can I get checkbox value and insert it into right row in database
I have this code until now:
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
echo "<td>";
echo "<input name=\"fahimeh\" type=\"checkbox\" value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<button onclick="ShowMeanings()">ShowMeanings</button>
<button onclick="feedback()">sendfeedback</button>
and it is my javascript code:
<script type="text/javascript">
function ShowMeanings(){
var hidings = document.getElementsByClassName('hiding');
for (var i=0; i<hidings.length; i++){
hidings[i].style.display = 'block';
}
}
</script>
I dont know how can I write feedback(), and how can I get checkbox value and insert it into right row in database
This should work :)
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("SELECT * FROM words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>";
echo "<td>";
echo "<input name=\"fahimeh\" type=\"checkbox\" name='" . $row['id'] . "' value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<button onclick="ShowMeanings()">ShowMeanings</button>
<button onclick="feedback()">sendfeedback</button>