while loop data doesnt print on screen - PHP - php

Does anyone see an error in this? I dont get the results printed which is expected from the second loop. How ever the second loop headers are printed without an issue.
I'm trying to print data from a SQL database.
<?php
$con= new mysqli('localhost','root','','regional_data');
if (mysqli_connect_errno()) {exit('Connection failed: '. mysqli_connect_error());}
$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
echo "<table cellpadding='2' class='tablet' cellspacing='0'>";
echo
"<tr>
<th></th>"
."<th>"."Starting Cheque No"."</th>"
."<th>"."Ending Cheque No"."</th>"
."<th>"."Total No of Cheques remaining"."</th>"
."<th>"."Cheque Type"."</th>"
."</tr>";
while ($reca = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' ></td>";
echo "<td>".trim($reca["sbstart"])."</td>";
echo "<td>".trim($reca["sbend"])."</td>";
echo "<td>".trim($reca["totsb"])."</td>";
echo "<td>SB</td>";
echo "</tr>";
}
echo "</table>";
echo "<table cellpadding='2' class='tablet' cellspacing='0'>";
echo
"<tr>
<th></th>"
."<th>"."Starting Cheque No"."</th>"
."<th>"."Ending Cheque No"."</th>"
."<th>"."Total No of Cheques remaining"."</th>"
."<th>"."Cheque Type"."</th>"
."</tr>";
while ($reca = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' ></td>";
echo "<td>".trim($reca["gwstart"])."</td>";
echo "<td>".trim($reca["gwend"])."</td>";
echo "<td>".trim($reca["totgw"])."</td>";
echo "<td>GW</td>";
echo "</tr>";
}
echo "</table>";
$con->close();
?>
</div>

while ($reca = mysqli_fetch_array($result))
This fetches all results from the result set. After that the result set is exhausted, which is why the loop ever ends. There are no more results to be fetched from the same result set afterwards.
Either issue a new query, or save the data into an array which you can loop over again as many times as you want.

I don't think you can use the same $result variable twice.
What I would do is the following:
$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
$result2 = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
Then your first while loop can use mysqli_fetch_array($result) and the second one can use mysqli_fetch_array($result2).
Hope this helps!

Related

Dynamic sort by header in PHP

I'm trying to create a dynamic header on Name which gives the user the ability to sort by ASC or DESC, the results is from the 'register'-table in MySQL. I have tried a couple of codings, but it hasn't given the correct result as of now. Hope anyone is able to help me :)
I have tried making another variable, but I couldn't manage to create the correct one.
<?php
$sql = "SELECT * FROM register";
if($sqlData = mysqli_query($db, $sql)) {
if(mysqli_num_rows($sqlData) > 0) {
echo "<table border ='1' bgcolor='#FFF' width='100%'>";
echo "<tr>";
echo "<th><a href='overview.php?order=name'>Name</a></th>";
echo "<th>Score</th>";
echo "</tr>";
while($row = mysqli_fetch_array($sqlData)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($sqlData);
} else{
echo "No results in DB";
}
} else{
echo "Error couldn't connect $sql. " . mysqli_error($db);
}
mysqli_close($db);
?>
Hm, that's not a good idea to sort table using MySQL each time user clicked on the table's header.
I suggest you to use javascript for it. Example

Displaying Multiple Rows with two tables

I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;

while mysql_fetch_array loop doesn't run

Here is my code
$itemSelect="select * from items where gender='men' and type='suite'";
$itemQuery=mysql_query($itemSelect) or die(mysql_error());
$num=count($itemQuery);
echo $num;
if($itemQuery)
{
echo "I am here";
echo "<table>";
while($row = mysql_fetch_array($itemQuery))
{
$photo=$row['photo'];
$name=$row['name'];
$price=$row['price'];
echo "<tr><td>". $photo ."</td><td>" .$name ."</td><td>" .$price ."</td> </tr>";
}
echo "</table>";
}
else
{
echo "There is some mistacke";
die();
}
So here $num=count($itemQuery); shows me that there is 1 item that satisfies the search but the loop is never executed what could be the problem? Thanks in advance.
The reason you are getting 1 is because of this code:
$num=count($itemQuery);
echo $num;
count is a function to get the number of items in an array. If you pass anything that isn't an array (or doesn't implement the ICountable interface), then count returns always 1.
So this piece of code is incorrect in that it shows 1, while that number isn't at all related to the number of rows returned.
The query itself looks fine to me, although I cannot validate it against your actual database. If you see the message "I am here", then your query executed fine, but didn't return any rows. If you don't see that message, then something else went wrong. You can use mysql_error to try to find any errors in that case.
$count= mysql_num_rows($itemQuery);
echo $count;
if($count > 0)){
echo "<table>";
while($row = mysql_fetch_array($itemQuery)){
$photo=$row['photo'];
$name=$row['name'];
$price=$row['price'];
echo "<tr><td>". $photo ."</td><td>" .$name ."</td><td>" .$price ."</td> </tr>";
}
echo "</table>";
}else{
echo "there are no results";
die();
}

