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...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to print shift in horizontally and name/id in vertically. I have an attendance table like this =>>.
attendance table:
I want to print shift value in this table format html table structure:
<?php
$d=cal_days_in_month(CAL_GREGORIAN,date("n"),date("Y"));
$totdate = $d;
$month = date("F");
echo '<table class="table table-striped table-bordered" id="table_content">';
echo "<thead>";
echo "<tr>";
echo "<th>Sl/No</th>";
echo "<th>Emp.ID</th>";
echo "<th>Name</th>";
for ($i=1; $i <= $totdate; $i++) {
echo "<th>".$i."</th>";
}
echo "</tr>";
echo "</thead>";
$qry = "SELECT * FROM attendance WHERE `month` = '$month'";
$cnt=1;
$query = mysqli_query($conn, $qry);
foreach ($query as $q) {
$sid = $q['empid'];
echo "<tr>";
echo "<td>".$cnt++ ."</td>";
$st_name_id = "SELECT `emp_id`, `name` FROM `emp` WHERE `id`='$sid'";
$re = mysqli_query($conn, $st_name_id);
while ($result = mysqli_fetch_array($re)) {
echo "<td>".$result['emp_id']."</td>";
echo "<td>".$result['name']."</td>";
}
for ($i=1; $i <= $totdate ; $i++) {
$qry2 = "SELECT * FROM `attendance` WHERE `empid`='$sid' AND `month`='$month' AND `date`='$i'";
$re2 = mysqli_query($conn, $qry2);
echo "<td>";
foreach ($re2 as $value => $q) {
$dt = $q['date'];
$s = $q['shift'];
if ($i == $dt) {
echo $s;
echo "</td>";
}else{
echo "<td>x</td>";
}
}
}
echo "</tr>";
}
echo "</table>";
?>
$qry ="SELECT * FROM emp";
HI guys here is the answer. Thank you for support
<?php
$d=cal_days_in_month(CAL_GREGORIAN,date("n"),date("Y"));
$totdate = $d;
$month = date("F");
echo '<table class="table table-striped table-bordered" id="table_content">';
echo "<thead>";
echo "<tr>";
echo "<th>Sl/No</th>";
echo "<th>Emp.ID</th>";
echo "<th>Name</th>";
for ($i=1; $i <= $totdate; $i++) {
echo "<th>".$i."</th>";
}
echo "</tr>";
echo "</thead>";
// $qry = "SELECT * FROM attendance WHERE `month` = '$month'";
$qry ="SELECT * FROM `emp`";
$cnt=1;
$query = mysqli_query($conn, $qry);
foreach ($query as $q) {
$sid = $q['id'];
echo "<tr>";
echo "<td>".$cnt++ ."</td>";
$st_name_id = "SELECT `emp_id`, `name` FROM `emp` WHERE `id`='$sid'";
$re = mysqli_query($conn, $st_name_id);
while ($result = mysqli_fetch_array($re)) {
echo "<td>".$result['emp_id']."</td>";
echo "<td>".$result['name']."</td>";
}
for ($i=1; $i <= $totdate ; $i++) {
$ii = sprintf("%02d", $i);
$qry2 = "SELECT * FROM `attendance` WHERE `empid`='$sid' AND `month`='$month' AND `date`='$ii'";
$re2 = mysqli_query($conn, $qry2);
echo "<td>";
foreach ($re2 as $q) {
$dt = $q['date'];
$s = $q['shift'];
if ($dt == $ii) {
echo $s;
echo "</td>";
}else{
echo "x</td>";
}
}
// echo sprintf("%02d", $i) ."</td>";
}
echo "</tr>";
}
echo "</table>";
as accepted result
i need help.. im trying to echo all the data from my database but from the last to the first.
im a college student and i need ur help.
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) { // my lecture ask me to echo 5 data only
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
that is my code for now, and its only echoing from the first table to the last. my lecture told me i need to echo from the last.
edit :
i think i haven't give all the information.. sorry, this is the first time im here :(.
<?php
$i = 1;
include_once("function/helper.php");
include_once("function/koneksi.php");
$query = mysqli_query($koneksi, "SELECT * FROM transaksi ");
$pemilik = mysqli_fetch_assoc($query);
?>
<?php
$i = 1;
$sql = "SELECT mutasi, waktu_tanggal,tujuan FROM transaksi WHERE user_id='$user_id' ORDER BY waktu_tanggal";
$result = mysqli_query($koneksi, $sql);
if(mysqli_num_rows($result) == 0) {
?>
<h1>Anda belum melakukan transaksi apapun</h1>
<?php
}else {
echo "<br>";
echo "<table border='1'>";
?>
<tr>
<th>Mutasi</th>
<th>Waktu</th>
<th>Keterangan</th>
</tr>
<?php
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) {
echo "<tr>";
foreach ($row as $field => $value) { line like this: foreach($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
echo "</table>";
}
?>
that is the entire page.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
The sql [ORDER BY] command is used to sort the result set in ascending or descending order.
SELECT column1, column2 FROM table_name ORDER BY column1 DESC;
Try php function mysqli_fetch_all () --- >> you don't need a while-loop.
Then create a new array and populate it with the required number of records.
Like this:
$query = "SELECT TOP 5*FROM `MyTable`";
// Or alternative query: "SELECT*FROM `MyTable LIMIT 5`
$result = mysqli_query($connection, $query);
if($result != 0) {
$newDataArray = mysqli_fetch_all($result, MYSQLI_BOTH);
}
I'm pretty new at PHP/MySQL, so please be patient with me.
I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name];
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching]; <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name]; <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house]; <----This only show the first row of $house under same student, so you need to loop it too.
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
So what you really want to do is
<?php
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
while( $row = mysqli_fetch_array( $select ) ) {
$teaching = $row[teaching];
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
} else {
while( $row2 = mysql_fetch_array($select2) ) {
$student=$row2[student_name];
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
while($row3 = mysql_fetch_array($select3)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>";
}
} // END ELSE
}
} // END ELSE
?>
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.
My code below displays 10 entries, but it displays it in 10 rows and 1 column. I would like for it to display 10 entries, but in 2 columns and 5 rows. Can anyone help? Thanks in advance.
< ?php
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10")
or die(mysql_error());
echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No')
{
continue;
}
else{
echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
echo '<tr>';
}
}
echo '</table>'
?>
<?php
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") or die(mysql_error());
$i = 1; //The following logic only works if $i starts at '1'.
$numofcols = 2; //Represents the number of columns you want in the table.
echo '<table>'; //Open table.
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No'){
continue;
}
else{
//If it's the beginning of a row...
if( $i % $numofcols == 1 ){
echo '<tr>'; //Open row
}
//Table Cell.
echo '<td>'; //Open Cell
echo 'ID Number: '.$row['id'];
echo '<br/> <img src="'.$row['Pic'].'"> <br/>';
echo '</td>'; //Close Cell
//If we have already placed enough cells, close this row.
if( $i % $numofcols == 0) {
echo '</tr>'; //Close Row.
}
//Now that we've made a table-cell, lets increment our counter.
$i = $i + 1;
}
}
//So we make sure to close our rows if there are any orphaned cells
if( ($i % $numofcols) > 0){
echo '</tr>';
}
echo '</table>' //Close Table
?>
Please note that the mod logic will only work if the starting value of $i is '1'.
This code will create a table with 2 columns.
Not tested, but basically you can call $row = mysql_fetch_array() in the loop as well to grab another row.
while($row = mysql_fetch_array( $result ))
{
echo "<tr>";
echo "<td>";
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
echo "</td>";
$row = mysql_fetch_array( $result );
if($row)
{
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
}
else
{
// Empty cell
echo "<td> </td>";
}
echo "</tr>";
}
Use modulo for echo "< tr>"
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10")
or die(mysql_error());
$i = 0;
echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No')
{
continue;
}
else{
echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
if ($i % 5)
echo '</tr>';
$i++;
}
echo '</table>'