page1, the code, below, works fine and lists recipe names i.e. BBQ Pork etc
<form action="recipe_show.php" method="post">
<?php
$result = mysql_query("SELECT recipe_name FROM recipes ORDER BY recipe_name ASC"); // Run the query
if ($result) { // If it ran OK, display the records
// Table header
echo '<table>
<td align="left"><b>Recipe Name</b></td>
</tr>';
// Fetch and print all the records
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td>'. $row['recipe_name'] .'</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($result); // Free up the resources
} else { // If it did not run OK
// Message
echo '<p class="error">The current recipe_name could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} // End of if ($result)
mysql_close($dbcon); // Close the database connection.
?></p>
</form>
</div>
When I click the link, from page1, it opens page2. I get the headings but not the data for the BBQ Pork link. Below is the code on page2.
<form action="" method="post">
<?php
$recipe = mysql_real_escape_string($_GET['recipe_name']); //if using mysql
$myresult = "SELECT * FROM recipes WHERE recipe_name = '".$recipe. "'";
$num = mysql_num_rows($myresult);
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($myresult, MYSQL_ASSOC);
$row = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
echo "<tr row=".$row.">";
}
if ($myresult) { // If it ran OK, display the records
echo '<table>
<td width="250" align="center"><b>Recipe Name</b></td>
<td width="250" align="left"><b>Instructions</b></td>
<td width="250" align="left"><b>Directions</b></td>
<td width="250" align="left"><b>Notes</b></td>
</tr>';
while ($row = mysql_fetch_array($myresult, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['recipe_name'] . '</td>
<td align="left">' . $row['ingredients'] . '</td>
<td align="left">' . $row['directions'] . '</td>
<td align="left">' . $row['notes'] . '</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($myresult); // Free up the resources
} else {
echo '<p row="error">The current Recipe could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} ($myresult)
mysql_close($dbcon); // Close the database connection.
?>
Hope I have correctly set this out? Many thanks, in advance, for help and guidence
Related
i have the below code written, actually i thought of converting the current webpage to pdf document, but i couldnt make how to declare the current url and get the contents of the webpage in pdf doc. below is my code.
<?php
require("mpdf60/mpdf.php");
$mpdf=new mPDF('utf-8','Letter-L','','',15,10,16,10,10,10);//A4 page in portrait for landscape add -L.
$mpdf->SetHeader('|Your Header here|');
$mpdf->setFooter('{PAGENO}');// Giving page number to your footer.
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetDisplayMode('fullpage');
$mpdf->debug = true;
// Buffer the following html with PHP so we can store it to a variable later
ob_start();
?>
<?php
include "contractview.php?ID=129";
//This is your php page ?>
<?php
$html = ob_get_contents();
ob_end_clean();
// send the captured HTML from the output buffer to the mPDF class for processing
$mpdf->WriteHTML($html);
//$mpdf->SetProtection(array(), 'user', 'password'); uncomment to protect your pdf page with password.
$mpdf->Output();
exit;
?>
i have written include "contractview.php?ID=129"; but this was not working, can any one please help me in how to assign this to get the current page contents.
MPDF doesn't support includes. You have to put it in a var. The full html can be converted using jquery or in php with file_get_contents.
This is how i did it with full page.
<?php
require("mpdf60/mpdf.php");
$mpdf=new mPDF('utf-8','Letter-L','','',15,10,16,10,10,10);//A4 page in portrait for landscape add -L.
$mpdf->SetHeader('|Your Header here|');
$mpdf->setFooter('{PAGENO}');// Giving page number to your footer.
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetDisplayMode('fullpage');
$mpdf->debug = true;
// Buffer the following html with PHP so we can store it to a variable later
ob_start();
?>
<?php
//$content = file_get_contents("contractview.php?ID=129"); //<=== WRONG ONE
$content = file_get_contents("http://www.yourwebsite.com/contractview.php?ID=129"); //<=== getting content from url and not file!!
print($content);
//This is your php page ?>
<?php
$html = ob_get_contents($content); //<=== put content in ob
ob_end_clean();
// send the captured HTML from the output buffer to the mPDF class for processing
$mpdf->WriteHTML($html);
//$mpdf->SetProtection(array(), 'user', 'password'); uncomment to protect your pdf page with password.
$mpdf->Output();
exit;
?>
I see you are using ob_start and ob_end
You coudl also use
<?php
require("mpdf60/mpdf.php");
$mpdf=new mPDF('utf-8','Letter-L','','',15,10,16,10,10,10);//A4 page in portrait for landscape add -L.
$mpdf->SetHeader('|Your Header here|');
$mpdf->setFooter('{PAGENO}');// Giving page number to your footer.
$mpdf->useOnlyCoreFonts = true; // false is default
$mpdf->SetDisplayMode('fullpage');
$mpdf->debug = true;
// Buffer the following html with PHP so we can store it to a variable later
$content = file_get_contents("http://www.yourwebsite.com/contractview.php?ID=129"); //<=== getting content from url and not file!!
// send the captured HTML from the output buffer to the mPDF class for processing
$mpdf->WriteHTML($content);
//$mpdf->SetProtection(array(), 'user', 'password'); uncomment to protect your pdf page with password.
$mpdf->Output();
exit;
?>
<?php require_once ('header.php'); ?>
<!-- 10/15/2014 START: Freze table header row; Updated Fancybox so it works with jQuery 1.10.1 as well -->
<META content="IE=11; IE=10; IE=9; IE=8; IE=7; IE=EDGE" http-equiv="X-UA-Compatible">
<!--script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script duplicate 10/6/2015-->
<SCRIPT type="text/javascript" src="jQueryfloatThead/jquery.floatThead.js"></SCRIPT>
<SCRIPT type="text/javascript" src="jQueryfloatThead/site.js"></SCRIPT>
<SCRIPT type="text/javascript" src="jQueryfloatThead/bootstrap.js"></SCRIPT>
<SCRIPT type="text/javascript" src="jQueryfloatThead/less.js"></SCRIPT>
<!-- 10/15/2014 END -->
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border=0 cellpadding="0" cellspacing="0">
<tr><td height="36" background="images/topbar_bg.png" style="background-repeat:repeat-x;"><img src="images/topbar_bg.png" height="36"></td></tr>
<tr><td>
<table width="960" border=0 cellpadding="0" cellspacing="0" align="center">
<?php require_once ('topnav.php'); ?>
<tr><td>
<table width="100%" border=0 cellpadding="10" cellspacing="5">
<?php
if(!isset($_SESSION['Username'])) {
header('Location: home?errMsg=Your session has been expired. Please re-login again.');
}
?>
<tr><td bgcolor="#006ec7" valign="top" width="100%" class="headertd">FAR Compliance Portal</td></tr>
<tr><td bgcolor="#ebf4f9" valign="top" width="100%" height="300">
<?php
require_once ('dbconnection.php');
require_once ('contractcheck.php'); //Check if users can view this section
if (!isset($_REQUEST['ID'])) {
print "ERROR: No record has been selected.";
exit;
}
if (!is_numeric($_REQUEST['ID'])) {
print "ERROR: Invalid record has been selected.";
exit;
}
$thisID = $_REQUEST['ID'];
$sqlGet = "select * from far_contract where ID='" .$thisID. "'";
$resultGet = mysql_query($sqlGet, $dbconnection);
if (!$resultGet) {
echo 'Could not run query [1] ($thisID): ' . mysql_error();
exit;
}
$num_rows = mysql_num_rows($resultGet);
if ($num_rows==0 || $num_rows>1) {
print "Error: Invalid record is being called ($thisID). Please check with administrator.";
exit;
}
$row = mysql_fetch_array($resultGet);
$thisID = $row[0];
$thisContractNumber = $row[1];
$thisCustomer = $row[2];
$thisCeilingValue = $row[3];
//$thisEffectiveDate = $row[4];
//$thisExpirationDate = $row[5];
//$thisLegalEntity = $row[6];
$thisProjectName = $row[4];
$thisMFilesLink = $row[5];
$thisCreatedByID = $row[6];
$thisCreatedDate = $row[7];
$thisModifiedByID = $row[8];
$thisModifiedDate = $row[9];
$thisContractStatus = $row[10]; //12/22/2014
$thisNotes = $row[11]; //3/27/2015
$thisSubmittedByID = $row[17]; //10/1/2015
$thisSubmittedDate = $row[18]; //10/1/2015
$thisMasterAgreement = $row[19]; //10/6/2015
if ($thisContractStatus==NULL) $thisContractStatus="Not Submitted"; //12/22/2014
$thisSpecialStatusNotes = $row[15]; //4/15/2015
unset($resultGet);
//if ($thisEffectiveDate!="") $thisEffectiveDate = date("m/d/Y", strtotime($thisEffectiveDate));
//if ($thisExpirationDate!="") $thisExpirationDate = date("m/d/Y", strtotime($thisExpirationDate));
$sqlGetFAR = "select * from far_contract_clauses where ContractID='" .$thisID. "'";
$resultGet = mysql_query($sqlGetFAR, $dbconnection);
if (!$resultGet) {
echo 'Could not run query [2] ($thisID): ' . mysql_error();
exit;
}
$num_rows = mysql_num_rows($resultGet);
if ($num_rows>0) {
$thisSelectClause = "";
while ($row = mysql_fetch_array($resultGet)) {
if ($thisSelectClause=="")
$thisSelectClause = $row[2];
else
$thisSelectClause .= ",".$row[2];
}
}
unset($resultGet);
?>
<h1>Contract Records > View Record</h1>
<a href='php-to-pdf.php' target='_blank' ><input type='button' value='Convert to pdf'></a>
<table cellspacing=0 cellpadding=3 border=0 width="100%" class="table-bordered">
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>ID:</b></td>
<td class="resultTbl"><?php echo $thisID ?></td>
</tr>
<tr>
<td nowrap width="220" class="resultTbl" bgcolor='#d9e0e4'><b>Direct Customer's Solicitation/Contract Number:</b></td>
<td width="100%" class="resultTbl"><?php echo $thisContractNumber ?></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Direct Customer:</b></td>
<td class="resultTbl"><?php echo $thisCustomer ?></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Ceiling Value ($):</b></td>
<td class="resultTbl"><?php is_numeric($thisCeilingValue)? print "$ ".number_format($thisCeilingValue,2) : print ""; ?></td>
</tr>
<!--<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Effective Date:</b></td>
<td class="resultTbl"><//php echo $thisEffectiveDate //></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Expiration Date:</b></td>
<td class="resultTbl"><//php echo $thisExpirationDate //></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Rapiscan Legal Entity:</b></td>
<td class="resultTbl"><//php echo $thisLegalEntity //></td>
</tr>-->
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Project Name/Description:</b></td><?php //3/27/2015 ?>
<td class="resultTbl"><?php echo $thisProjectName ?></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>M-Files Link:</b></td>
<td class="resultTbl"><?php echo $thisMFilesLink ?></td>
</tr>
<tr><?php //3/27/2015 ?>
<td class="resultTbl" bgcolor='#d9e0e4' valign='top'><b>Notes:</b></td>
<td class="resultTbl"><?php echo $thisNotes ?></td>
</tr>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Created By:</b></td>
<td class="resultTbl"><?php echo fetchFullName($thisCreatedByID) . " on " . $thisCreatedDate ?></td>
</tr>
<?php
$SubmissionDate = $thisCreatedDate; //1/6/2015
if ($thisModifiedByID!="") { ?>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Last Modified By:</b></td>
<td class="resultTbl"><?php echo fetchFullName($thisModifiedByID) . " on " . $thisModifiedDate ?></td>
</tr>
<?php
$SubmissionDate = $thisModifiedDate; //1/6/2015
}
?>
<?php
$SubmittedDate = $thisSubmittedDate; //10/1/2015 Added Submitted By and Submitted Date
if ($thisSubmittedByID!="") { ?>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Submitted By:</b></td>
<td class="resultTbl"><?php echo fetchFullName($thisSubmittedByID) . " on " . $thisSubmittedDate ?></td>
</tr>
<?php
$SubmittedDate = $thisSubmittedDate; //10/1/2015
}
?>
<?php
$MasterAgreement = $thisMasterAgreement; //10/1/2015 Added Submitted By and Submitted Date
?>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Master/Framework<b>Agreement Contract</b></td>
<td class="resultTbl"><?php echo ($thisMasterAgreement==1)?'Yes':'No'; ?></td>
</tr>
<?php
?>
<tr>
<td class="resultTbl" bgcolor='#d9e0e4'><b>Status:</b></td>
<td class="resultTbl"><?php echo $thisContractStatus //12/22/2014 ?>
<?php //4/15/2015
if ($AllowContractFull) {
if ($thisContractStatus=="Submitted") {
print " ";
print "[Suspend]";
print " [Cancel]";
} elseif ($thisContractStatus=="Suspended") {
print " ";
print "[Re-activate]";
print " [Cancel]";
}
}
if (! ($thisSpecialStatusNotes=="" || is_null($thisSpecialStatusNotes)) ) {
print "<BR><font color=gray>$thisSpecialStatusNotes</font>";
}
?>
</td>
</tr>
<tr><td valign=top colspan=2 class="resultTbl"><b>FAR Clauses:</b></td></tr>
<tr><td valign=top colspan=2 class="resultTbl">
<?php
//get list of departments
$sql = "select * from far_departments order by Department";
$result = mysql_query($sql, $dbconnection);
if (!$result) {
echo 'Could not run query [3]: ' . mysql_error();
exit;
}
$deptList = array();
while ($row = mysql_fetch_array($result)) { //loop each department
$deptList[] = $row[1];
}
unset ($result);
printByCompliance ($thisID, $thisSelectClause, $thisContractStatus, $SubmissionDate); //1/6/2015
?>
</td>
</tr>
</table>
<?php
require_once ('logging.php'); //7/31/2014
mysql_close($dbconnection);
?>
</td></tr>
</table>
</td></tr>
<?php require_once ('footer.php'); ?>
</table>
</td></tr>
</table>
</body>
</html>
<?php
function printByCompliance ($thisID, $thisSelectClause, $thisContractStatus, $SubmissionDate){ //1/6/2015
global $deptList;
global $dbconnection;
//print table header
//echo "<h1>Compliance Report</h1>";
echo "<DIV id='options'><table width='100%' class='table table-bordered table-striped' cellspacing=0 cellpadding=5>";
echo "<thead><tr bgcolor='#d9e0e4'>";
echo "<th class='resultTbl' nowrap><b>FAR Clauses</b><img src='images/spacer.gif' width='80' height='1'></th>"; //9/15/2014
if ($thisContractStatus=="Submitted" || $thisContractStatus=="Suspended" || $thisContractStatus=="Cancelled") //4/15/2015
echo "<th class='resultTbl' nowrap><font color=blue><b>Certified<BR>as of<BR>".str_replace(" ", "<BR>", $SubmissionDate)."?</b></font></th>"; //1/6/2015
else
echo "<th class='resultTbl' nowrap><font color=blue><b>Certified?</b></font></th>"; //9/15/2014
foreach ($deptList as $EachDept) { //loop each dept
echo "<th class='resultTbl' align=center><b>" . $EachDept . "</b></th>";
}
echo "</tr></thead>"; //10/15/2014 END
//get all questions
$sql = "select * from far_questions where QStatus='ACTIVE' and ID in ($thisSelectClause) order by Title, Alternate"; //2/9/2015
$result = mysql_query($sql, $dbconnection);
if (!$result) {
echo 'Could not run query [4]: ' . mysql_error();
exit;
}
$listQuestionArray = array(); //START natural-order sorting questions by title
while ($row = mysql_fetch_array($result)) {
$listQuestionArray[] = array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7]); //2/9/2015; 10/10/2014; 6/17/2014
}
usort($listQuestionArray, function ($elem1, $elem2) {
return strnatcmp ($elem1['1']." ".$elem1['7'], $elem2['1']." ".$elem2['7']);
//2/9/2015 need to take Alternate in nartural sort*** return strnatcmp ($elem1['1'], $elem2['1']);
});
unset($row); //END natural-order sorting questions by title
foreach ($listQuestionArray as $row) { //loop each question
$thisQuestionID = $row[0];
$thisTitle = trim($row[1]);
$thisURL = trim($row[2]);
$thisVersionDate = trim($row[3]);
$thisFrequency = trim($row[4]); //6/17/2014
$thisClauseType = trim($row[6]); //10/10/2014
$thisAlternate = trim($row[7]); //2/9/2015
$RequireDepartment = ""; //6/17/2014 START new tbl structure
if ($thisClauseType=="YELLOW") { //10/10/2014
$sqlgetdept = "select RequireDepartment from far_yellowDept where QuestionID='$thisQuestionID'";
} else {
$sqlgetdept = "select RequireDepartment from far_questionsDept where QuestionID='$thisQuestionID'";
} //10/10/2014
$resultgd = mysql_query($sqlgetdept, $dbconnection);
if (!$resultgd) {
echo 'Could not run query [6]: ' . mysql_error();
exit;
}
while ($rowgd = mysql_fetch_array($resultgd)) {
$RequireDepartment .= $rowgd[0] . ", ";
}
unset ($rowgd);
unset ($resultgd);
//$RequireDepartment = trim($row[5]);
//$thisFrequency = trim($row[6]); //6/17/2014 END
$curClauseInfo = ""; //9/15/2014 temp string for Clause info
$curClauseInfo .= "<td class=resultTbl bgcolor=#d9e0e4><a class='rptnav' href='" .$thisURL. "' target=new>" . $thisTitle . "</a>";
if ($thisVersionDate!="" && $thisVersionDate != NULL) {
$curClauseInfo .= "<BR>(" .$thisVersionDate. ")";
}
if ($thisAlternate!="" && $thisAlternate != NULL) { //2/9/2015
$curClauseInfo .= "<BR><BR>" .$thisAlternate;
}
$curStatusStr = ""; //9/15/2014 temp string to store current status for each dept for current FARS clause
$chkQuestion = true; //assume this question is compliance.
foreach ($deptList as $EachDept) { //loop each dept
if (strpos($RequireDepartment, $EachDept) !== false) { //if this question is required for current dept
$curStatusStr .= "<td class=resultTbl nowrap align=center>";
if ($thisClauseType=="YELLOW") { //10/10/2014
//12/11/2014 fetch approval status for YELLOW clause from new table
$sqlConApp = "select * from far_contract_approval where ContractID='$thisID' and QuestionID='$thisQuestionID' and Department='{$EachDept}'";
$resultConApp = mysql_query($sqlConApp, $dbconnection);
if (!$resultConApp) {
echo 'Could not run query [7]: ' . mysql_error();
exit;
}
$num_rowsConApp = mysql_num_rows($resultConApp);
if ($num_rowsConApp>0) { //if approval response is found
$rowConApp = mysql_fetch_array($resultConApp);
$curStatusStr .= "<B>Approved By</B><BR>" . fetchFullName($rowConApp[4]) . "<BR>" . str_replace(" ", "<BR>", $rowConApp[5]);
} else { //if NO approval response is found
$curStatusStr .= "Approval<BR>Required";
}
unset($resultConApp); //12/11/2014
$curStatusStr .= "</td>";
$chkQuestion=false;
} else { //10/10/2014 for clause type = CERTIFY and GREEN
//check for approval
$sqlConApp = "select * from far_contract_approval where ContractID='$thisID' and QuestionID='$thisQuestionID' and Department='{$EachDept}'";
$resultConApp = mysql_query($sqlConApp, $dbconnection);
if (!$resultConApp) {
echo 'Could not run query [7b]: ' . mysql_error();
exit;
}
$num_rowsConApp = mysql_num_rows($resultConApp);
if ($num_rowsConApp>0) { //if approval response is found
$rowConApp = mysql_fetch_array($resultConApp);
$curStatusStr .= "<B>Approved By</B><BR>" . fetchFullName($rowConApp[4]) . "<BR>" . str_replace(" ", "<BR>", $rowConApp[5]);
} else {
//get latest response per each question
$sql2 = "select * from far_responses where QuestionID=$thisQuestionID and Department='{$EachDept}' order by ResponseDate desc limit 1";
$result2 = mysql_query($sql2, $dbconnection);
if (!$result2) {
echo 'Could not run query [8]: ' . mysql_error();
exit;
}
$num_rows2 = mysql_num_rows($result2);
if ($num_rows2>0) { //if response is found
$row2 = mysql_fetch_array($result2);
$thisResponse = $row2[2];
$thisResponseBy = $row2[5];
$thisResponseDate = $row2[6];
$tempResponseDate = str_replace(" ", "<BR>", $thisResponseDate); //6/12/2014 show time on next line
$curStatusStr .= "<B>". $thisResponse . "</B><BR>" . $thisResponseBy . "<BR>" . $tempResponseDate;
if ($thisResponse=="YES") { //if latest response is YES, check if it has expired or not
/* 6/2/2014 Disable this function - no need to check Frequency as all answers will be reset on 1/1
$chkDate = strtotime($thisResponseDate);
$chkDate = strtotime("+$thisFrequency day", $chkDate);
if (time() > $chkDate) {
print "<font color=red><BR>Expired</font>";
$chkQuestion=false;
}
6/2/2014 */
} else { //PENDING or EXPIRED
$chkQuestion=false;
}
} else { //no previous submission
$curStatusStr .= "Response<BR>Pending";
$chkQuestion=false;
}
}
unset($resultConApp); //12/11/2014
$curStatusStr .= "</td>";
} //10/10/2014
} else { //this question does not apply to current dept
$curStatusStr .= "<td class=resultTbl align=center>N/A</td>";
}
}
$curStatusBox = ""; //9/15/2014 temp string to store green/red icon
$curStatusBox .= "<td class=resultTbl align=center bgcolor=#d9f1fe>";
if ($thisContractStatus=="Submitted" || $thisContractStatus=="Suspended" || $thisContractStatus=="Cancelled") { //4/15/2015; 1/6/2015 Contract has been submitted
$sqlFreezeStat = "select LightIndicator from far_contract_clauses where ContractID='$thisID' and QuestionID='$thisQuestionID'";
$resultFreezeStat = mysql_query($sqlFreezeStat, $dbconnection);
if (!$resultFreezeStat) {
echo 'Could not run query [10]: ' . mysql_error();
exit;
}
$num_rowsFS = mysql_num_rows($resultFreezeStat);
if ($num_rowsFS>0) { //if response is found
$rowFS = mysql_fetch_array($resultFreezeStat);
switch ($rowFS[0]) {
case "GREEN": $curStatusBox .= "<img src='images/greenicon.gif' width='20'>"; break;
case "YELLOW": $curStatusBox .= "<img src='images/yellowicon.gif' width='20'>"; break;
case "RED": $curStatusBox .= "<img src='images/redicon.gif' width='20'>"; break;
default: $curStatusBox .= "Status is not being frozen for this record";
}
} else { $curStatusBox .= "Status is not being frozen for this record"; }
unset ($resultFreezeStat);
} else { //1/6/2015: Not submitted - so shows real-time light status
if ((int)$chkQuestion==1) {
$curStatusBox .= "<img src='images/greenicon.gif' width='20'>";
//9/15/2014 Latest Audit Info
/* 12/11/2014 hide audit *** (if want to add it back, need to add fancybox script ***
$sqlchkAudit = "select AuditDate from far_audit where QuestionID=$thisQuestionID order by AuditDate Desc limit 1";
$resultA = mysql_query($sqlchkAudit, $dbconnection);
if (!$resultA) {
echo 'Could not run query [9]: ' . mysql_error();
exit;
}
$num_rowsA = mysql_num_rows($resultA);
if ($num_rowsA >0) { //if response is found
$rowA = mysql_fetch_array($resultA);
$AuditDateTemp = $rowA[0];
$AuditDateSplit = explode(" ", $AuditDateTemp);
$AuditDate = "<a class=\"fancybox\" href=\"audithistory.php?ID=" .$thisQuestionID. "\">" .$AuditDateSplit[0]. "</a>";
//$AuditDate = $AuditDateSplit[0];
} else {
$AuditDate = "N/A";
}
$curClauseInfo .= "<BR><BR>Last Audit: " . $AuditDate;
if($_SESSION['UserAccessLevel']=="FULLAUDIT") //9/15/2014 new access level AUDIT rights
$curClauseInfo .= "<BR>Auditor: <a class=\"fancybox\" href=\"audit.php?ID=" .$thisQuestionID. "\">Audit It</a></td>";
else
$curClauseInfo .= "</td>";
hide audit*** */
} elseif ($thisClauseType=="YELLOW") { //10/10/2014
$curStatusBox .= "<img src='images/yellowicon.gif' width='20'>";
$curClauseInfo .= "</td>";
} else {
$curStatusBox .= "<img src='images/redicon.gif' width='20'>";
$curClauseInfo .= "</td>";
}
}
$curStatusBox .= "</td>";
print "<tr>"; //9/15/2014 Print out new temp strings
print $curClauseInfo;
print $curStatusBox;
print $curStatusStr;
print "</tr>";
}
echo "</table></div>"; //10/15/2014 closing DIV, added document ready
?>
<SCRIPT type="text/javascript">
$(document).ready(function () {
$('#options table').floatThead({
//the pageTop is a global function i have here, it takes care of making the table float under my floated nav
scrollingTop: pageTop,
useAbsolutePositioning: false
});
});
</SCRIPT> <!--10/15/2014 END -->
<?php
}
?>
It's smaller and cleaner and the file_get_contents() can get the job done. But you always need to make sure you have the html of the page.
Here is the table below that I'm trying to delete rows out of:
<form method="POST" >
<table class="sortable">
<thead>
<tr>
<th id="makehead">Make </th>
<th id="modelhead">Model </th>
<th id="idhead">Delete </th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($carArray as $k => $carInfo) {
$i++;
echo '<tr>';
if ($i % 2) {
echo '<td class="make">' . $carInfo['make'] . '</td>
<td class="model">' . $carInfo['model'] . '</td>
<td class="id"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
} else {
echo '<td class="makelight">' . $carInfo['make'] . '</td>
<td class="modellight">' . $carInfo['model'] . '</td>
<td class="idlight"><input type="checkbox" name="id" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
}
}
?>
</tr>
</table>
</tbody>
<td>
<input Onclick="return ConfirmDelete();" name="delete" type="submit" id="delete" value="Delete"></input>
</td>
</table></form>
As you can see i'm using checkboxes to tick each row then the delete button will have a confirm message then should delete but it doesn't here is my if statement:
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->setdbid($dbid);
So when this wasn't working I had a look on here and on other questions people said I need a setter function so i did this: EDIT: this is my class file.
public function setdbid($dbid){
$this->dbid=$dbid;
}
for this main function to delete things:
public function delete($dbid) {
try {
$sql = "DELETE FROM cars WHERE id = '$dbid'";
$this->db->exec($sql);
echo "Car has been deleted.";
} catch (PDOException $e) {
echo $e->getMessage();
}
}
So that's all the relevant code I think, please help me if you can.
You just have to replace some piece of code in PHP :
if ($_REQUEST['delete']) {
$dbid = $_REQUEST['id'];
$db->delete($dbid); //Assuming delete is well a $db method, else replace it by the correct delete call
}
As you are using checkboxes with the same name, you have to change it so it's an array (and this way you'll be able to delete multiple rows at once) :
<td class="id"><input type="checkbox" name="ids[]" value="' . $carInfo['id'] . '">' . $carInfo['id'] . '</td>';
Then in your php code, treat this data as such :
if ($_REQUEST['delete']) {
foreach($_REQUEST['ids'] as $id){
$xxx->delete(intval($id)); //convert to integer to avoid sql injection.
}
}
Note that you don't need to set $db->setdbid since you pass that id as a parameter of your delete method.
What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>
I have put together a basic order list for admin users in php for checking order contents placed by logged in users.
The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).
The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.
However, it doesn’t display the user’s name and surname.
I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:
<?php
$page_title = 'View Individual Order';
include ('includes/header.html');
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{ // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{ // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/header.html');
exit();
}
?>
<h1>Order Details</h1>
<?php
require_once ('database.php'); // Connect to the db.
// Retrieve the user's, order and product information.
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price,
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result)) { // Valid user ID, show the form.
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
}// end of WHILE loop
echo '</table>
<p> Here:</p>
<br><br>
<p> << Back to Orders</p>
<p> </p>
<p> </p>
<p> </p>
';
} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close the database connection.
?>
<p>Footer here</p>
<?php
include ('./includes/footer_admin_user.html'); // Include the HTML footer.
?>
One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:
if (mysql_num_rows($result)) { // Valid user ID, show the form.
/*********** I added this line ***********/
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
/*********** I changed this from a while loop to a do-while loop ***********/
do { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
} while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop
It looks like the problem is when you're trying to display the user's name:
echo '<p>Order for:<strong>'.$row[2].' '.$row[3]
The $row variable doesn't exist yet. Not seeing your database or the result from your database query, my guess is that the user's name is repeated next to every single item in their order, so it might be as simple as just starting the WHILE loop, and checking to see if you've printed their name yet:
$lastUser = NULL;
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] !== $lastUser) {
if (isset($lastUser)) {
// finish up the report for the previous user
}
// echo the stuff for the current user's name
$lastUser = $row[0];
}
// go on echo-ing their order information
}
// after the while loop is over,
// close up the last user's report
But like I said, this is just a guess, and might be totally off.
The problem is that you tried to access $row[2] and $row[3] before mysql_fetch_array(). Since you are already echo'ing HTML tags, why don't you "buffer" your output first like this?:
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
$order = '<tr bgcolor="' . $bg . '">
<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
$orders[$row[2] . " " . $row[3]][] .= $order;
}
Then do a second foreach loop for the $orders
foreach($orders as $name => $orderList)
{
echo "Order for: $name";
echo "<table ...>";
foreach($orderList as $order)
{
echo $order;
}
echo "</table>";
}
I have a table like this full of orders, each customer will have multiple orders:
Customer ponumber quantity ..
customer1 234345 56
customer2 343454 34
customer1 w34234 54
customer5 332423 54
I'm trying to loop through each DISTINCT customer and print the corresponding orders with the following code.
<?php
include("php/database_connect.php");
$query = mysql_query("SELECT DISTINCT customer FROM orders");
$customer = mysql_fetch_array($query);
foreach($customer as $customers)
{
echo ' <li><h1>'. $customers . '</h1></li>';
$result = mysql_query("SELECT * FROM orders WHERE misc='new' AND customer='$customers' ORDER BY columnpos ASC ");
while($row = mysql_fetch_array($result))
{
echo'
<li id="id_' . $row['id'] . '">
<div class="card">
<table>
<tr>
<td>' . $row['customer'] . '</td>
<tr>
<td>P/O: ' . $row['ponumber'] . '</td>
</tr>
<tr>
<td>' . $row['partnumber'] . '</td>
</tr>
<tr>
<td><b>' . $row['quantity'] . '</b> x ' . $row['foil'] . '</td>
</tr>
<tr>
<td>' . $row['daterequired'] . '</td>
</tr>
<tr>
<td>' . $row['prep'] . '</td>
</tr>
</table>
<input class="hiddenid" type="hidden" value="' . $row['id'] . '" />
<input class="hiddenquantity" type="hidden" value="' . $row['quantity'] . '" />
<input class="hiddenpartnumber" type="hidden" value="' . $row['partnumber'] . '" />
<input class="hiddenfoil" type="hidden" value="' . $row['foil'] . '" />
</div>
</li>
';
}
}
?>
However the above code currently prints:
customer1 234345
customer1 w34234
customer1 234345
customer1 w34234
which seems odd... its not going through each customer.
any help is greatly appreciated!
Make only one DB query (better performance) but process the results before looping over them:
$result = mysql_query("SELECT * FROM orders WHERE misc='new' ORDER BY customer ASC ");
$ordersByCustomer = array(); // nested array. 1. level: customers, 2. level: orders
while(($row = mysql_fetch_assoc($result))) {
if(!array_key_exists($row['customer'], $ordersByCustomer)) {
$ordersByCustomer[$row['customer']] = array();
}
$ordersByCustomer[$row['customer']][] = $row;
}
foreach($ordersByCustomer as $customer=>$orders) {
echo ' <li><h1>'. $customer . '</h1></li>';
foreach($orders as $order) {
// rest of HTML code
}
}
I also encourage you to use a better separation of HTML and PHP, like so, using the alternative syntax for control structures:
<?php foreach($ordersByCustomer as $customer=>$orders): ?>
<li><h1><?php echo $customer; ?></h1></li>
<?php foreach($orders as $order): ?>
<li id="id_<?php echo $row['id'];?>">
<div class="card">
<table>
<tr><td><?php echo $order['customer']; ?></td></tr>
<!-- and so forth -->
<?php endforeach; ?>
<?php endforeach; ?>
Btw, you are not generating valid HTML. li element must be contained in ul or ol elements.
use
while($customer = mysql_fetch_array($query){
instead of
$customer = mysql_fetch_array($query);
foreach($customer as $customers)
{