Jump to content

Best way to make this into class method?


devxtec

Recommended Posts

I'm in the process of converting a PHP application that was written in 4 to be written in 5 and use OOP. What is the best way to convert the code below to be a class method that is called if $linkObj-> edit is 1? I'm trying to avoid putting the HTML also inside the method. Any insight would be appreciated as I'm learning OOP as I go along, so the more examples the more I'll learn as I'm a visual learner. I'll be replacing all the mysql functions to use PDO as the new rewritten code will be interfacing with different databases.

 

<?php else if( $linkObj->edit == "1" )
{
    ?>
<html>
<head>
<title>Edit Link</title>
</head>
<script language="javascript">
function wrap_up() {
    var par = self.opener;
    par.location.reload(false);
    self.close();

}
</script>
<body>
    <?php
    $custId = $_GET['id'];
    $linkId = $_GET['link'];

    $qry = "select * from Links where CustomerNum = " . $custId . " and LinkIndex = " . $linkId;
    $res = mysql_query( $qry, $conn );
    if( $res )
    {
        ?>
<form name="editlink" action="link.php" method="post"><input
type="hidden" name="custId" value="<?php echo $custId?>" /> <input
type="hidden" name="linkId" value="<?php echo $linkId?>" /> <input
type="hidden" name="save" value="Yes" />
<table width="100%" border=0>
<?php
while( $row = mysql_fetch_assoc( $res ) )
{
    ?>
<tr>
	<td align="right">Link Text:</td>
	<td align="left"><input type="text" name="linktext"
		value="<?php echo $row['LinkText'];?>" /></td>
</tr>
<tr>
	<td align="right">Link Target:</td>
	<td align="left"><select name="linktarget">
	<?php
	$qry = "select PageName, BodyIndex from Body where CustomerNum = " . $custId . " and Active = 'Y' order by BodyIndex";
	//echo $qry;
	$res2 = mysql_query( $qry, $conn );
	if( $res2 )
	{
	    while( $row2 = mysql_fetch_assoc( $res2 ) )
	    {
	        $tag = "<option value=\"".$row2['BodyIndex']."\"";
	        if( $row2['BodyIndex'] == $row['BodyIndex'] )
	        {
	            $tag .= " selected ";
	        }
	        $tag .= ">".$row2['PageName']."</option>";
	        echo $tag;
	    }
	}
	?>
	</select></td>
</tr>
<?php
}
?>
<tr>
	<td colspan="2" align="right"><input type="submit" name="but1"
		value="Save" /><input type="button" name="but2" value="Cancel"
		onclick="wrap_up();" /></td>
</tr>
</table>
</form>
<?php
    }

    ?>
</body>
</html>
    <?php
}

Link to comment
Share on other sites

You can use Google example's to work this out really.

 

or the current book ur using now, u said learning so we exspect you to do the work then ask if stuck,

very clever chap

 

i dont use oop but i like ur code..

 

The books I'm referencing don't really have a good example. I was thinking about possibly implementing template files but my only concern then would be as I rewrite more an more code it'll end up being a template for each page. I'm trying to keep this as clean and simple as possible without crossing the HTML and PHP as much as possible. Separating Logic and Presentation/Display from one another, which will help making visual changes easier.

 

Could you provide a link to the google example page? Oh and I've learned a lot and have already rewritten several files into 5 classes to simplify the site. It's just I've now run into an area that it seems is more personal preference in how people handle it. So if anyone can throw out ideas on how they would handle it then I could probably come up with something. I'm just having a thought block and unable to think up a way to handle the issue in the best way.

Link to comment
Share on other sites

If you are going to use PHP and OOP and all that fanciness, I would suggest learning an MVC framework like CakePHP, Kohana, Symfony etc..

 

MVC is a pattern where an application is broken up into Models (the database) Views (html templates) and Controllers (PHP code that ties the two together).

 

Everybodys doin' it!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.