Displaying data horizontally using php and mysql - php

Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}

try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop

try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }

I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop

$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.

Related

For loop missing first row from mysql DB PHP

I want to add data from phpmyadmin into a table format, but this code is missing the first row in the table and I don't understand why, I have tried other examples on SO such as
for ($i=0; $i< count($num); $i++)
But this did not work either.
Can someone see the issue?
Thanks
<?php
include('connect.php');
$con=mysqli_connect("localhost","******","*******","********");
$display = mysqli_query($con,"SELECT * FROM markers");
$num = mysqli_num_rows ($display);
$col = mysqli_num_fields ($display);
$row = mysqli_fetch_assoc($display);
$name = $row['name'];
?>
<?php
// start for loop
for ($i=0; $i<$num; $i++){
$row = mysqli_fetch_row($display); // fetching data
//echo results to table
echo "<tr data-name='$row[1]' data-address='$row[2]' data-lat='$row[3]' data-long='$row[4]'>";
for ($j=0; $j <$col; $j++){ // looping through each row of table
$test = $row [$j];
echo "<td test='$test'>" . $row [$j] . "</td>";
}
echo "</tr>";
}
mysqli_close($conn); // closing the connection
?>
Why do it that way?
I'd do a fetch array and loop that.
$result = mysqli_query($con,"SELECT * FROM markers");
while($row = mysqli_fetch_array($result))
{
echo "<tr data-name='$row['dataName']' data-address='$row['Address']' data-lat='$row['LAT']' data-long='$row['LONG']'>"
}
This would loop til you didn't have any more rows.

PHP While loop not working but no errors

Why my while loop not working, I also have a while loop on the other PHP page, but there's only one page that doesn't work with PHP's while loop. But it does not contain any errors. Here's my code:
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
if($num <= 0){
echo "<h2>No records found.</h2>";
}
$x=0;
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
You were reading your first result row and incorrectly using that as a count of resulted rows, then ignoring its content.
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
// this reads the first row of your result set and then of course gets lost
//$num = mysqli_fetch_array($result);
// use mysqli_num_rows instead
if(mysqli_num_rows($result) <= 0){
echo "<h2>No records found.</h2>";
} else {
$x=0;
// now this will get the first row, which you must have been missing before
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$x=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
else {
echo "<h2>No records found.</h2>";
}

Create dynamic table in php with mysql data

I would to like to create a dynamic table that the rows and columns of the table depends on the data in mysql data. Please see below my codes.
<?php
require "connect/db.php";
$query = mysqli_query($mysqli, "SELECT COUNT(level) level, shelf_no, bin_id FROM location_bin WHERE rack_id =1 GROUP BY shelf_no");
while($res=mysqli_fetch_array($query)){
$col = $res['level']; //col
$row = $res['shelf_no']; //rows
echo "<table border='1'>";
$i = 0;
while ($i < $col){
if ($i==$col){
echo "<tr>";
}
echo "<td>".$res['bin_id']."</td>";
$i++;
}
echo "</tr>";
echo "</table>";
}
?>
What I want is to display A-1-01 up to A-1-06 in the first row then A-2-01 to A-2-03 on the second row. Note that the data is dynamic.
Can you try this code . It works. change db.php path as your system path.
require "db.php";
$query = mysqli_query($conn, 'SELECT level, shelf_no, bin_id FROM location_bin ORDER BY shelf_no asc, level asc ');
echo "<table border='1'>";
echo "<tr>";
$i=0;
while($res = mysqli_fetch_assoc($query)) {
$row=$res['shelf_no'];
if ($i < $row){
$i=$row;
echo "</tr><tr>";
}
echo "<td>".$res['bin_id']."</td>";
}
echo "</table>";
?>

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

mysql_fetch_array dropping first entry

I am trying to display a list of links based upon mysql output. The code works except that it drops the first record. I know it is because of the duplicate [ #row3 = mysql_fetch_array($result3) ] entries but if i remove one the code fails. Can anyone suggest a fix?
Thanks
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = 3";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
while ($row3 = mysql_fetch_array($result3))
{
echo "<table>";
for ($i = 0; $i < $num; $i++){
$row3 = mysql_fetch_array($result3);
//var_dump($row3);
$ridesid = $row3[0];
$rides = $row3[1];
echo "<tr>";
echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
}
?>
You have fetch same thing twice!
Try this:
<?php
$sql3 = "SELECT `Record_ID`, `Name` FROM `rides` WHERE `Rating` = 3";
$result3=mysql_query($sql3)or die(mysql_error());
//var_dump ($result3);
$num = mysql_num_rows($result3);
echo "<table>";
while ($row3 = mysql_fetch_array($result3))
{
$ridesid = $row3[0];
$rides = $row3[1];
echo "<tr>";
echo "<a href='attraction_page.php?rideID=". urlencode($ridesid) ."'>$rides</a>";
echo "<br />";
echo "</tr>";
}
echo '</table>';
?>
Call mysql_fetch_array() only once...

Categories