Jump to content

Recommended Posts

Hi, I'm a noob to OOP in PHP and am in the process of converting a static site to a dynamic one. As I'm progressing, I'm not sure that I've laid the foundations correctly for an administrative editing page that I'm working on. As originally set up, I have a class, class.inc.animals.php, which includes functions getAllEdit(), which retrieves a result set of all animals currently in the database, orders the result, and then for each row calls function formatAllEdit(), which formats the row and echoes it into a div. I'm now working on implementing different sorting functions for the result set, which should on change re-order the result set and replace the div contents with the newly ordered set.

 

I'm thinking that changing the value of the select should trigger an Ajax call, which should not requery the database, but only reorder it and replace the contents of the div with the newly re-ordered result-set. I'm not sure I understand the best way to do that in an OOP context though.

 

Here's some of the relevant code. The editing page includes:

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])):
	include_once "inc/class.animals.inc.php";
	$sheepObj = new Sheep($db);
	$pageTitle = "Edit Animals";
	$pageDescription = "Administration - edit animals";		
	include_once "common/header.php";
?>
<div id="sheepform">
	<div id="searchheader">
		Sort by;
			<select class="middle" id="searchtype">
				<option value="nameASC">Name ascending</option>
				<option value="nameDESC">Name descending</option>
				<option value="dobASC">DOB ascending</option>
				<option value="dobDESC">DOB descending</option>
			</select>
	</div>
	<form action="unknown.php" method="post">
<?php 
$sheepObj->displayviewheader(); // displays a filter bar

$sheepObj->getAllEdit("nameASC"); // uses name ascending as default sort type on page load
?>
</div> <!-- end sheepform -->

 

and class.animals.inc.php includes:

 

public function getAllEdit ($searchtype) {
 	// $sql statement prepared here

	try {
		$stmt = $this->_db->prepare($sql);
		$stmt->execute();
		$arrayAll = $stmt->fetchAll(); 
		$stmt->closeCursor();

	} catch (PDOException $e) {
			return $e->getMessage();
	}
	switch ($searchtype) {
		case "nameASC":
			usort($arrayAll, array($this, 'compareLastNameASC'));
			break;
		case "nameDESC":
			break;
		case "dobASC":
			break;
		case "dobDESC":
			usort($arrayAll, array($this, 'compareDOBdesc'));
			break;
	}
	foreach($arrayAll as $row) echo $this->formatAllEdit($row);
 }

 /**
  * Formats each row for editing in Edit Animals.php
  *
  *
  */
public function formatAllEdit($row) {
	// prepare values
	// various code to prepare variables goes here
	return '<div class="'.$class.'">
		<span class="first"><a href="editOne.php?sheepID='.$id.'">'.$name.'</a></span>
		<span class="second">'.$dob.'</span>
		<select class="third">'.$sold.'</select>
		<select class="fourth">'.$active.'</select>
	</div>
	';
}

 

How would you suggest refactoring this so that when the select#searchtype is changed, an Ajax call is made that re-sorts the existing record set and replaces the contents of the div#sheepform with the newly sorted results? Do I need to split the getAllEdit() function up so that the sorting, including the switch($searchtype), is in one separate function?

 

And how do you make an Ajax call on an existing object?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/228187-best-way-to-architect-this/
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.