Getting the first row from database refreshes the page continuously - php

I'm trying to get the first row from phpmyadmin. After trying to do it different ways, I thought I would ask. Right now, the problem is that it is refreshing the browser continuously.
i've tried to add a die, but I'm not very good at mysqli/php
$sql2="SELECT * FROM stellingen WHERE REGIOID=1;";
$records2 = mysqli_query($con, $sql2);
$row = mysqli_fetch_assoc($records2);
while ($stellingen = mysqli_data_seek($records2, 0)){
echo "<p>".$stellingen['Stelling']."</p>";
}
I expect the php to fetch the first data. In the context of a loop where the next data will nneed to come later in the page.
some more code
<div class="stellingen">
<div class="stelling-wrapper" id="btn1">
<img src="images/button.svg" alt="Show more..." class="btn" id="bton">
<p type="button" class="stelling">
<?php
$sql2="SELECT * FROM stellingen WHERE REGIOID=1;";
$records2 = mysqli_query($con, $sql2);
$row = mysqli_fetch_assoc($records2);
while ($stellingen = mysqli_data_seek($records2, 0)){
echo "<p>".$stellingen['Stelling']."</p>";
}
/*
if ($recordscheck2 > 0){
while ($stellingen = mysqli_fetch_assoc($records2)){
echo "<p>".$stellingen['Stelling']."</p>";
}
}*/
?>
</div>
<div id="p1">
<table id="tabel" border=1px class="data">
<tr>
<th>Title</th>
<th>Source</th>
<th>Weight</th>
<th>Date</th>
</tr>
<?php
$sql1="SELECT * FROM stelling WHERE stelling_iD=1;";
$records1 = mysqli_query($con, $sql1);
$recordscheck = mysqli_num_rows($records1);
if ($recordscheck > 0){
while ($stelling = mysqli_fetch_assoc($records1)){
echo "<tr>";
echo "<td>".$stelling['Title']."</td>";
echo "<td>".$stelling['Source']."</td>";
echo "<td>".$stelling['Wheight']."</td>";
echo "<td>".$stelling['Timer']."</td>";
echo "</tr>";
}
}
?>
</table>
</div>

Okey, so your using mysqli_data_seek() in a while() loop, which is wrong...
<?php
if($result = mysqli_query($con, "SELECT * FROM stellingen WHERE REGIOID=1;")) {
mysqli_data_seek($result, 0);
$row = mysqli_fetch_assoc($result);
echo "<p>{$row["Stelling"]}</p>";
}
?>
Should work for you.
[Edit] I corrected my variable names.

Related

Navigate and pass values to another page in PHP (HTML)

I am new to PHP and here I have a problem navigating to another page by clicking in one of the rows of the table. I want also to pass the values of that row to the next page and display them there.
Here is my code:
<body>
<div class="wrapper">
<h1>List of customers</h1>
<?php
require_once "db_settings.php";
$sql = "SELECT id, username FROM customers";
$result = $dbi->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Username</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr> <td>".$row["id"]."</td> <td>".$row["username"]." </td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
You have to add "a" tag with GET link:
while($row = $result->fetch_assoc()) {
echo "<tr> <td>".$row["id"]."</td> <td><a href='new_url?user_id=".$row["id"]."'>".$row["username"]." </a></td> </tr>";
}
and in the new_url page use
$user_id = $_GET['user_id']
and now you can use the id to get the user from the database again

Table pagination not going to a different page when selected

I added pagination to a table a I created a while back and I have not ever been able to get it to work correctly since.
The table limit works, but that's it. If I select "First, Last or the page number" it just reloads the page, but the new page does not display.
If I set the page limit to a low number like 5 and select 'Last Page', when the page loads it shows =1 like it doesn't know there are other pages.
<?php
$con = mysqli_connect("localhost","root","","bfb");
$per_page=20;
if(isset($_POST["page"])) {
$page = $_POST["page"];
}
else {
$page = 1;
}
//Page will start from 0 and multiply per page
$start_from = ($page-1)*$per_page;
//Selecting the data from the table but with limit
$query = "SELECT * FROM orders LIMIT $start_from, $per_page";
$result = mysqli_query($con, $query);
?>
<table class="tableproduct">
<tr>
<th class="thproduct">Order ID</th>
<th class="thproduct">Customer Name</th>
<th class="thproduct">Product</th>
<th class="thproduct">Total Price</th>
<th class="thproduct"></th>
<th class="thproduct"></th>
</tr>
<?php
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="orderhistory.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['product_name']; ?> </td>
<td class="tdproduct"><?php echo $row['total_price']; ?> </td>
<td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
<?php
//Count the total records
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records /$per_page);
?>
<span class="spandarkblue">
<?php
//Going to first page
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='orderhistory.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>";
?>
Any ideas?
Found below issues in you code.
1) You are trying to get page value from URL using POST, where as you need to GET method to fetch values from URl. Using POST is returning null value, so $page value is always set to 1
So use $_GET["page"] instead of $_POST["page"]
2) You are preparing pagination by considering row count of the query which you are executing. But as you have added limits , your $total_records is always equals to $per_page value or less, resulting in only one page number.
Use the following code for generate pazination
$query1 = "SELECT count(*) as totalRecords FROM orders";
$result1 = mysqli_query($con, $query1);
if ($result1) {
$row1 = mysqli_fetch_assoc($result1);
$total_records = $row1['totalRecords'];
if ($total_records == 0) {
echo 'No results founds.';
}
}
Instead of below code
if ($result != false) {
$total_records = mysqli_num_rows($result);
if ($total_records == 0) {
echo 'No results founds.';
}
}
Hope it helps you.
You need to fetch all the data to know the total of your records, then you show only the desired data whithin a for() loop, so remove LIMIT $start, $per_page from your query, in this node you will always have 20 or less results
In your while() save the data in arrays like this:
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$ordID[$index] = $row["order_id"];
// The rest of your data...
$index++; // Increment the index
}
Then you chan show only the desired data:
for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
echo $orderID[$i];
// And so on...
}
The min() function returns the lowest value between two vars, in this way in the last page, if you have 17 products instead of 20 you will not have errors.

