Jump to content

Problem in a href location from php code to html code

- - - - -

  • Please log in to reply
5 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
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>  

</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.

#2
RHochstenbach

RHochstenbach

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Unfortunately HTML can not process variables or other PHP statements. The only thing you can do is echoing the entire line through PHP, like this:

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



#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Why are you hardcoding things when you are using templates? You should write {Emp_id} in the template or whatever the template allows you, and have the PHP template parser that you include to convert it in to the appropriate value. I would highly recommend to understand the code you are working with, you seem to be shooting in the dark on every corner and this often makes for bad code.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts

RHochstenbach said:

Unfortunately HTML can not process variables or other PHP statements. The only thing you can do is echoing the entire line through PHP, like this:

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



I cannot put an php syntax in my html code. eventhough I want it.

#5
rySch

rySch

    Newbie

  • Members
  • Pip
  • 8 posts
why not?
Php gets parsed way before html so it wont make any difference?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

rySch said:

why not?
Php gets parsed way before html so it wont make any difference?

I believe he means that he is not allowed to put PHP in there, as per his job requirements.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users