I've been struggling for weeks just to analyze the problem of my code, the JQuery Pagination table didn't show the PHP value. I thought it would be PHP that causes the problem.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>No.</th>
<th>Nama Mitra</th>
<th>Jenis Kelamin</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
//I think the problem starts here
while ($rowmitra = mysqli_fetch_array($mitra)) {
echo '<tr>';
echo "<th scope='row'>" . $i . "</th>";
echo "<td>" . $rowmitra['id_mitra'] . "</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . $rowmitra['mitra'] . "</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/' . $rowmitra["logo"] . '"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
?>
</tbody>
</table>
And here's what I do to fetch Mitra table
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$rowmitra = mysqli_fetch_array($mitra);
I put it in a different file that I use include 'head.php'; command to merge both. When I fetch it in outside while command it works.
It didn't fetch an array of my PHP when I put it in while. Am I clumsy enough not to know where is the problem exactly? I've tried to match the variable and also search for alternative ways to fetch array. But it didn't work.
Thank you for your help.
You should not declare the result fetch array outside while loop. Remove this $rowmitra = mysqli_fetch_array($mitra); and try with mysqli_fetch_assoc to get database column name.
<?php
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$i = 1;
//I think the problem starts here
if (mysqli_num_rows($mitra) > 0) {
while ($rowmitra = mysqli_fetch_assoc($mitra)) {
echo '<tr>';
echo "<th scope='row'>".$i."</th>";
echo "<td>".$rowmitra['id_mitra']."</td>";
echo "<td>".$i."</td>";
echo "<td>".$rowmitra['mitra']."</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/'.$rowmitra["logo"].'"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
}
?>
Related
*Below is my code
When I click search, it only display one result from my database.
Is there other method to solve this problem?
Or how to save multiple variables in the session and then display it?
<?php
session_start();
?>
<?php
include("htmltable.php");
?>
<table align="center">
<table class="data-table">
<caption class="title">Donor Lists</caption>
<thead>
<tr>
<th>DonorID</th>
<th>Donor</th>
<th>gender</th>
<th>bloodgroup</th>
<th>State</th>
</tr>
</thead>
<tbody>
<td><?php echo("{$_SESSION['DonorID']}"."<br />");?></td>
<td><?php echo("{$_SESSION['FullName']}"."<br />");?></td>
<td><?php echo("{$_SESSION['gender']}"."<br />");?></td>
<td><?php echo("{$_SESSION['bloodgroup']}"."<br />");?></td>
<td><?php echo("{$_SESSION['State']}"."<br />");?></td>
</tbody>
</table>
</table>
This is the process, when user search, it will select from the database and then display it at the search donor page.
<?php
session_start();
include "dbh.inc.php";
if (isset($_POST['submit']))
{
$sql="SELECT * FROM donorregistration WHERE (bloodgroup='{$_POST["bloodgroup"]}' AND State='{$_POST["State"]}')";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['DonorID'] = $row['DonorID'];
$_SESSION['FullName'] = $row['FullName'];
$_SESSION['gender'] = $row['gender'];
$_SESSION['bloodgroup'] = $row['bloodgroup'];
$_SESSION['State'] = $row['State'];
header("Location: ../assignbloodbank/Searchdonor.php?search=success");
exit();
}
}
else
{
echo "<div class='alert alert-danger'><i class='fa fa-users'></i> No Donors Found</div>";
}
}
?>
When you use header("Location") inside of a while loop only the first redirect will work. And when you use exit() no further code will execute. Also, there's no need to set $_SESSION for every row (as that will overwrite the variable too, unless you use an array for each session value with $_SESSION['key'][]).
The easiest approach would be to simply craft your <table> inside of your if statement, and craft your table rows inside of your while loop:
<?php
if (mysqli_num_rows($result) > 0)
{
?>
<table>
<thead>
<tr>
<th>DonorID</th>
<th>Donor</th>
<th>gender</th>
<th>bloodgroup</th>
<th>State</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo "<td>" . $row['DonorID'] . "</td>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['bloodgroup'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
}
?>
</tbody>
</table>
<?php
}
?>
This way, the table itself only gets generated if there are rows returned from the database, meaning you'll never end up with a table with nothing in it.
You need to add new array in existing $_SESSION element rather than override the previous $_SESSION. Add this code in you while loop. if you want to print multiple records then you have to use foreach loop
$_SESSION['DonorID'][] = $row['DonorID'];
$_SESSION['FullName'][] = $row['FullName'];
$_SESSION['gender'][] = $row['gender'];
$_SESSION['bloodgroup'][] = $row['bloodgroup'];
$_SESSION['State'][] = $row['State'];
For printing multiple records use foreach loop
foreach ( $_SESSION as $row ) {
echo "<td>" . $row['DonorID'] . "</td>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['bloodgroup'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
}
EDIT: The problem is due to you never place the session variable with array, hence your output from sql actually kept repeating overwrite inside the variable, that the reason why you able to acquire one result only.
What you can do is declare your session as array before store the data.
$_SESSION['DonorID'] = array();
$_SESSION['FullName'] = array();
$_SESSION['gender'] = array();
$_SESSION['bloodgroup'] = array();
$_SESSION['State'] = array();
Then retrieve your data using index. Make sure add your for new rows.
$counter=0;
$loopcounter=count($_SESSION['DonorID']);
while($counter < $loopcounter)
{
<tr><td><?php echo("{$_SESSION['DonorID'][$counter]}"."<br />");?></td>
.....</td></tr>
$counter++;
}
Reference: store mutiple values in php session
I'm quit new with mysql, and php but everytime i'm learning.
For a friend of my I'm building a website with a small pricing-list on it.
The prices are from a database, but I now want to create a empty line bewtween the 2nd and the 3th line, but I don't know how to do that.
I use the following code to receive the information from the database;
<table id="nails" class="prijzen">
<thead>
<tr>
<th>Nails</th>
<th>Price first visite</th>
<th>Regular price</th>
</tr>
</thead>
<?php
// while ($row = $queryNails->fetch_assoc()) {
$i = 0;
while ($row = $regularNails->fetch_assoc())
{
if($i%3 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
$i++;
?>
<tbody>
<?php
}
?>
</tbody>
</table>
<br>
I now have every odd-line a styling, but that is not something I like. I would like to create a sort of group of the output.
Hope someone can help me with that. If more information is needed, please let me know.
Please stop suggestion to enter a <br /> in between lines. That's not gonna work at all, tables-101: Everything not placed in a td will be placed in before the table. So all you get is a bunch of empty lines before the whole table.
The code is allready there (if you want it before every 3rd row:
if($i%3 == 0)
{
// The code right here will only run when $i / 3 is exactly 0 (3,6,9,etc)
echo '<tr><td colspan="3">Blank line</td></tr>';
echo "<tr class='even'>";
echo "<td>" . $row['name'] ."</td>";
echo "<td>" . $row['prijs'] ."</td>";
echo "<td>" . $row['actie_prijs'] ."</td>";
echo "</tr>";
}
If you only want it once before the 3rd row, make this the first code in your while:
if($i==3){ echo '<tr><td colspan="3">Blank line</td></tr>'; }
The % is modulus. It gives you how much is left after getting the max amount of whole divisions:
echo 1 %3 = 1;
echo 2 %3 = 2;
echo 3 %3 = 0; // exactly dividable by 3
echo 4 %3 = 1;
echo 5 %3 = 2;
echo 6 %3 = 0; // exactly dividable by 3
I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop
Im building a web site that will return results from mysql and display them on a web page.
I have no problem with storing the data in mysql or retrieving the data
and displaying it on the web page.
When the data is returned to the page I want to store it in a table and run a loop so that for each record there will be a number incremented down the side like a list number.
Any help would be appreciated.
I keep running into errors with my code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("forloops", $con);
$result = mysql_query("SELECT * FROM userinfo ORDER BY age DESC");
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
$row = mysql_fetch_array($result);
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
echo "</table>";
mysql_close($con);
?>
The problem with your current code is that you're only fetching the first row from the query, take a look at how I've down it below:
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
while ($row = mysql_fetch_array($result)) { // This is new code
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
} // This is new code
echo "</table>";
mysql_close($con);
?>
If you notice, I've created a loop around the echoing of the table's rows <tr> and cells <td>.
This while loop will go through each row returned from the query.
If you have more then one result from database, you should use:
while ($row = mysql_fetch_array($result)) {
//process one row
}
Using your way, $row is always the same.
And secondly, if i saw right, you dont increase $position variable.
I have looked extensively through the site and have come to the conclusion that I am encountering a problem that many people do, yet none of the answers I have seen seem to work!
Basically, I am trying to populate an HTML table from the data stored in a mySQL table. The data is in a table called "categories". When I load the page, the table headers appear but no table data.
I have written and rewritten my code no less than 4 times - it works fine as an "ul" or when rendered as just plain text, but as soon as I put it into a "table" to seems to stop working!
Here's my code:
<?php
include("includes/dbconnect.php"); //This works fine as tested on the page
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
$product = mysql_fetch_array($myData);
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php do {
echo $product['title']." <br />";
} while ($product = mysql_fetch_array($myData));
//-----------------------------------------
//Table - doesnt work
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
while ($product = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>".$product['title']."</td>";
echo "<td>" . $product['category'] . "</td>";
echo "<td>" . $product['sport'] . "</td>";
echo "<td>" . $product['team'] . "</td>";
echo "<td>" . $product['price'] . "</td>";
echo "<td>" . $product['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I really am at my wits end, Ive worked my way through countless YouTube tutorials and still for some reason the code wont work. Can anyone help?
You've already run mysql_fetch_array in a do while loop at the top of your code. You aren't able to pull a result row more than once. Instead, push all of the the returned rows on to an array:
$products = array();
while ($product = mysql_fetch_array($myData)) {
$products[] = $product;
}
You can then loop through $products as many times as you'd like to build out your page. So for your table, this would look like:
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
foreach($products as $item) {
echo "<tr>";
echo "<td>" . $item['title'] . "</td>";
echo "<td>" . $item['category'] . "</td>";
echo "<td>" . $item['sport'] . "</td>";
echo "<td>" . $item['team'] . "</td>";
echo "<td>" . $item['price'] . "</td>";
echo "<td>" . $item['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
if that code really looks like this remove the do-while loop and everything where you iterate over your $mydata and it will work since you waste the internal mysql-counter
meaning in the second loop your $result is already at the end, either you query again OR you push all the results in an array for multiple use
You have already pulled your data once with this.....$product = mysql_fetch_array($myData);
so just use this array with foreach loop....
foreach($product as $sProduct){
// User $sProduct to access that single product.
}
Your code contains many bugs and implementation
try this if it works for you :
<?php
include("includes/dbconnect.php");
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php
if(mysql_num_rows ($myData) > 0)
{
$html = "<table border=1><tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th></tr>";
while ($product = mysql_fetch_array($myData))
{
$html .= "<tr>";
$html .= "<td>".$product['title']."</td>";
$html .= "<td>".$product['category']."</td>";
$html .= "<td>".$product['sport']."</td>";
$html .= "<td>".$product['team']."</td>";
$html .= "<td>" . $product['shipping'] . "</td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
}
else
{
echo "No Product Found";
}
?>
</body>
</html>
If this works for you, you can figure out the problems or I will explain :)