while loop in table [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this problem with this while loop. I want to make a table where an administrator can see users and delete them. So I want this into a table. I have tried some echo's like:
echo <table>, echo <td>, echo </td>, echo </table>.
Unfortunately nothing of this al works.
Can anybody help me?
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
$conn->close();
?>

Simply print a table -
<table>
<tr>
<td>Account</td>
<td>Password</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["username"]. "</td><td>" . $row["password"]. "</td></tr>";
}
?>
</table>

<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table><tr><td>Username</td><td>Password</td><td>Action</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td> modify | delete </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>

$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo "<table>";
echo "<tr>";
echo '<td>Username</td>';
echo '<td>Password</td>';
echo '<td>Action</td>';
echo "</tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
echo '<tr>';
echo '<td>'.$row["username"].'</td>';
echo '<td>'.$password1.'</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
} else {
// nothing in DB is 0 results
echo "0 results";
echo '<tr><td colspan="3" rowspan="" headers="">No records Found</td></tr>';
}
echo "</table>";
$conn->close();

Replace your while loop with :
echo "<table><tr>
<td>Account</td>
<td>Username</td>
<td>Password</td>
</tr>"
while($row = $result->fetch_assoc())
{
echo "<tr><td>".$account."</td><td>" .$row["username"]. "</td><td>" . $password1 . "</td><td>" . $row["password"]. "</td></tr>";
}
echo "</table>";

Related

How to get SQL data to PHP array

I kinda messed up the title, but i will try to explain my problem. i have a html page called leden.html and have a PHP script on it which gets data from my database and creates a table on the html page. Now the part where i get stuck is showing if a member is online and if someone is online the $sql1= "ja" else $sql1= "nee", but i messed up somewhere because when two people are online, the last person who came online shows online and the first dude goes back to "nee". Here is the code, i think something goes wrong at the array part.
<?php
$conn = mysqli_connect("******", "******", "******", "******");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
$row1 = $result1->fetch_assoc();
$nameninsessie = array($row1["username"]);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td>
<td>". $row["email"]. "</td><td>" . $sql1 . "</td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>
You are only getting ONE of the logged in users from the sessie_username query. And also building the array of logged in users incorrectly. See below
<?php
$conn = mysqli_connect("******", "******", "******", "******");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
// initialise the array
$nameninsessie = array();
// loop over all logged in users
while ( $row1 = $result1->fetch_assoc() ) {
// add thir names to an array
$nameninsessie[] = $row1["username"];
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td>
<td>". $row["email"]. "</td><td>" . $sql1 . "</td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>
There is a problem in your data fetching. See the returning array using print_rcommand. You only get the first value of the sql query. So try this. Furthermore you have to initialize the array if not when there is 0 results for the mysql query it will give an error.
<?php
$conn = mysqli_connect("localhost", "root", "*****", "*****");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, email FROM register";
$sessie_username = "SELECT username FROM sessie";
$result = $conn->query($sql);
$result1 = $conn->query($sessie_username);
$nameninsessie = array();
$i=0;
while($row1 = $result1->fetch_assoc()) {
$nameninsessie[$i] = $row1["username"];
$i++;
}
print_r($nameninsessie); //thi is to chek the array when problem solved please comment this line
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (in_array($row["username"], $nameninsessie)) {
$sql1 = "Ja";
} else {
$sql1 = "Nee";
}
echo "<tr><td>" . $row["id"]. "</td><td> " . $row["username"] . "</td>
<td>". $row["email"]. "</td><td> " . $sql1 . "<br></td></tr>";
}
echo "</table>";
} else { echo "0 resultaten"; }
$conn->close();
?>

How can I post my data that i get from SQL into divs in php

I need to make a page that shows posts from database. It has to have a Title and text in the post. I get all of the information from database already, but I don't know how to make it so they are placed into divs. Currently the data I get is set into tables, but I would like to get them into divs. Current code: (note: I have the php and sql connection working)
$sql = "SELECT title, txt FROM xxxxx";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Also by that I was looking for bootstrap panel-group classes, but I was not able to implement that.
It seems you forgot to include your divs:
if ($result->num_rows > 0) {
// output data of each row
echo "<div><table><tr><th>Title</th><th>Text</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["txt"]. " </td></tr>";
}
echo "</table></div>";
} else {
echo "0 results";
}
or if you don't want to include a table but only the divs, simply do it like so:
if ($result->num_rows > 0) {
// output data of each row
echo "<div class='title'><h1>Title</h1>";
while($row = $result->fetch_assoc()) {
echo "<p>" . $row["title"]. "</p><p>" . $row["txt"]. " </p>";
}
echo "</div>";
} else {
echo "0 results";
}

