I need to echo all rows from SQL column.
Here is my PHP
$query2="SELECT * FROM field_activity WHERE field_id='$fetch'";
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2)){
$activity = $row['activity'];
$cr_date = date_create($row['date']);
$for_date = date_format($cr_date, 'F j, Y');
$amount = $row['amount'];
$acres_complete = $row['acres_complete'];
$duration = $row['duration'];
$status = $row['status'];
}
Here is my HTML output..
<?php
{
echo "<tr>";
echo "<td width='16%'><strong>Date</strong></td>";
echo "<td width='16%'>$for_date</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Activity</strong></td>";
echo "<td width='16%'>$activity</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Amount</strong></td>";
echo "<td width='16%'>$amount</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Acres Complete</strong></td>";
echo "<td width='16%'>$acres_complete</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Duration</strong></td>";
echo "<td width='16%'>$duration</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Status</strong></td>";
echo "<td width='16%'>$status</td>";
echo "</tr>";
}
?>
This only displays one of the rows (the latest row) from the column.. I want it to display all rows..
You need to actually do the echoing in the loop. If you move all of the echo statements to the end of the while loop, it will work exactly as you expect. You also need <table> and </table>.
Please embed your one row HTML echos into the while loop and everything will be done
OR
You can build your html string in the loop and echo your html string elsewhere.
You are overriding your variables in your while loop, so only the last value will be stored in your variables.
Try this:
$query2="SELECT * FROM field_activity WHERE field_id='$fetch'";
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2)){
$activity = $row['activity'];
$cr_date = date_create($row['date']);
$for_date = date_format($cr_date, 'F j, Y');
$amount = $row['amount'];
$acres_complete = $row['acres_complete'];
$duration = $row['duration'];
$status = $row['status'];
echo "<tr>";
echo "<td width='16%'><strong>Date</strong></td>";
echo "<td width='16%'>$for_date</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Activity</strong></td>";
echo "<td width='16%'>$activity</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Amount</strong></td>";
echo "<td width='16%'>$amount</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Acres Complete</strong></td>";
echo "<td width='16%'>$acres_complete</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Duration</strong></td>";
echo "<td width='16%'>$duration</td>";
echo "</tr><tr>";
echo "<td width='16%'><strong>Status</strong></td>";
echo "<td width='16%'>$status</td>";
echo "</tr>";
}
Additionally, this is not very good code.
Related
Following is the code
I want out put as
http://crysol.com/crysol_soft/Test/Screenshot_3.png
With following code I am getting output as
http://crysol.com/crysol_soft/Test/Screenshot_4.png
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
echo " </tr>";
}
echo "<td rowspan='10'>Good</td>";
?>
What are the changes required
Here is the code with your desired output
used if condition for print only 1 time good column and used rowspan='5'
<?php
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
?>
and also you can used this code
for($i=1;$i<=5;$i++){
echo '<tr>';
if($i==1){
echo "<td rowspan='5'>Cat</td>";
}
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
Create Table inside td:
<?php
echo "<table border='1'>";
echo "<tr><th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th></tr>";
echo "<tr><td style=''>Cat</td>";
echo "<td><table style='width:100%;'>";
for($i=1;$i<=5;$i++){
echo "<tr>";
echo "<td style='border-bottom:1pt solid black;'>".$i.'</td>';
echo "</tr>";
}
echo "</table></td>";
echo "<td>Good</td></tr>";
?>
I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you
I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance
I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}
Hello I have 2 tables.
One is a table (nameinfo) that consists of contact information (account, name, email, address, city, state, zip, tags)
Table Two is an events tables (events) (email, event_type, event_name, date, keywords)
Here is what I am trying to do...
I would like to show a list of all the events that the person went to based on the matching email address. I would like to return the results like this...
nameinfo person 1
events person 1 went to
nameinfo person 2
events person 2 went to
nameinfo person 3
events person 3 went to
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query))
{
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'
>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
**echo THIS IS WHERE I WOULD LIKE TO PUT THE EVENT INFORMATION BUT HAVE NO IDEA HOW TO DO IT.;**
echo "</div>";
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>
Thank you for any assistance.
Run another query inside the loop. Here I am calling a function to do the job:
$i=0;
$link = ""\\your database connection
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query)){
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
getEvent($row['email']);//Pass the email to the function here
echo "</div>";
}
function getEvent($email){
global $link;
$evant = "";
$query = mysql_query("SELECT * FROM events WHERE email='$email'", $link);
while($row = mysql_fetch_array($query)){
$event = $event . $row["event_name"] . "<br />";
//Format this however you wish to format
}
return $event;
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>