came across a problem for my coursework when trying to place selected items from my database into a table, here is my code:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";"
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td>" "<option value=\"Location1\">" . $row['current_location'] . </option>" "</td></tr>";
}
echo "</table>";
Can anyone see where I am going wrong? The error mentions that I have no ',' or ';' tags, therefore it never ends, however I cannot find this error through messing with the program a bit.
Sorry for any formatting issues, still adjusting to the website.
There is an extra double-quotes at the end of the 6th line and then you should modify the while loop's echo like this:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td><option value='Location1'>" . $row['current_location'] . "</option></td></tr>";
}
echo "</table>";
Related
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
I am trying to display status of employee for each date in a month in the given application.
i have divided data in two tables.
Now, if you can see , My tables are displaying dates correctly.
But i want to start new table for each month. i.e. whenever 1st date of new month begins, i want to start the procedure of rendering dates for the month again.
So i will be able to distinguish them by their months.
I hope i have explained the problem correctly.
Please provide guidance on this .
Thanks in advance.
My code for the given problem :
<?php
$name=$_SESSION['sess_username'];
$dateofattendance=$_SESSION['sess_date'];
$time="00-00-00";
$status="absent";
$counter=0;
$conn = new mysqli('localhost', '', '', 'test');
$sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table class='main'><tbody><tr><td >";
// create the opening table
echo "<div align='left'><table class='sep1'style='float:left;'; border='black' cellpadding='5' ><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>tbody>";
while($row = $result->fetch_assoc())
{
// create the row
echo "<tr><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td>";
if($row["status"]=='present')
{
echo "<td ><span class='label label-success'>". $row["status"]."</span></td>";
}
else
{
echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
}"
</tr>";
$counter++;
// when the counter is 15, close this table and open another
if($counter == 15)
{
echo "</td><td>"; // move to the next cell in our wrap table
echo "</tbody></table></div> ";
echo "<table class='sep2'style='float:left;'border='black'cellpadding='5'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";
}
}
// close the last table
echo "</tbody></table>";
// close our wrapper table
echo "</td></tr></tbody></table>";
}
$conn->close();
?>
Instead of having a counter and creating a new table when it hits 15, you want to have a variable that stores the month number of the last row rendered, and check if the month number of the next row to be rendered matches. If it doesn't match, generate a new table.
The following code is produces the results you want. I've tested it with a database table with identical structure (table named "test_employees").
Note, the code outputs valid HTML and I re-formatted the echo statements and added newline chars to make the both the PHP and HTML source code more readable.
This code could be cleaned up considerably, so it's by no means good production code, but again, it demonstrates the logic you are after.
One item to note, the code only works within a given year. Additional code would be required if you wanted to display tables across multiple years.
If this helps you, please mark my answer.
I can also spend more time to refactor this code if you want. Let me know and I'll reply faster the next time.
<!DOCTYPE html>
<html>
<head>
<title>table test</title>
</head>
<body>
<?php
$name=$_SESSION['sess_username'];
$dateofattendance=$_SESSION['sess_date'];
$time="00-00-00";
$status="absent";
$counter=0;
$conn = new mysqli('localhost', '', '', 'test_employees');
$sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>IN</th>";
echo "<th>OUT</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$first_iteration = 1;
$row = $result->fetch_array(MYSQLI_ASSOC);
$last_month_num = split('-', $row["dateofattendance"])[1];
while($row = $result->fetch_assoc())
{
$current_month_num = split('-', $row["dateofattendance"])[1];
if ($first_iteration == 1) { // do nothing the first time around
$first_iteration = 0;
}
else if ($current_month_num == $last_month_num) { // only close row in current table
echo "</tr>";
}
else { // close table and start new table
echo "</tr>";
echo "</table>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>IN</th>";
echo "<th>OUT</th>";
echo "<th>Status</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
}
echo "<tr>" . "\n";
echo "<td>" . "\n" . $row["name"] . $row["dateofattendance"] . "\n" . "</td>" . "\n";
echo "<td>" . "\n" . $row["timeofattendance"] . "\n" . "</td>" . "\n";
echo "<td>" . "\n" . $row["timeofdeparture"] . "\n" . "</td>" . "\n";
if($row["status"]=='present')
{
echo "<td><span class='label label-success'>". $row["status"]."</span></td>";
}
else
{
echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
}
$counter++;
$last_month_num = split('-', $row["dateofattendance"])[1];
}
echo "</tr>";
echo "</table>";
}
$conn->close();
?>
</body>
</html>
I am trying to put extracted mysql data into a table using php. The issue is that I am able to put the fetched data into either all rows or all columns. What I would like to do is put the fetched data into a column format and as soon as 4 columns are used, the next data should start on the next row and so on.
Currently I am using the logic below, however I am getting everything in a column.
The goal is to limit the data to a page size so the data can be printed.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?></div>
If I understand your problem correctly you will want to do something like:
Keep a counter to see which number record you're looking at ($i)
$i = 0; // Your counter
while($row = mysql_fetch_array($result))
{
// ...
On every 4th iteration of the loop output something like </tr><tr>.
if ($i % 4 == 0 && $i > 0) // Will return true if `$i` is a multiple of 4 and greater than 0
{
echo "</tr><tr>"; // Output your HTML to start a new row
}
One final note: As others will be pointing out. You should avoid using the MySQL extension. You should use MySQLi or PDO instead.
<div>
$result = mysql_query("SELECT * FROM master_data");
echo "<table border='1'>
<tr>
<th>Accounts</th>
</tr>";
echo "<tr>";
$i = 1;
while($row = mysql_fetch_array($result))
{
if($i == 4)
{
echo "<td>";
echo "First Name:" . $row['First_Name'] . "</br>";
echo "Middle Name:" . $row['Middle_Name'] . "</br>";
echo "Last Name:" . $row['Last_Name'] . "</br>";
echo "</td>";
$i=0;
}
$i++;
}
echo "</tr>";
echo "</table>";
mysql_close($con);
?>
</div>
Hi I am having trouble getting this to store the value when submitted and then using it in my next query. The drop down is populated fine, but it is either not storing the value or the way I am using it dynamically in my query is wrong. Any help is appreciated. I know I am probably missing something obvious or small, I am new to php.
drop down box
$sth = $db->prepare("SELECT DISTINCT WEEK FROM Player_Points_2013");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sth->execute();
echo '<form method="POST" action=" " >';
echo '<select id="week" name="week"><OPTION>';
echo "Select a Week to see the stats</OPTION>";
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
{
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
}
echo '</SELECT>';
echo '<input type="submit" name="table_stats" value="Submit"/>';
echo '</form>';
$select_val = $_POST['week'];
//use value from drop down to display weeks stats
if(isset($_POST['table_stats'])){
$sql = $db->prepare("SELECT PName,CombinedPoints FROM `Player_Points_2013` WHERE
WEEK = '$select_val' ORDER BY CombinedPoints ");
//$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sql->execute();
Print "<table border cellpadding=10 width=500>";
Print "<tr><th>Player</th><th>Points</th></tr>";
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Print "</table>";
}
else {
echo $select_val;
}
?>
Use either $row or $info but not both, e.g.:
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
Print " <td>".$row['PName'] . "</td> ";
Print " <td>".$row['CombinedPoints'] . "</td> </tr>";
}
Also you have code:
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
What is $WEEK? Maybe it should be $row['WEEK'] instead?
So my objective was to print data from two into a standard table.
It should ideally display the name of the each distinct scholarship and its table with a list of all the students who applied for it. I m uncertain if my approach is correct.
Please help, i m trying to learn the basics. Thanks in advance.
<?php
include_once 'function.php';
connect();
?>
<html><body>
<?php
$query = "SELECT *
FROM entry, student_details
WHERE entry.user_id=student_details.user_id";
//run query
$result = mysql_query($query);
// creating a table
echo "<table border='1'>
<tr>
<th>Student ID</th>
<th>Student Name</th>
</tr>";
//Print the record that matches the criteria of the query
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row['s_name']
{
echo "<tr>";
echo "<td>" . $row['student_id'] . "</td>";
echo "<td>" . $row['student_name' ] . "</td>";
echo "</tr>";
}
}
?>
</table>
<?php close()
?>
</body></html>
the error i get is this Parse error: syntax error, unexpected 'echo' (T_ECHO) on line echo "<tr>";
Parse error messages don't necessarily originate on the actual line number stated, but many times one line above, being this:
echo $row['s_name']
-------------------^
// missing semi-colon
You forgot to put an ending semi-colon at the end.
Modify it to look like this:
echo $row['s_name'];
-------------------^
You have a syntax error.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row['s_name'];
echo "<tr>";
echo "<td>" . $row['student_id'] . "</td>";
echo "<td>" . $row['student_name' ] . "</td>";
echo "</tr>";
}