Please Help!! I just want to display the Barangay which is talojongon with no other duplicate values
Let's say my current output is:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| Talojongon | Cancer |
| Talojongon | Cancer |
and i want to turn this to:
| Barangay | Disease |
__________________________________
| Talojongon | Cancer |
| | Cancer |
| | Cancer |
The receive in the database is for disease and status in the database is the barangay...thank you...Any suggestions is appreciated ..:)
This is my current part of my code
<br>
<table id="keywords" class="table table-striped table-bordered">
<thead>
<tr>
<th><center>Barangay</th></center>
<th><center>Diseases</th></center>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
echo '<td><center>'. $row['status'] . '</center></td>';
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
</tr>
</tbody>
How about this?
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer" order by status';
$last = '';
foreach ($pdo->query($sql) as $row){
echo '<tr>';
if($last == $row['status'])
{
echo '<td></td>';
}
else
{
$last = $row['status'];
echo '<td><center>'. $row['status'] . '</center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM patients WHERE receive="Cancer"';
$counter=0;
foreach ($pdo->query($sql) as $row){
$counter++;
echo '<tr>';
if($counter==1){
echo '<td><center>Cancer</center></td>';
}else{
echo '<td><center></center></td>';
}
echo '<td><center>'. $row['receive'] . '</center></td>';
}
Database::disconnect();
?>
Simply echo the status just in the first row...
Save the last value of $row['status'] in a variable. If it's the same leave it blank in the output.
$last_status = null;
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
$status = $row['status'] == $last_status ? '' : $row['status'];
echo '<td style="text-align: center;">' . $status . '</td>';
echo '<td style="text-align: center;">' . $row['receive'] . '</td>';
echo '</tr>';
$last_status = $row['status'];
}
Also, you shouldn't use the obsolete <center> tag, use CSS. And </tr> should be inside the loop, not at the end.
You probably should also use ORDER BY status in the query, so that all the rows with the same status are together.
Related
I'm running a MySQL/PHP and i'm trying to display a simple report that tracks when a salesrep contacts a customer. I can't figure out what I'm doing wrong, as i'm a novice in this area. The simplest solution to me seems extremely convoluted (making a separate recordset for each figure). I figured there would have to be a simpler way.
I'm looking to display the number of contacts made during the current week/month/year in a simple table. see below. any help would be greatly appreciated.
|Current|Current| |
| Week | Month | YTD |
------|-------|-------|-------|
Brian | 7 | 14 | 37 |
------|-------|-------|-------|
Chad | 0 | 15 | 27 |
------|-------|-------|-------|
David | 11 | 26 | 52 |
------|-------|-------|-------|
Current recordsets
mysql_select_db($database_Sales, $Sales);
$query_rsCurWeek = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1) GROUP BY Sales.rep";
$rsCurWeek = mysql_query($query_rsCurWeek, $Sales) or die(mysql_error());
$row_rsCurWeek = mysql_fetch_assoc($rsCurWeek);
$totalRows_rsCurWeek = mysql_num_rows($rsCurWeek);
mysql_select_db($database_Sales, $Sales);
$query_rsCurMonth = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE MONTH(date) = MONTH(CURDATE()) GROUP BY Sales.rep";
$rsCurMonth = mysql_query($query_rsCurMonth, $Sales) or die(mysql_error());
$row_rsCurMonth = mysql_fetch_assoc($rsCurMonth);
$totalRows_rsCurMonth = mysql_num_rows($rsCurMonth);
mysql_select_db($database_Sales, $Sales);
$query_rsCurYear = "SELECT Sales.rep, COUNT(*) FROM Sales WHERE YEAR(date) =
YEAR(CURDATE()) GROUP BY Sales.rep";
$rsCurYear = mysql_query($query_rsCurYear, $Sales) or die(mysql_error());
$row_rsCurYear = mysql_fetch_assoc($rsCurYear);
$totalRows_rsCurYear = mysql_num_rows($rsCurYear);
Current Output Table
<table width="400" border="1" cellspacing="0" cellpadding="0">
<tr>
<th width="175" align="center"></th>
<th width="75" align="center">Current<br />Week</th>
<th width="75" align="center">Current<br />Month</th>
<th width="75" align="center">YTD</th>
</tr>
<?php do { ?>
<tr>
<th align="left"><?php echo $row_rsCurYear['rep']; ?></th>
<td align="center"><?php echo $row_rsCurWeek['COUNT(*)']; ?></td>
<td align="center"><?php echo $row_rsCurMonth['COUNT(*)']; ?></td>
<td align="center"><?php echo $row_rsCurYear['COUNT(*)']; ?></td>
</tr>
<?php } while ($row_rsCurWeek = mysql_fetch_assoc($rsCurWeek)); ?>
try{
$sql = "SELECT * FROM contacts";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
echo "<table>";
echo "<tr>";
echo "<th>username</th>";
echo "<th>current_week</th>";
echo "<th>current_month</th>";
echo "<th>ytd</th>";
echo "</tr>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['current_week'] . "</td>";
echo "<td>" . $row['current_month'] . "</td>";
echo "<td>" . $row['ytd'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
unset($result);
} else{
echo "No records matching your query were found.";
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
I try to write code that receive a student ID as an input in student_list.php, and pass this ID to score.php.
At score.php, use that ID to match and pull out student's name from database, and display it here.
Then, below the name, there is an input field, for adding 1 score to database for this student.
But I got this message, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1".
Any help would be appreciated. Thank you.
student_list.php
<?php
include_once 'DBconnect.php';
?>
<html>
<head>Student List</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> <th> Button </th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '<td> <button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button> </td>';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
score.php
<?php
include_once 'database_connect.php';
?>
<html>
<head>Add Score</head>
<body>
<?php
$student_id = $_POST["btn_student_id"];
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> <th>First Name</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . ' </td> ';
echo '<td>' . $row['student_fname'] . ' </td>';
echo '</tr>';
}
echo "</table>";
if(isset($_POST['btn_add_score'])) {
$score = $_POST['score'];
mysql_query ("UPDATE term3 SET score = score + 1 WHERE student_id = ' ".$_POST['btn_student_id']. " ' ");
}
?>
<form method="post">
<table>
<tr>
<td>Score</td>
<td> <input type="number" name="score" size="8">
<button type="submit" name="btn_add_score" >Add</button>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------
Student ID | Name | Select |
----------------------------------------
10001 | Pat | Button (submit) |
----------------------------------------
10002 | Jess | Button (submit) |
----------------------------------------
Try to remove the quotation marks around the student id in the update query. It is redundant
UPDATE term3 SET score = score + 1 WHERE student_id = ".$_POST['btn_student_id']);
Update
$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])
to
$result = mysql_query("SELECT * FROM term3 WHERE student_id='".$_POST['btn_student_id']."'")
Database schema looks like
ID Link_name Description
and wish The link name from the database to load in the table as
-----------------------------
Link_name1 | Link_name2 |
------------------------------
-----------------------------
Link_name3 | Link_name4 |
------------------------------
But the code
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error()); while($infoe = mysql_fetch_array( $datae )) {?>
<tr>
<td width="7%"> </td>
<td width="93%" class="main_text">
<h3><?php echo $infoe['Link_name']; }?><hr><br></h3>
</td>
</tr>
</table>
Output as
--------------
Link_name1 |
--------------
---------------
Link_name2 |
--------------
Not sure what are you asking for, but just a guess:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
require('config.php');
$datae = mysql_query("SELECT * FROM `products` ORDER BY id DESC")or die(mysql_error());
$leftRight = 'left';
while($infoe = mysql_fetch_array( $datae )) {
if ( $leftRight == 'left') {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$leftRight = 'right';
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$leftRight = 'left';
}
}
if ( $leftRight == 'right') echo '<td></td><td></td></tr>'; ?>
</table>
EDIT If you need to format N column table:
$current = 1;
$columnLimit = 3; //you can set any number of column to output
while($infoe = mysql_fetch_array( $datae )) {
if ( $current == 1) {
echo '<tr><td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
} elseif ( $current == $columnLimit) { {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td></tr>';
$current = 1;
} else {
echo '<td width="7%"> </td>';
echo '<td width="93%" class="main_text">';
echo '<h3>'.$infoe['Link_name'].'<hr><br></h3>';
echo '</td>';
$current++;
}
}
while ( $current!=1 && $current <= $columnLimit) {
if ($current != $columnLimit)
echo '<td></td>';
else
echo '<td></td></tr>';
$current++;
}
?>
Use the modulus operator (http://php.net/manual/de/language.operators.arithmetic.php)
With that you know if your current data entry is a odd or even number. so you can say something like: If your a even dataset then close the current row and open a new one.
How to make student id card in horizontal shape?
This code showing id card in vertical shape and I want in horizontal shape. How can I fix this issue? Please help me to fix this issue.
I want it like this:
----------------------------------
| Photo | Student Detail |
----------------------------------
here |Student Name : |
|Roll No: |
photo |Classs : |
|Grn : |
|Father Name : |
----------------------------------
and now its showing like this:
--------------------------------------------------------
Photo | Student Detail |
--------------------------------------------------------
here |
|
photo |
|
|
--------------------------------------------------------
Student Name: |
Roll No: |
Class: |
Grn: |
Father Name: |
--------------------------------------------------------
Code:
echo "<table class='hovertable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>Photo</th><th>Student Detail</th></tr>";
if ($rows > 0) {
while($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo "<img src=http://localhost/student/images/".$row['photo'] ." width='150' height='100'></a></td>";
echo "<tr><tr><td>";
echo $row['name'];
echo "</tr></td>";
echo "<tr><td>";
echo $row['rollno'];
echo "</tr></td>";
echo "<td>";
echo $row['fathername'];
echo "</tr></td>";
echo "<td>";
echo $row['class'];
echo "</td></tr>";
}
} else {
echo "<tr><td colspan=\"5\">No results found!</td></tr>";
}
echo "</table>";
The details are below the photo, because you start a new row before $row['name']. You cannot nest rows the way you do. If you want to have the details side by side with the photo, you must start with a <td> tag and then break the parts with either <br/>
echo '<td>';
echo $row['name'], '<br/>';
echo $row['rollno'], '<br/>';
echo $row['fathername'], '<br/>';
echo $row['class'], '<br/>';
echo '</td></tr>';
or <div> tags, as #Fabio already suggested in the comments.
RESOLVED
Works perfectly! Here is my final code:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
// Write rows
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<?php
foreach($row as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}
?>
<td><button id="edit_project_button(<?php echo $row['ID']; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row['ID']; ?>)">Edit</button></td>
</tr>
<?php } ?>
</tbody>
</table>
I wish to echo out a HTML table using mysql_fetch functions appropriately. I plan on making a thead to contain the mysql table column names and a tbody to contain the mysql table resultset. The SQL query selects a couple of columns from the table, with default limit set.
The issue: It doesn't seem to print the first row of table data, everything else displays (record #1 missing)
It displays the with column names echo'd within each , it then skips the first record and successfully echo's the 2nd row onward. For example:
| id | firstname | lastname | date_start | date_end | clientid | members | edit |
| 2 | Cal | Clark | 2012-12-12 | 2012-12-12 | 22 | Rob | (edit button) |
| 3 | Rob | Robin | 2012-12-12 | 2012-12-12 | 33 | Cal | (edit button) |
I'm 100% sure that the first record will display from my query in phpmyadmin.
Here is my code:
<table>
<thead>
<tr>
<?php
$row = mysql_fetch_assoc($result);
foreach ($row as $col => $value) {
echo "<th>";
echo $col;
echo "</th>";
}
?>
<th>Edit</th>
</tr>
</thead>
<?php
// Write rows
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
<td><?php echo $row[5]; ?></td>
<td><?php echo $row[6]; ?></td>
<td><button id="edit_project_button(<?php echo $row[0]; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row[0]; ?>)">Edit</button></td>
</tr>
<?php } ?>
</table>
I feel so oblivious right now =/
make a rewind of your data first!!
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...