I made this code to extract data from mysql and bring the results on the page. The results that the code generated goes down the page. I would like them to be set side by side, may be 30 rows, then the next 30 rows. (After 3 columns of table,if there are still more data, they go on like this down the page if possible). I found a similar post about this, but since I am new to this,I couldn't apply the offered solution there to my code. Any help would be greatly appreciated. Thanks!
A simple illustration to show the solution I need:
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"]. "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
Do get the output you describe, you can fetch all rows at once into an associative array, then step through the columns in groups based on the number of columns to display.
NOTE: I added an order by clause to your SQL statement.
<?php
$numRowsPerGroup = 30;
$rowTitles = array(
array('title' => "Ball1",'colname' => "Ball1"),
array('title' => "Ball2",'colname' => "Ball2"),
array('title' => "Ball3",'colname' => "Ball3"),
array('title' => "Ball4",'colname' => "Ball4"),
array('title' => "Ball5",'colname' => "Ball5"),
array('title' => "Ball6",'colname' => "Ball6")
);
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample ORDER BY week";
$result = $conn->query($sql);
$numrows = $result->num_rows;
$allrows = $result->fetch_all(MYSQLI_ASSOC);
if($allrows) {
$rownum = 0;
while($rownum < $numrows) {
// Output heading row for this group
echo "<table>\n";
echo "<thead>\n";
echo "<tr>\n";
echo " <th> </th>\n";
// Calculate the starting and ending column numbers. These correspond to the rows in the results
$startColNo = $rownum;
$endColNo = $rownum + $numRowsPerGroup;
if($endColNo > $numrows) {
$endColNo = $numrows;
}
// Output the week column headers
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <th>".$allrows[$colNo]['week']."</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
// Output each item type row of the columns for this group.
foreach($rowTitles as $idx => $rowInfo) {
echo "<tr>\n";
// Step through the columns for this group for this item within the range.
echo " <td class='rowtitle'>".$rowInfo['title']."</td>\n";
for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
echo " <td>".$allrows[$colNo][$rowInfo['colname']]."</td>\n";
}
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
$rownum = $endColNo + 1;
}
} else {
echo "<p>0 results</p>\n";
}
$conn->close();
?>
It sounds like you want separate tables for each 30 values displayed side-by-side, but with your image it appears that they would also go on to a separate row after three groups of 30. That part can be handled with CSS (i.e. width: 33%;), so I will provide a solution that handles the PHP portion. The following uses a counter to separate each of group of 30 into a new table.
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "db");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// HERE IS WHERE I ADD A COUNTER
$counter = 1;
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"]. "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
// HERE I INCREMENT THE COUNTER, AND REPEAT THE END OF THE CURRENT TABLE/BEGINNING OF THE NEXT IF $counter%30 == 0
$counter++;
if($counter % 30 == 0) { ?>
</table>
<table class="result-table">
<tr>
<th>Week</th>
<th>Ball1</th>
<th>Ball2</th>
<th>Ball3</th>
<th>Ball4</th>
<th>Ball5</th>
<th>Ball6</th>
</tr>
<?php }
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
<html>
<table>
<tr>
<th>Id</th>
<th>Username</th>
<th>Result</th>
</tr>
<?php
$conn = new mysqli("localhost", "root", "", "roboit");
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, result FROM result";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["username"] . "</td><td>". $row["result"]. "
</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</html>
This is the code. I've checked it from different sources (starting from Indian programmers and ending by W3Schools) and they are very similar or even absolutely equal. But it tells me that $result is a non-object. What to do?
I couldn't find the answer on the similar questions too. (here I mean)
<table class="scoretable;">
<tr>
<th>Id</th>
<th>Username</th>
<th>Result</th>
</tr>
<?php
$conn = new mysqli("localhost", "root", "", "roboit");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `id`, `login`, `score` FROM `result`";
$result = $conn->query($sql);
$rows = mysqli_num_rows($result);
if ($rows > 0) {
// output data of each row
while($rows = $result->fetch_assoc()) {
echo "<tr><td>" . $rows['id']. "</td><td>" . $rows['login'] . "</td><td>".
$rows['score']. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
Ok, maybe no one cares but here is the answer.
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();
?>
I'm trying to query MySQL DB and print a single result on the page using PHP.
It's always going to be a single result, so I'm not sure if I need to loop
In any case, would anyone mind advising why the below doesn't work?
Thank you!!
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "success";
}
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Total 2017 " $row["id"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
If there will be only one result you can simplify code to:
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo "Total 2017 " . $row["id"] . "<br>"; // Don't forget dots for concatenation
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>
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.