Im trying to customise search fields and then display in a pdf but the code just doesnt work. can someone take a look and see why exactly this is not working? if u see any mistakes?

<?php
//access incoming name, id or radio button values
$cs = $_GET['cs'];
$name=$_GET['name'];
$uni=$_GET['uni'];
$dept=$_GET['dept'];
$course=$_GET['course'];
$cd=$_GET['course_date'];
$gen=$_GET['gen'];
$sf=$_GET['selectfields'];
$title=$_GET['title'];

$con=mysql_connect("localhost", "root", "") or die ("couldnt connect");
mysql_select_db("sdc_cpds") or die ("couldnt select");

//search query

$searchquery="SELECT * FROM course_participant WHERE name LIKE '%$name%' AND gender LIKE '$gen%' AND university LIKE '%$uni%' AND facdept LIKE '%$dept%'  AND course LIKE '%$course%' AND course_date LIKE '%$cd%' AND completion LIKE '%$cs%' UNION SELECT * FROM past_participant WHERE name LIKE '%$name%' AND gender LIKE '%$gen%' AND university LIKE '%$uni%' AND facdept LIKE '%$dept%'  AND course LIKE '%$course%' AND course_date LIKE '%$cd%' AND completion LIKE '%$cs%' ORDER BY name";

$result=mysql_query($searchquery);
$num_rows=mysql_num_rows($result);

echo "<b>$num_rows</b> results found<br>"; 
//echo "<INPUT TYPE='image' SRC='http://localhost/drupal/printwebpagepdf.gif' OnClick='clickPrint()'>";
//display results(search results table)
if($num_rows >0) {

echo "<div style='width:620px; height:100%;'>";
echo "<table border='2' width='100%' id='blutable'>";
echo "<tr style='background-color:#A9D0F5'><th></th><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th><th>Gender</th><th>Age</th><th>Marital Status</th><th>University</th><th>Fac/Dept</th><th>Course</th><th>Course Date</th><th>Reg. Date</th><th>Completion</th></tr>";

$i=1;

while($row = mysql_fetch_array($result)) {

$r = fmod($i,2);
if($r=="1"){
$rowclass="odd";
}
else{
$rowclass="even";
}

echo "<tr class='$rowclass'>";
//echo "<td><input type='submit' value='EDIT' onclick='editer($i)' style='width:50px; height:25px background;'/></td>";

echo "<td style='padding:1px;' valign='bottom'><INPUT TYPE='image' SRC='http://localhost/drupal/more.png' onclick='editer($i)' style='width:25px; height:25px;'/></td>";


 echo "<td nowrap><div id='cpid_$i'>";echo $row['name'];"</div></td>";
 //echo "<td><div id='cpid_$i'>";echo $row['nic'];echo "</div></td>";
 echo "<td nowrap>";echo $row['res_address'];echo "</td>";
 echo "<td>";echo $row['cont_no'];echo "</td>";
 echo "<td>";echo $row['email'];echo "</td>";
 echo "<td>";echo $row['gender'];echo "</td>";
 echo "<td>";echo $row['age'];echo "</td>";
 echo "<td>";echo $row['marital_stat'];echo "</td>";
 echo "<td nowrap>"; echo $row['university']; echo "</td>";
 echo "<td nowrap>"; echo $row['facdept']; echo "</td>";
 echo "<td nowrap><div id='course_$i'>"; echo $row['course']; echo "</div></td>";
 echo "<td nowrap><div id='coursed_$i'>"; echo $row['course_date']; echo "</div></td>";
 echo "<td nowrap>"; echo $row['reg_date']; echo "</td>";
 echo "<td>";echo $row['completion']; echo "</td>";
echo "</tr>";

$i++;
}

echo "</table>";
echo "</div>";
echo "<form action='http://localhost/drupal/pdf.php' method='POST' name='prnt'/>";
echo "<input type='hidden' name='cs' id='cs' value='$cs'/>";
echo "<input type='hidden' name='name' id='name' value='$name'/>";
echo "<input type='hidden' name='uni' id='uni' value='$uni'/>";
echo "<input type='hidden' name='dept' id='dept' value='$dept'/>";
echo "<input type='hidden' name='course' id='course' value='$course'/>";
echo "<input type='hidden' name='course_date' id='course_date' value='$course_date'/>";
echo "<input type='hidden' name='gen' id='gen' value='$gen'/>";
echo "<input type='hidden' name='selectfields' id='selectfields' value='$sf'/>";
echo "<input type='hidden' name='title' id='title' value='$title'/>";
//echo "<input type='submit' value='PRINT RESULTS' name='pr' style='float:right;' class='up'onmouseover='this.className='over''onmouseout='this.className='up'/>";
echo "</form>";
}
?>
<html>
<form action="http://localhost/drupal/pdf.php" id="cust" method="post" style="background-color:#F2F2F2; border:1px solid; border-color:#084B8A;
width:98%; padding:10px;" >
	<table>
		<tr>
			<td><strong>Title of Your Report:</strong></td>
			<td>
				<input type="text" name="title" value="" size="60" id="title" />
