For loop missing first row from mysql DB PHP - 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.

Related

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>";
?>

how to display two records in a table row out of multiple total records in php?

I have to display the SQL records (displayed in divs) in a table in which I can display only 2 records in one row. The total number of records i.e. n are not fixed and not an array too. The number of rows will depend on the number of records available. I tried couple of ways for instance defining "define('TABLE_COLS', 2);" and using for loop inside my while loop but they didn't give me the required result. The code I am working on is as following:
echo "<table>";
$sql = "Some SQL";
$result = mysql_query($sql);
$num = mysql_numrows($result);
$i = 0;
define('TABLE_COLS', 2);
echo "<tr>";
while ($i < $num) {
// for($i=0; $i<=2; $i++){
$name = mysql_result($result,$i,"name");
$Count = mysql_result($result,$i,"count");
// echo "<tr>";
echo "<td>";
echo "<div>";
echo "<div>";
echo "$name";
echo "</div>";
echo "<div>";
echo "$Count";
echo "</div>";
echo "</div>";
echo "</td>";
// echo "</tr>";
$i++;
}
echo "</tr>";
mysql_free_result($result);
echo "</table>";
The result I am looking for is like:
Now I have to add count values of corresponding event id and display it in a result table as following. I am very new in PHP so please help me. Any comments much appreciated.
+------------+ +--------------+
| Abby Bory | | Manny Mua |
+------------+ +--------------+
| 4 | | 22 |
+------------+ +--------------+
+------------+ +--------------+
| Senyz Dory| | Kory Bua |
+------------+ +--------------+
| 8 | | 27 |
+------------+ +--------------+
You can try if this help to solve your problem. I would declare multiple variables to make things easier to manage such as changing columns per row. The given code should adapt to any number of columns per row. Let me know if this is what you need.
Also, consider using mysqli instead of mysql.
Update 1: Include sample database format to standardize the format
database.php
<?php
$db_servername = "DATABASE SERVER NAME";
$db_username = "DATABASE USERNAME";
$db_password = "DATABASE PASSWORD";
$db_database = "DATABASE TABLE";
// Create connection
$con = new mysqli($db_servername, $db_username, $db_password, $db_database);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
Example Code
require "database.php";
global $con;
echo "<table>";
$sql = "SOME SQL";
$result = mysqli_query($con, $sql);
// define number of columns per row
$num_col = 2;
// calculate total number of row based on columns per row defined
$total_row = intVal(mysqli_num_rows($result) / $num_col);
// calculate left over result if the division result is not perfect number
$left_over = mysqli_num_rows($result) % $num_col;
$counter = 0;
while($counter < $total_row){
echo '<tr>';
for($i = 0; $i < $num_col; $i++){
$row = mysqli_fetch_array($result);
echo "<td>";
echo "<div>".$row["name"]."</div>";
echo "<div>".$row["count"]."</div>";
echo "</td>";
}
echo '</tr>';
$counter++;
}
echo "<tr>";
while($left_over > 0 ){
$row = mysqli_fetch_array($result);
echo "<td>";
echo "<div>".$row["name"]."</div>";
echo "<div>".$row["count"]."</div>";
echo "</td>";
$left_over--;
}
echo "</tr>";
echo "</table>";
This can give some ideas. definitely you must pay attention to the last record because in each iteration two records are read.
echo "<table>";
$sql = "Some SQL";
$result = mysql_query($sql);
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
$name1 = mysql_result($result,$i,"name");
$Count1 = mysql_result($result,$i,"count");
$name2 = mysql_result($result,$i+1,"name");
$Count2 = mysql_result($result,$i+1,"count");
echo "<tr>";
echo "<td>";
echo "$name1";
echo "</td>";
echo "<td>";
echo "$name2";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "$count1";
echo "</td>";
echo "<td>";
echo "$count2";
echo "</td>";
echo "</tr>";
$i+=2;
}
mysql_free_result($result);
echo "</table>";
The simple is to do this is make it on div's with bootstrap css.
Like
foreach($row = mysqli_fetch_array($result)){
echo '<div class="col-sm-3">'.$row['name'].'<br />'.$row['count'].'</div>';
}
as you see "col-sm-3" will divide one row into four columns.
and "col-sm-6" will divide row into two columns.
col-sm-12 will be the will have one row with one column.
Hope it helps.
Then try it,
echo "<table>";
$sql = "Some SQL";
$result = mysqli_query($sql);
$count = 0;
//No of columns per row
$noColumns = 2;
while($row = mysqli_fetch_array($result)){
if( $count % $noColumns == 0){
echo '<tr>';
}
echo '<td><div>'.$row['name'].'<br />'.$row['count'].'</div></td>';
$count++;
if( $count % $noColumns == 0){
echo '</tr>';
}
}
if( $count % $noColumns != 0){
//Close the last row
$remainCols = $noColumns - ($count % $noColumns);
echo '<td colspan="'.$remainCols.'"> </td>';
echo '</tr>';
}
echo '</table>';
mysqli_free_result($result);
it is based calculation with Mod(%) operator. Hope it solves.
I have used mysqli as reference instead of mysql.
This solution solved my problem.
$result = mysql_query($sql);
$num_col = 2;
$total_row = intVal(mysql_num_rows($result) / $num_col);
$i = 0;
while($i < $total_row){
echo '<tr>';
for($i = 0; $i < $num_col; $i++){
$row = mysql_fetch_array($result);
echo "<td>";
echo "<div>".$row["name"]."</div>";
echo "<div>".$row["count"]."</div>";
echo "</td>";
}
echo '</tr>';
$i++;
}
Thanks to all who helped me.
Hi this is the simplest answer i found, i went through several ways but failed, this is the right way
<table class="table" id="table">
<?php for($loop = 0; $loop <= 7; $loop++) : ?>
<td>
<input type="checkbox" name="brand[]" value="<?php echo $loop; ?>"> Brand <?php echo $loop; ?>
</td>
<?php endfor; ?>
</table>
dont use tr and in style do something like this
#table td {
width: 50%;
float: left;
}

How to store table Field(column) name into An Array?

This is the my code and I am trying to store the value into the Array.
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
echo $row['Field'];
echo "<br/>";
}
I am trying to do this $ar[]=$row; but there is nothing display !!
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
ar[] = $row;
echo "<br/>";
}
for($i = 0, $i<12, $i++) {
echo ar['field'][0];
}
Nothing Display in the output
You can do following way to store column name into array. Read comment after every line
For PDO
$rs = $cid->query('SELECT * FROM commissions');// your query
for ($i = 0; $i < $rs->field_count(); $i++) {/// count nu of column
$col = $rs->fetch_fields()($i);//Returns metadata for a column in a result set
$columns[] = $col['name'];// get name from metedata
}
print_r($columns);
For mysqli
$rs = $cid->query('SELECT * FROM commissions');// your query
$col = $rs->fetch_fields();//Returns metadata for a column in a result set
foreach ($col as $val) {
$columns[]=$val->name;
}
print_r($columns);

Displaying data horizontally using php and mysql

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.

how do I traverse rows in a table

I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}

Categories