php loop through MySQL records - make decision based on row value

I am using this code to loop through MySQL rows:
$sql = "SELECT * FROM vwPublicServices2 ORDER BY Service_Date__c,
Service_Time__c";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo $row["ServiceDate"]. "(".$date1.")"."<br/>";
echo "<tr><td>" . $row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" . $row["Location__c"]. "</td>
<td>" . $row["PublicName"]. "</td><td>" . $row["FullName"]."</td>
</tr>";
// set $date1 = row ServiceDate
$date1 = $row["ServiceDate"];
} else {
// echo row data to follow on previous - i.e date has not
changed
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Every time row["ServiceDate"] changes I want to insert some header data.
Here are some of the results:
There are two things I cannot understand - why is the date check 'one date out' i.e in the second line why is 14/05/2017 compared to 13/05/2017 ?
Also, why does the second echo statement not appear ?
I think I am missing some fundamental point with the way the while loop works ! Any help here much appreciated. Thanks.
Here is an illustration of the header insertion on date change using similar data to above. (This web page pulls data from Salesforce via their API and uses a similar date check to the one in the MySQL code above - but it loops through the data with a For..Each loop )
Here is the full code incorporating moni_dragu's code:
<style>
table, th, td {
border: 1px solid black;
}
</style>
<?php
$servername = "****";
$username = "****";
$password = "****";
$dbname = "*****";
$date1="01/01/1900";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM vwPublicServices2 ORDER BY Service_Date__c,
Service_Time__c";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo "<tr><th>".$row["ServiceDate"]. "(".$date1.")"."</th></tr>";
echo "<tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
$date1 = $row["ServiceDate"];
}
// echo row data
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
Thanks again for solving the problem and also for sharpening my understanding of while loops with a MySQL recordset.
You need to output your header only when the date changes. Because the data row needs to be displayed all the time (for each each row) it does not need to be in the if condition at all.
Your code should look something like this:
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
// insert header stuff on change of $date1
if ($row["ServiceDate"] <> $date1) {
echo "<tr><th>".$row["ServiceDate"]. "(".$date1.")"."</th></tr>";
echo "<tr><th>Service Date</th><th>Service Time</th>
<th>Church</th><th>Service</th><th>Service Leader</th></tr>";
$date1 = $row["ServiceDate"];
}
// echo row data
echo "<tr><td>" .
$row["ServiceDate"]. "</td><td>" .
$row["Service_Time__c"]. "</td><td>" .
$row["Location__c"]. "</td><td>" .
$row["PublicName"]. "</td><td>" .
$row["FullName"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}

How to put my data from a table using PHP/MySQL

I know this question has been asked multiple times but looking through and trying the solutions has not got me far.
I would like the table to have a format of:
First name | Last Name
Mike Hannover
Steve Dortmund
however I am not sure how to achieve this, it currently lays out like as I have taken the table coding out that I have tried.
FirstName: Mike - LastName: Hannover
FirstName: Steven - LastName: Dortmund
I have attached my PHP code below and thanks in advance for your time and help.
<body>
<h1>Clients information</h1>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "lastgo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT FirstName, LastName FROM info";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "FirstName: " . $row["FirstName"]. "
- LastName: " . $row["LastName"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Probably the easier way is to build your table is to do it as a table.
// Just looking at this part of the code.
if ($result->num_rows > 0) {
// Begin a table
echo "<table>";
// Create a header row
echo "<tr><th>First Name</th><th>Last Name</th></tr>";
while($row = $result->fetch_assoc()) {
// output data for each row
echo "<tr><td>" . $row["FirstName"]. "</td><td>" .
$row["LastName"]. "</td></tr>";
}
// Close table
echo "</table>";
}
As far I understand you want to print your FirstnName and LastName like so
Change your while loop to:
echo "<table>";
echo "<th>FirstName: </th><th>LastName</th>";
while($row = $result->fetch_assoc()) {
echo "<tr>".
"<td>" .$row["FirstName"] ."</td>"
."<td>" . $row["LastName"]."</td>
.</tr>>";
}
echo "</table>";
you can use a table, then style it the way you want the records to appear
<table>
<tr>
<td>Name</td>
<td>Last Name</td>
</tr>
<?php foreach ($names as $value) : ?>
<tr><td><?php echo $value['Name']; ?></td><td><?php echo $value['LastName']; ?></td></tr>
<?php endforeach; ?>
</table>

Display result from database in two columns

EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.

Categories