<!-- BLOCK # 01 -->
                <!-- all hidden fields go here -->
				<?php
                echo "<input type='hidden' name='cs' value='$cs' />";
                echo "<input type='hidden' name='name' value='$name' />";
                echo "<input type='hidden' name='uni' value='$uni' />";
                echo "<input type='hidden' name='dept' value='$dept' />";
                echo "<input type='hidden' name='course' value='$course'/>";
                echo "<input type='hidden' name='course_date' value='$course_date' />";
                echo "<input type='hidden' name='gen' value='$gen' />";
				?>
<!-- END OF BLOCK # 01 -->
			</td>
		</tr>
		<tr>
			<td>
				<input type="radio" name="name" value="name" checked/> Name
			</td>
			<td></td>
		</tr>
		<tr>
			<td><strong>Tick Fields Required:</strong></td>
			<td></td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[address]" /> Address
			</td><td>
				<input type="checkbox" name="selectfields[contactnumber]" /> Contact Number
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[gender]" /> Gender
			</td><td>
				<input type="checkbox" name="selectfields[age]" /> Age
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[email]" /> E-Mail
			</td><td>
				<input type="checkbox" name="selectfields[maritalstatus]" /> Marital Status
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[university]" /> University
			</td><td>
				<input type="checkbox" name="selectfields[facdept]" /> Faculty/Department
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[course]" /> Course
			</td><td>
				<input type="checkbox" name="selectfields[cd]" /> Course Date
			</td>
		</tr>
		<tr>
			<td>
				<input type="checkbox" name="selectfields[rd]" /> Registration Date
			</td><td>
				<input type="checkbox" name="selectfields[completion]" /> Completion
			</td>
		</tr>
		<tr>
			<td> </td>
			<td>
				<input type="submit" value="CUSTOMIZE & PRINT" style="float:right;" class="up"onmouseover="this.className='over'"onmouseout="this.className='up'" />
			</td>
		</tr>
	</table>
</form>
</html>

<?php
//close connection
mysql_close($con);


?>

thats the search.php code

here is the pdf.php code

<?php
# Catch the posted data, validate by getting rid of > and <
$header =$_POST['header'];
# Replace > and < with a blank space
$header = str_replace('<','',$header);
$header = str_replace('>','',$header);
$body =$_POST['body'];
# Replace > and < with a blank space
$body = str_replace('<','',$body);
$body = str_replace('>','',$body);

# Include the file that does all of the work
include ('class.ezpdf.php');

# Start a new PDF file
$pdf =& new Cezpdf();

# Select the font we'll be using. There are more fonts in the zip file.
$pdf->selectFont('./fonts/Helvetica.afm');

# include the header, then move down a couple of lines, font size 25
# justification centered (centred if you're from the UK)
$pdf->ezText($header . "\n\n",5,array('justification'=>'centre'));

# include the body after moving down 7 lines to get under the pic.
# Font size 16, justification centered.
$pdf->ezText("\n" . $body,8,array('justification'=>'centre'));

//inside
$pdf->ezText('',6);
//--------------------------------------------------
// you will have to change these to your settings
$host = 'localhost';
$user = 'root';
$password = '';

$database = 'sdc_cpds';

$cs=$_POST['cs'];
$name=$_POST['name'];
$uni=$_POST['uni'];
$dept=$_POST['dept'];
$course=$_POST['course'];
$course_date=$_POST['course_date'];
$gen=$_POST['gen'];
$sf=$_POST['selectfields'];