echo from sql to table

I need to select some data from mysql and echo them into a table,
I have 20 entries which I want to echo them into 5by4 table I can select them like this:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr> <td class="auto-style3">
<?php
while($row = $result->fetch_assoc()) {
?>
<?php echo "<br>". $row["player"].""; ?>
<?php
}}
?>
</td> </tr> </table>
it gives me something like this:
but I want it like this:
Can anyone help?
try it like this, using $count to count your item in array. and after 5 items have been echoed, then you put <tr> to echo in new row
<?php
$count=0;
echo "<table>";
while($row = $result->fetch_assoc()) {
if($count==0) {
echo "<tr>";
}
$count++;
echo "<td>".$row["player"]."</td>";
if($count==5) {
echo "</tr>";
$count=0;
}
}
echo "</table>";
?>
I changed your code to add a counter and close the tag every five records, this is the result:
<?php
$sql = "SELECT player FROM `prize` WHERE inviter='$player'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table style="width: 100%;border:1px">
<tr>
<?php
$counter=0;
while($row = $result->fetch_assoc()) {
if($counter == 4){
echo '</tr><tr>';
$counter=0;
}
?>
<td><?php echo $row["player"].""; ?></td>
<?php
$counter++;
}}
?>
</tr> </table>

PHP Table. Displaying multiple rows into one

Here is my code:
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><Center>Username:</Center><br /></td>
<td width="50%"><center>Numebr Of Warnings:</center><br /></td>
</tr>
</table>
<?
$query = mysql_query("SELECT * FROM warnings");
$numrows = mysql_num_rows($query);
if($numrows != "0"){
while($row = mysql_fetch_assoc($query)){
$warned = $row['username'];
$by = $row['by'];
$sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
$warns = mysql_num_rows($sql);
?>
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><center><b><?php echo $warned; ?></b></center></td>
<td width="50%"><center><b><?php echo $warns; ?></center></b></td>
</tr>
</table>
<?php
}
}
else
echo "No Currently Warned Users";
?>
<hr />
And here is the result:
How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.
Help would be appreciated.
You can use DISTINCT in your query to avoid duplicate rows in your results:
SELECT DISTINCT username, `by` FROM warnings
Change the mysql_fetch_assoc with mysql_feth_array
You're getting the user with double record. Then for each user you are getting 2 lines
The best way to check it is to add pseudo-points to your code.
$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
$arr[]=$row;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
exit;
Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again
Here is the table
(by,id,username,date)
a1,1,u1,date
a2,2,u2,date
a3,3,u2,date
a4,4,u2,date
And here is the php code
<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");
$warns = mysqli_query($conn,"select username, count(username) as warncount from dtest.warnings W group by username");
while($line = mysqli_fetch_array($warns))
{
echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>

Having a lot of trouble not uselessly accessing my DB

<?php
session_start();
include '../dbconnect_form_fields.php';
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr><td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td></tr>
<tr><td><select>";
$res1 = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
while($row1 = mysql_fetch_assoc($res1)){
echo "<option value='".$row1['id']."'>".$row1['field']." ".$row1['price']."</option>";
}
?>
For some reason, when i change that line that says $res1 = mysql_query blah blah blah, It doesnt seem to work, the select field is empty with no options. It seems as though I would have to define $res as a mysql_fetch and for a second select box, access the DB a second time but using a different variable...
How can I make the $res variable carry across the loops without having to access the DB so many times... I play to have six of these loops... Help me gurus!
I think you might need to clarify what your intentions are. From your code you are creating two of the same select elements. If you want to output the same select element twice, you could try
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<?php for($i=0;$i<1;$i++) { //Loop twice to create a second select element ?>
<td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td>
<?php } //End of for loop ?>
</tr>
Alternatively, you could just copy/paste the same while loop twice without changing any variables
<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
</tr>
</table>

Categories