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

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

Related

How to list out two different product's details at the same table with php

I only can list out all the data row by row but I want to list out those 2 products like this from top to bottom, not row by row within
index.php
while ($row = mysqli_fetch_array($results)) {
echo '<td><'.$row['name'].'></td>';
}
server.php
$db = mysqli_connect('localhost','root','','crud');
$results = mysqli_query($db, "SELECT * FROM info");
If I really understand, you want each product to be in a different line. If yes, you could create new rows directly inside your loop like this
while ($row = mysqli_fetch_array($results)) {
echo '<tr><td>'.$row['name'].'</td></tr>';
}
<?php
while($row = mysqli_fetch_array($results)){
$detail_row.="<td>" . $row['items'] . "</td>";
$category_row.="<td>" . $row['items'] . "</td>";
}
?>
<table>
<tr>
<td>Details</td>
<?php echo $detail_row; ?>
</tr>
<tr>
<td>Categories</td>
<?php echo $category_row; ?>
</tr>
</table>

Getting the first row from database refreshes the page continuously

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.

Formatting mysqli output table with different functionalities

I am outputting newsletter email registrations.
How can I format them into tables and what edits can I do so that I can drop that row also.
My PHP code is :
<?php include "navbar-datacheck.php" ?>
<?php
include "../backend/db.php";
$sql = "SELECT * from newsletter";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
print "id: " . $row["id"]. " - Email ID: " . $row["email"]."<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
?>
Construct your table comprising of all the newsletter details in the following manner,
// your code
if ($result->num_rows > 0) {
// output data of each row
?>
<table>
<tr>
<td>ID</td>
<td>Email</td>
<td>Action</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td>Delete</td>
</tr>
<?php
}
?>
</table>
<?php
} else {
echo "0 results";
}
// your code
... what edits can I do so that I can drop that row also.
As you can see, I have added a third column Action in your table so that you could delete a row of your choice at any given time. So once the user clicks on Delete link, this is how you should process and delete the corresponding record in delete.php file.
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// Now delete the corresponding record from the table
}

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>

How do I make this into a table and sort it alphabeticaly

This is a piece of my php script where the database it mirrored into a webpage.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["id"]. "" . $row["student"]. "" . $row["subject"]."". $row["grade"]. "<br></div>";
}
} else {
echo "0 results";
}
So sort it alphabetically, it needs to go back to your query.
To do this you need to do your normal query and add ORDER BY whatever_field_you_want_to_set_as_alphabetical ASC at the end of it. Example:
SELECT * FROM table ORDER BY name ASC
That sorts out the alphabetical issue.
To put it into a table, you need to first check if there are results.
If there are, then create your table and make sure for every row you create a new row in the table.
<?php if ($result->num_rows > 0) {
<table>
<thead>
<th>
<td>ID</td>
<td>Student</td>
<td>Subject</td>
</th>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['student'];?></td>
<td><?php echo $row['subject'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo "0 Results";
}
?>

Categories