$query ="SELECT * FROM course_participant WHERE name LIKE '%$name%' AND gender LIKE '$gen%' AND university LIKE '%$uni%' AND facdept LIKE '%$dept%'  AND course LIKE '%$course%' AND course_date LIKE '%$cd%' AND completion LIKE '%$cs%' UNION SELECT * FROM past_participant WHERE name LIKE '%$name%' AND gender LIKE '%$gen%' AND university LIKE '%$uni%' AND facdept LIKE '%$dept%'  AND course LIKE '%$course%' AND course_date LIKE '%$cd%' AND completion LIKE '%$cs%' ORDER BY name";

//--------------------------------------------------
/*
$data = array(  array('Name'=>1,'Address'=>'gandalf','Contact Number'=>'wizard','Email'=>'wizard','Gender'=>'wizard','Age'=>'wizard','Marital Status'=>'wizard','University'=>'wizard','Fac/Dept'=>'wizard','Course'=>'wizard','Course Date'=>'wizard','Reg Date'=>'wizard') );
*/
//$pdf->ezTable($data);
// open the connection to the db server
$link = mysql_connect($host,$user,$password);
// change to the right database
mysql_select_db($database);
// initialize the array
$data = array();
// do the SQL query
$result = mysql_query($query);
// step through the result set, populating the array, note that this could also have been written:
// while($data[] =  mysql_fetch_assoc($result)) {}
while($data[] =  mysql_fetch_array($result, MYSQL_ASSOC)) {}

$cols=array("name"=>"Name","res_address"=>"Residential Address","course"=>"Course");

/*while($data = mysql_fetch_assoc($result)) {
	
	if($_POST['name'] == 'name') {
		echo $data['name'];
	}
	
	if($_POST['selectfields']['address'] == 'on') {
		echo $data['res_address'];
	}
	
	if($_POST['selectfields']['contactnumber'] == 'on') {
		echo $data['cont_no'];
	}
	if($_POST['selectfields']['age'] == 'on') {
		echo $data['age'];
	}
	if($_POST['selectfields']['gender'] == 'on') {
		echo $data['gender'];
	}
	if($_POST['selectfields']['maritalstatus'] == 'on') {
		echo $data['marital_stat'];
	}
	if($_POST['selectfields']['university'] == 'on') {
		echo $data['university'];
	}
	if($_POST['selectfields']['facdept'] == 'on') {
		echo $data['facdept'];
	}
	if($_POST['selectfields']['cd'] == 'on') {
		echo $data['course_date'];
	}
	if($_POST['selectfields']['rd'] == 'on') {
		echo $data['reg_date'];
	}
	if($_POST['selectfields']['email'] == 'on') {
		echo $data['email'];
	}
	if($_POST['selectfields']['completion'] == 'on') {
		echo $data['completion'];
	}
	if($_POST['selectfields']['course'] == 'on') {
		echo $data['course'];
	}
	
}*/
$pdf->ezTable($data, $cols);

// do the output, this is my standard testing output code, adding ?d=1
// to the url puts the pdf code to the screen in raw form, good for checking
// for parse errors before you actually try to generate the pdf file.
if (isset($d) && $d){
  $pdfcode = $pdf->output(1);
  $pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
  echo '<html><body>';
  echo trim($pdfcode);
  echo '</body></html>';
} else {
  $pdf->stream();
}



#create the pdf and stream it to the page
$pdf->output();
$pdf->ezStream();
?>

and this is the search form

<html>
<head>
<style type="text/css">


</style>
<script type="text/javascript" src="http://localhost/drupal/jquery.js"></script>

<script type="text/javascript">


//AJAX FUNCTION TO GET RESULTS FROM PHP SEARCH PAGE