Printing two separate SQL table data on the dynamically populated table - PHP

Can someone please help me on the following code? Simply put, I'm trying to get two separate SQL tables' data, one on the horizontal side (brands) and the other (distributors) on the vertical side of a dynamically populated table.
my issue is, if you go through the code I cant get the text boxes populated under each respective brand name I get to display from the database. The text boxes are appearing only for the first brand name column.
My second issue if how do I assign a unique ID or a name to a dynamically populated text box here?
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$sqlq="SELECT * FROM brands";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");
$resultq = mysqli_query($db,$sqlq) or die ("SQL Error_er2");
echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";
while($rowq = mysqli_fetch_array($resultq))
{
echo "<td>" . $rowq['bname'] . "</td>";
}
"</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['dname'] . "</td>";
echo "<td><input type='text' name='txt1'></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db);
?>
You're creating loop without relating to eachother, and the same goes for the execution of the querys...
If you want to solve it you will have to nestle the loops together, something like:
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");
echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";
//Go through all distributors
while($rowq = mysqli_fetch_array($result)) {
echo "<td>" . $rowq['bname'] . "</td>";
//Show all brands for current distributor
$sqlq="SELECT * FROM brands where distributor_id = " . $rowq['rsm'];
$resultBrands = mysqli_query($db,$sql) or die ("SQL Error Brands");
while($row = mysqli_fetch_array($resultBrands))
{
$id = $row['rsm'];
echo "<tr>";
echo "<td>" . $row['dname'] . "</td>";
echo "<td><input type='text' name='textBox[]'></td>";
echo "</tr>";
}
//End show all brands for current distributor
}
//End Go through all distributors
A better solution though, would be something like (of course $q has to validated before input inside of query and also binded with bind_param()).
<?php
$q=$_GET["q"];
include ("../connection/index.php");
$sql = " SELECT * FROM distributors d";
$sql .=" LEFT JOIN brands b ON (d.brand_id = b.brand_id)";
$sql .=" WHERE d.rsm=$q";
$result = mysqli_query($db,$sql) or die ("SQL Error");
echo "<table border='1'>";
while($rowq = mysqli_fetch_array($result))
{
$id = rowq['rsm'];
echo "<tr>";
echo "<td>" . $rowq['dname'] . "</td>";
echo "<td>" . $rowq['bname'] . "</td>";
echo "<td><input type='text' name='textBox[]'></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($db);
?>
Take notice of the name='textBox[]'. From PHP you can access the variable with $_POST['textBox'] (or $_GET['textBox'] and PHP will return an array).

PHP outputs the wrongly formatted html table

I'm having trouble echoing data into a HTML table.
It comes out like that:
But it should be:
Here's the code. What am I doing wrong?
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</table>";
}
}
else{
echo "No results";
}
}
?>
The problem is that you keep outputting a new table for each iteration.
Your code should look like this:
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table>"; // Start the table
// Output the table headers
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>"; // End the table
}
else{
echo "No results";
}
}
?>
just put echo "<table>"; and first tr creation statment out side of while loop and also put closing of table after finishing of while loop and see i am sure it will work.
Try
<?php
echo "<table><tr><td>Model name</td><td>Year</td></tr>";
while($results = mysql_fetch_array($raw_results))
{
echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>";
}
echo "</table>";
?>
use this code, you have to first start the table, use while loop to iterate the result, and then close the table.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
Remove table code from while loop and put outside.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
replace your if block, use the following code , hope it may help you
if(mysql_num_rows($raw_results) > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}echo "</table>";
}

Categories