Jump to content

Strategic app-building advice needed


bothwell

Recommended Posts

I have an application which is now growing in terms of functionality and complexity. To save time and headaches later during maintenance, I'm trying to reduce my code so it's as DRY as possible.

 

An example: I find myself repeating this behemoth over and over again inside various if statements (I wouldn't advise actually reading through it all, it's just for size demonstration):

 

    if (mysql_num_rows($res) == 0) { print "<p>Couldn't find anything in this street!</p>"; }
     else {
       $num_rows = mysql_num_rows($res);
       print "<p>Currently displaying $num_rows records.  <a href='{$_SERVER['PHP_SELF']}'>View all properties</a></p>";

             do
               { 
                 $strID=$data['property_id']; $area=$data['area_id']; $type=$data['type_id']; 

                 $areaq="SELECT * FROM prop_areas WHERE area_id='$area'";
                 $arear=mysql_query($areaq);
                 $areaData=mysql_fetch_assoc($arear);

                 $typeq="SELECT * FROM prop_types WHERE type_id='$type'";
                 $typer=mysql_query($typeq);
                 $typeData=mysql_fetch_assoc($typer);

								print "<table width='100%'><tr><td width='20%'>";
								print "<p>".$data['houseno']." ".$data['street'].", ";
								print "".$data['town'].", ";
								print "".$data['postcode']." <br />";
								if(strcmp($data['type_id'], $typeData['type_id'] ) == 0) { print "".$typeData['type'].". <br />"; }
								print "".$data['beds_id']." bedroom(s). <br />"; 
								$dbDate="SELECT DATE_FORMAT(available,'%d-%b-%Y') as f_open FROM property_info WHERE property_id='$strID'";
								$dateRes=mysql_query($dbDate);
								$date=mysql_fetch_array($dateRes); 
								print "£".$data['rent']."pcm, available from: ".$date['f_open']."<br />";
								print "Currently listed? ".$data['list']."</td><td width='20%' align='center'>";

                 $imageRes=mysql_query("SELECT * FROM images WHERE property_id='$strID' LIMIT 3");
                 $imageData=mysql_fetch_assoc($imageRes);
                 while ( is_array( $imageData ) ) 
                   { 
                     print "<a href='updateimages.php?id={$imageData['image_id']}&prop=$strID'><img src='../images/properties/".$imageData['images']."' height='50' width='50' alt='image'/></a> ";
                     $imageData = mysql_fetch_assoc( $imageRes ); 
                   } 
								print "</td><td width='20%' align='center'><a href='viewproperty.php?strID=$strID' style='text-decoration:none;'><img src='images/view.png' height='32' width='32' alt='View'/><br/>View full details</td><td width='20%' align='center'><a href='editproperty.php?strID=$strID' style='text-decoration:none;'><img src='images/edit.png' height='32' width='32' alt='Edit'/><br/>Edit this property</a></td><td width='20%' align='center'><a href='deleteproperty.php?strID=$strID' style='text-decoration:none;'><img src='images/delete.png' height='32' width='32' alt='Delete'/><br/>Delete this property</a></td></tr></table>";
               } while ($data=mysql_fetch_assoc($res) ); 
           }
  }

 

Because my if statements modify the SQL query, I need a new one every time. And that result set has to be printed out for each one, every time. Nothing changes inside the statement, so I'm looking at huge execution blocks all saying the same thing and it makes me want to weep! There is surely a better way to do this.

 

Initially I thought functions were the way to go, but functions are a pain to pass variables in and out of this way, and I've seen some people actively advise against it. I could create an include('annoyingblock.php') but that seems to me to be barking up entirely the wrong tree.

 

Can anybody point me in the direction of the right way to do this? Should I be moving into an OOP method instead? Where would I start? All I want is to be able to turn that execution block into a single-line call that I can stick into my code wherever and however often I like without having to look at a morass of repeated commands. :/

 

Apols if this is a bit of a nebulous question, but I'm very new to programming in general and I don't really have the vocabulary yet to know exactly what I'm looking for. Any advice is well-appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/114450-strategic-app-building-advice-needed/
Share on other sites

well if i understand you somewhat i would have a function that queries the database and returns the result.

 

something like:

<?php

function queryDB($select, $table, $cols, $where, $order) {

$query = "SELECT ".$select." FROM ".$table." WHERE ".$where." ORDER BY ".$order;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
return $array;
}

?>

 

so to call it, say:

</php

$result = queryDB("*", "users", "userid='".$userid."', "userid DESC");
$username = $result['username'];
?>

 

i don't know if something like that would help?

No, I think I really am going to have to go full OOP on this - it'll save a lot of headaches later with cobbling together functions, like darkwater says.

 

So, all the tutorials for OOP are ridiculously confusing and have no direct bearing on anything I'm trying to do in the real world which makes them doubly difficult to understand. I'm looking at some beginners' ones and I'm hoping to figure some stuff out shortly, but first, a quick question:

 

If I was to try to do what I want in my example above using OOP, would this pseudocode be on the right track?

class myDisplay {
var $strID = $data['property_id']; // Can I do this? assign a variable to a value obtained from a POST/GET somewhere outside of the class?
var $some other vars;

function myDisplay() { }

function showDisplay() {
print "some stuff".$this->$strID;
}
}

// and then in some other page call it with something like:

$display=new myDisplay();
$display->showDisplay(); 

 

Doesn't that just give me all the same issues with passing variables in and out of a function?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.