function ajaxsearch(){

//Taking values from the form and assigning into variables
var name=$("#name").val();
var uni=$("#uni").val();
var dept=$("#dept").val();
var gen=$("#gen").val();
var course=$("#course").val();
var dofcourse=$("#cd").val();

$("#cust #uni").val($("#myForm #uni").val());
$("#cust #name").val($("#myForm #name").val());
$("#cust #gen").val($("#myForm #gen").val());
$("#cust #cs").val($("#myForm #cs").val());
$("#cust #dept").val($("#myForm #dept").val());
$("#cust #course").val($("#myForm #course").val());
$("#cust #cd").val($("#myForm #cd").val());

//Assigning radio button values into variables
if (document.f1.cs[0].checked==true){
var v="yes";
}
else if (document.f1.cs[1].checked==true){
var v="no";
}
else if (document.f1.cs[2].checked==true){
var v="cur";
}
else if(document.f1.cs[3].checked==true){
var v="";
}

if (document.f1.gen[0].checked==true){
var g="Male";
}
else if (document.f1.gen[1].checked==true){
var g="Fem";
}
else if (document.f1.gen[2].checked==true){
var g="";
}

//Ajax get request
$.get("http://localhost/drupal/samsearch.php", { name: name, course: course, course_date: dofcourse, cs: v, uni: uni, dept: dept, gen: g},
   function(data){
     //alert("Data Loaded: " + data);
document.getElementById("show").innerHTML=data;
   });
return false;
}

//FUNCTION TO DIRECT USER TO EDIT PAGE 
function editer(id){

//Taking values from the form and assigning into variables
var idn = document.getElementById("cpid_" + id).innerHTML;
var crs = document.getElementById("course_" + id).innerHTML;
var crsd = document.getElementById("coursed_" + id).innerHTML;

//Building query string for get request
var queryString = "?name=" + idn + "&course=" + crs + "&course_date=" + crsd;

//Redirect using query string
var location="http://localhost/drupal/node/3" + queryString;
var newwin;
newwin=window.open(location);
newwin.focus();
	

}

</script>

</head>

<strong>SEARCH</strong><br/>
<div>
<form id="myForm" name="f1" style="background-color:#F2F2F2; border:1px solid; border-color:#084B8A;
width:98%; padding:10px;" onSubmit=" return ajaxsearch()">
<table>
<tr>
<td>Name:</td><td width="100%"><input type="text" size="60" name="name" id="name"/></td>
</tr>

<tr>
<td>Gender:</td>
<td width="500">
<input type="radio" name="gen" value="male">MALE</radio> 
<input type="radio" name="gen" value="female">FEMALE</radio> 
<input type="radio" name="gen" value="either" checked>EITHER</radio> 
</td>
</tr>

<tr>
<td>University:</td><td width="150"><input type="text" size="50" name="uni" id="uni"/></td>
<td>Department/Faculty:</td><td width="150"><input type="text" size="50" name="dept" id="dept"/></td>
</tr>

<tr>
<td>Course:</td>
<td>
<input type="text" name="course" id="course"/>
Date of Course:
<input type="text" name="cd" id="cd"/>
</td>
<td>Completed Successfully:</td>
<td width="500">
<input type="radio" name="cs" value="yes">YES</radio> 
<input type="radio" name="cs" value="no">NO</radio> 
<input type="radio" name="cs" value="cur">CURRENT</radio> 
<input type="radio" name="cs" value="all" checked>ALL</radio> 
</td>
</tr>


</table>
</div>

<input type="reset" value="RESET" name="find" style="float:right;" class="up"onmouseover="this.className='over'"onmouseout="this.className='up'"/>

<input type="submit" value="FIND" name="find" class="up"onmouseover="this.className='over'"onmouseout="this.className='up'"/>

<br/>
</form>

<div id="show" style='width:100%; height:100%; overflow:auto; border:1px solid; border-color:#084B8A;'>
</div>

</html>

Recommended Answers

All 3 Replies

what do mean by not working? syntax, query or what?

i mean. thiis code is for creating a pdf based on a search criteria. when something is searched, based on the criteria, the entire table's fields are displayed. but when priniting something like a report we dont need all fields. user will need particular fields only, for example only name and address etc.

but when i press customise and print even tho the pdf opens its blank, how do i get that customized table to be displayed on the pdf?

for customization, once search results are displayed below is a box for customizing with checkboxes to choose what fields are needed.

please help.

in your pdf.php file add both lines. it will display error description with line number.
error_reporting(E_ALL);
ini_set("display_errors", 1);

then I am not sure about line number
122-124. I think you should not echo anything while creating a pdf file. you should concat this lines with variable, $pdfcode.

Also remove any blank line before <?php tag or after ?> tag.

comment following lines. and any other print/echo statment you are using in pdf generation.

echo '<html><body>';  
echo trim($pdfcode);  
echo '</body></html>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.