PDA

View Full Version : Problem in a href location from php code to html code



newphpbees
11 May 2011, 09:17 AM
Good day!

On my old code I mixed html and php code in <a href> now I need to separate my php code to html code.

I don't know how can I get the id without using php in my <a href>

this is my old code for the link of Edit and Delete


<td><a href = 'edit.php?id=<?php echo $emp_id; ?>'>Edit</a> <a href='delete.php?id=<?php echo $emp_id; ?>' onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>


As you can see I used php code to echo the Emp_ID now i need to change into html code.

this is my new machine1.php


<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
include('includes/config.sender.php');
include('includes/template.inc');


/*Sorting of Data*/
$sort = "ASC";
$data_sort = "Emp_ID";

if(isset($_GET['sorting']))
{
if($_GET['sorting'] == 'ASC'){
$sort = "DESC";
}
else{
$sort = "ASC";
}
}


if (isset($_GET['field_name'])) {
if($_GET['field_name'] == 'Emp_ID'){
$data_sort = "Emp_ID";
}
elseif($_GET['field_name'] == 'Last_Name'){
$data_sort = "Last_Name";
}
elseif($_GET['field_name'] == 'First_Name'){
$data_sort = "First_Name";
}
elseif($_GET['field_name'] == 'Birthday'){
$data_sort = "Birthday";
}
}




/*Pagination, Sorting and Limit*/

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}


$sql_select = "SELECT COUNT(*) as numrows
FROM
machine_problem_rhoda";
$result = $_DB->opendb($sql_select);

if(isset($result[0]['numrows'])) //check if result returned a numrows
{
$numrows = (int)$result[0]['numrows']; //typecast, to clearify the value range.
}
else
{
$numrows = 0;//or false -
}


$rows_per_page = 5;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;



$sql_select = "SELECT
Emp_ID,
Last_Name,
First_Name,
Birthday
FROM
machine_problem_rhoda
ORDER BY $data_sort $sort $limit
";
$rows = $_DB->opendb($sql_select);

$tpl = new Template('.', 'keep');
$tpl->set_file(array('handle' => 'html/machine1.html'));

$tpl->set_block('handle', 'block_list', 'tag_list');
foreach($rows as $row) {
$tpl->set_var(array('id'=> $row['Emp_ID'],
'lastname' => $row['Last_Name'],
'firstname' => $row['First_Name'],
'birthday' => $row['Birthday'],
'sorting' => $sort
));
$tpl->parse('tag_list', 'block_list', true);
}

$tpl->parse('handle', array('handle'));
$tpl->p('handle');
?>

<?php

if(isset($_GET['sorting']))
{
if($_GET['sorting'] == 'ASC'){
$sort = "ASC";
}
else{
$sort = "DESC";
}
}

if ($pageno == 1) {
echo " FIRST PREV ";
} else {
?>
<a href="machine1.php?pageno=1&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">FIRST</a>
<?php
$prevpage = $pageno-1;
?>
<a href="machine1.php?pageno=<?php echo $prevpage;?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">PREV</a>

<?php
}
echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;

?>

<a href="machine1.php?pageno=<?php echo $nextpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">NEXT</a>
<a href="machine1.php?pageno=<?php echo $lastpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">LAST</a>
<?php
}

?>


and this is my machine.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Machine 1</title>
</head>

<body>

<table border="1" cellpadding="1" cellspacing="1">

<tr>
<td><a href="machine1.php?sorting={sorting}&field_name=Emp_ID">Employee Id</a></td>
<td><a href="machine1.php?sorting={sorting}&field_name=Last_Name">Last Name</a></td>
<td><a href="machine1.php?sorting={sorting}&field_name=First_Name">First Name</a></td>
<td><a href="machine1.php?sorting={sorting}&field_name=Birthday">Birthday</a></td>
<td>Option</td>
</tr>


<!-- BEGIN block_list -->
<tr>
<td>{id}</td>
<td>{lastname}</td>
<td>{firstname}</td>
<td>{birthday}</td>
<td><a href = 'edit.php?id=<?php echo $emp_id; ?>'>Edit</a> <a href='delete.php?id=<?php echo $emp_id; ?>' onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>
</tr>
<!-- END block_list -->
</table>

<A HREF="javascript:void(0)" onClick="window.open('add.php','welcome','width=300,height=200')">
<input type="button" name="add" value="ADD"> </A>&nbsp;&nbsp;
</body>
</html>


and this is my edit.php where the id was declare to check what data will be edit.



<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
include('includes/config.sender.php');
include('includes/template.inc');


$id=$_GET['id'];

$sql_select = "SELECT
Emp_ID,
Last_Name,
First_Name,
Birthday
FROM
machine_problem_rhoda
WHERE
Emp_ID = $id";

$info = $_DB->opendb($sql_select);

//$data_p = mysql_query("SELECT * FROM tbl_machine1 WHERE Emp_ID = $id") or die(mysql_error());
//while($info = mysql_fetch_array( $data_p ))
//{
$emp_id = $info['Emp_ID'];
$lname = $info['Last_Name'];
$fname = $info['First_Name'];
$bday = $info['Birthday'];

$date = date('d-m-Y', strtotime($bday));
//}

if(isset($_POST['update'])){
$id=$_GET['id'];
$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['date'];
$date = date('Y-m-d', strtotime($bday));



$Lname = addslashes_gpc($Lname);
$Fname = addslashes_gpc($Fname);
$date = addslashes_gpc($date);


$sql_update = "UPDATE machine_problem_rhoda SET
Last_Name = '".$Lname."',
First_Name = '".$Fname."',
Birthday = '".$date."'
WHERE Emp_ID = '".$id."'";

//mysql_query("UPDATE tbl_machine1 SET Last_Name = '".$Lname."', First_Name = '".$Fname."', Birthday = '".$date."' WHERE Emp_ID = '".$id."' ");
header ('Location:machine1.php');
}

$tpl = new Template('.', 'keep');
$tpl->set_file(array('handle' => 'html/edit.html'));

$tpl->parse('handle', array('handle'));
$tpl->p('handle');
?>


I don't know how can I set_var or how can I call the variable in my <a href> to check what should be edit.I am new in template,function and separating php and html.