I have one not understood point In MVC pattern. Please help understood.
for example we have table for cars in database, we want obtain and print results from table, but if results are not found (0 rows), in this case print: "We dont have results"
this is models.php
class modesl {
function getCars () {
$res = $this->db->query("SELECT names FROM cars");
if ($res->num_rows == 0) {
return "We dont have results";
}
else {
return $res;
}
}
}
this is views.php
class views {
function loadHTML ($resultFromCars) {
require 'carspage.php';
}
}
this is carspage.php
<html>
<body>
<?php
if (is_object($resultFromCars)) {
while ($row = $resultFromCars->fetch_assoc()) {
echo $row['names']."<br>";
}
}
else {
echo $resultFromCars;
}
?>
</body>
</html>
this is controllers.php
class controllers {
function generatePage () {
$model = new models();
$resultFromCars = $model->getCars();
$view = new views();
$view->loadHTML($resultFromCars);
}
}
This works, but as I know, many php code in view, (that is condition if (is_object) { } else { } ) is not right MVC. tell please for this concret case, what must be change in my architecture (lol), for obtain right MVC concept?
I like the answer provided by Havelock.
I would adjust this even further, by making sure your model already returns the data in an array format (or false, if nothing is found). Therefore, the logic for extracting data from resultset stays in the model, where it really should be.
Your view becomes even simpler then:
<?php
if (!empty($results)) {
foreach ($results as $row) {
echo $row['name'] . "<br />";
}
} else {
echo "Eh, Nothing found...";
}
You seem to have done a good job, just one small thing to improve. As the model is a wrapper for data only, so you should return only data (and no strings, containing error/exception messages). In the case there's no data to return, then return FALSE, as it's done in PHP.
class CarModel {
function getCars () {
$res = $this->db->query("SELECT names FROM cars");
if ($res->num_rows == 0) {
return FALSE; // if that happens, the function will stop execution here, so no "else" is needed
}
return $res;
}
}
And in your view
<?php
if ($resultFromCars === FALSE && !empty($resultFromCars)) {
echo "We don't have results";
}
else { // now you know it's not FALSE, so it must be an object, no need to check whether it is one
while ($row = $resultFromCars->fetch_assoc()) {
echo $row['names']."<br>";
}
}
?>
Related
I come across this problem all the time: you have a series of if/else statements, where the intended result can come from two completely different branches, and you end up duplicating the code across both. Here's an example:
if (condition1) {
return error
} else {
if (condition2) {
return intendedResult
} else {
if (condition3) {
return error
} else {
return intendedResult
}
}
}
I have this problem in a PHP script at the moment, where the intended result is approximately 200 lines of PHP queries, HTML and additional if/else statements. Is this the prime example of when you should use a function? And return all that code in a concatenated string?
EDIT:
Thanks for all your replies. I can see that my logic would need tidying up. If I gave you a real-world example, could you help me restructure my logic? I think it's a little more complicated than my pseudo-code.
I pull two dates, $lift_deadline and $show_guide_close from a database, and depending on today's date's relation to those, do the following:
if ($lift_deadline > $today) {
echo "No access until " . $lift_deadline;
} else {
if ($lift_deadline !== "0000-00-00") {
if ($today > $show_guide_close) {
$use_date = $show_guide_close;
} else {
$use_date = $lift_deadline;
}
echo "HTML Table"; // Intended result goes here, using $use_date
} else {
if ($show_guide_close > $today) {
echo "No access until " . $show_guide_close;
} else {
$use_date = $show_guide_close;
echo "HTML Table"; // Intended result also goes here
}
}
}
Well part of the solution is certainly making intendedResult code into a separate method, but just reoganizing the conditions can help:
if ($condition1) {
return 'error';
}
if (!$condition2 && $condition3) {
return 'error';
}
return $this->intendedResult();
This could be further simplified to:
return ($condition1 || $condition3) ? 'error' : $this->intendedResult();
But that might not be as readable, and doesnt really illustrate my point, which is as Tom mentions, to flatten your control structure
Yes, functions are one way to simplify code and make it easier to read. It might be also worth looking into classes and exceptions.
In simple instances I use a switch statement for stuff like this
switch(true)
{
case (condition1):
break;
case (condition2):
break;
default:
}
I have a PHP script that executes another PHP script(that contains quite a lot of functions some being recursive) using the include method , multiple times, but the second time i get a error saying that one of the function in the "included" PHP script can not be redeclared.
Now i get that what include does is just inserting the commands from the referenced script in the script that called it and thus I have like the same functions declare too many times resulting in a abomination.
So, could somebody tell me how i should approach this problem?
This is the included script
<?php
$_SESSION['det']=0;
$x=array();
function valid($k)
{
for($i=1;$i<$k;$i++)
if($GLOBALS['x'][$i]==$GLOBALS['x'][$k])
return 0;
return 1;
}
function semn($k)
{
$nr=0;
for($i=1;$i<$k;$i++)
for($j=$i+1;$j<=$k;$j++)
if($GLOBALS['x'][$i]>$GLOBALS['x'][$j])
$nr++;
if($nr%2==0)
return 1;
else
return -1;
}
function determinant($k)
{
$prod=1;
for($i=0;$i<$k;$i++)
$prod*=$_SESSION['matrix'][$i][$GLOBALS['x'][$i+1]-1];
$_SESSION['det']+=semn($k)*$prod;
}
function solve($k,$n)
{
for($i=1;$i<=$n;$i++)
{
$GLOBALS['x'][$k]=$i;
if(valid($k))
if($k==$n)
{
determinant($k);
}
else
solve($k+1,$n);
}
}
$n=$_SESSION['size'];
solve(1,$n);
unset($x);
?>
And this is the script thats includes.
<?php
include 'determinant.php';
if(!$_SESSION['det'])
{
echo "The inverse cant be calculated cause the determinant is equla to 0.";
}
else
{
$detA=$_SESSION['det'];
//Transpusa
for($i=0;$i<$_SESSION['size']-1;$i++)
for($j=$i+1;$j<$_SESSION['size'];$j++)
{
$aux=$_SESSION['matrix'][$i][$j];
$_SESSION['matrix'][$i][$j]=$_SESSION['matrix'][$j][$i];
$_SESSION['matrix'][$j][$i]=$aux;
}
$Dcar=array();
//Matricile caracteristice
for($i=0;$i<$_SESSION['size'];$i++)
{
for($j=0;$j<$_SESSION['size'];$j++)
{
$r=0;
$c=0;
$semn=1;
$a=array();
$_SESSION['matrix'][$i][$j]
for($m=0;$m<$_SESSION['size'];$m++)
{
if($m==$i)
continue;
else
{
for($n=0;$n<$_SESSION['size'];$n++)
{
if($n==$j)
continue;
else
{
$a[$r][$c]=$_SESSION['matrix'][$m][$n];
$c++;
}
}
$r++;
$c=0;
}
}
//Apelarea functiei determinant pentru fiecare matrice
$aux=$_SESSION['matrix'];
$_SESSION['matrix']=$a;
$_SESSION['size']-=1;
include 'determinant.php';
$_SESSION['matrix']=$aux;
$_SESSION['size']+=1;
$Dcar[$i][$j]=($semn*$_SESSION['det'])/$detA;
$semn*=-1;
}
}
for($i=0;$i<$_SESSION['size'];$i++)
{
for($j=0;$j<$_SESSION['size'];$j++)
echo $Dcar[$i][$j]." ";
echo "<br>";
}
}
?>
Use include_once() function.
Follow documentation:
This is a behavior similar to the include statement, with the only
difference being that if the code from a file has already been
included, it will not be included again. As the name suggests, it will
be included just once.
You can use require_once() instead. It's all in the name in this case ;-)
So, I'm working on a dynamic forum signatures script and I alredy got it working. Now I will to make it so that only a specific user group can a specific design.
This is the function I made.
function userGroup($rank)
{
if ($rank == 38)
{
return true;
}
else
{
return false;
}
}
And used it like this.
if ($userGroup == true)
{
...
}
else echo('User with that ID doesn\'t exist in the database');
}
else echo('This user hasn\'t in the specific user group');
But it wont work like that.
Regards,
Lazar!
You made a function function userGroup($rank) and you are trying to call it as if its a variable $userGroup. What happened to the parameter? Your if-statement should be something like:
if (userGroup($var) == true) { ... }
You don't actually need the == true either:
if (userGroup($var)) { ... }
you can not use a function like this. you should have a function call with valid argument like this:
if (userGroup($intUserRank))
{
...
}
else echo('User with that ID doesn\'t exist in the database');
assume $intUserRank containing user rank.
You function could just be like this:
function userGroup($rank)
{
return ($rank == 38);
}
Then you would use it like this:
if (userGroup($some_rank))
{
...
}
So here is my case:
I have three functions performing some chemical reactions(synthesis1(), synthesis2() & synthesis3() ).
All of these functions will give an answer or a fail in the results.
They were originally separate scripts but are now in a class.
NB: the functions work fine by themselves, even in the class.
Below is my script to instantiate the class and start the functions.
My problem is that since i am running a reaction which fires all the functions;
i get one 1 correct answer and two fails or three fail at once.
What is the best way to handle the situation.
I want one correct answer and suppress the two fails or just show one fail in case of three fails(all fails). I don't expect three right answers.
P.s. All answers are strings.
<?php
// create an object for class name
$aaa = new synthesis();
$abc = new synthesis();
$abcd = new synthesis();
// call the functions in the class
$synthesis1 = $aaa->synthesis1();
$synthesis2 = $abc->synthesis2();
$synthesis3 = $abcd->synthesis3();
// call the if functions
$searches = array($synthesis1, $synthesis2, $synthesis3);
foreach($searches as $search) {
if ($aaa->synthesis1($search)){
echo 'Match found: ' . $search;
break;
}
elseif ($abc->synthesis2($search)){
echo 'Match found: ' . $search;
break;
}
elseif ($abcd->synthesis3($search)){
echo 'Match found: ' . $search;
break;
}
else{ echo"Please try again or try another reaction";}
}
?>
I don't know why you need to instantiate three different objects if you have three individually named methods.
I would think you might want to add a method to your class to simply run all synthesis methods all at once and return the result. So something like:
class synthesis {
protected $synthesis_methods = array(
'synthesis1',
'synthesis2',
'synthesis3',
// add more methods here if needed
}
public function synthesis1() {
// your method logic here
}
public function synthesis2() {
// your method logic here
}
public function synthesis2() {
// your method logic here
}
public function synthesize_all() {
$result = false;
$i = 0;
while(false === $result && $i < count($this->synthesis_methods)) {
$result = call_user_func(array($this, $this->synthesis_methods[$i]));
$i++;
}
return $result;
}
}
You would then only instantiate a single object. Usage would be:
$synth_obj = new synthesis();
var_dump($synth_obj->synthesize_all());
An easy way to handle this is to use OR logic:
if($aaa->synthesis1($search) or $abc->synthesis2($search) or $abcd->synthesis3($search))
{
echo "Match Found: $search";
break;
}
else
{
echo "Please try again or try another reaction.";
}
Inherited an old CakePHP site and I'm trying to figure out what some functions do. I have several functions that have the same name as another function but with an underscore first, e.g. save() and _save(). However the function _save() is never called in any context, though save() is.
I read this question and it looks like it's from an old worst-practices exercise, but that doesn't really explain why it's in my code; you still have to call function _save() as _save() right? If there's no calls to _save() is it safe to remove?
I want it gone, even the save() function wasn't supposed to be there, rewriting perfectly good framework functionality. It looks like an older version of the same function, but there's no comments and I don't know if there's some weird context in which php/Cake will fall back to the underscored function name.
Here's the code for the curious. On closer inspection it appears the underscored functions were old versions of a function left in for some reason. At least one was a "private" method being called (from a public function of the same name, minus the underscore...):
function __save() {
$user = $this->redirectWithoutPermission('product.manage','/',true);
if ($this->data) {
$this->Prod->data = $this->data;
$saved_okay = false;
if ($this->Prod->validates()) {
if ($this->Prod->save()) $saved_okay = true;
}
if ($saved_okay) {
$product_id = ($this->data['Prod']['id']) ? $this->data['Prod']['id'] : $this->Prod->getLastInsertId();
if ($this->data['Plant']['id']) {
$this->data['Prod']['id'] = $product_id;
$this->Prod->data = $this->data;
$this->Prod->save_plants();
$this->redirect('/plant/products/'.$this->data['Plant']['id']);
} else {
$this->redirect('/product/view/'.$product_id);
}
die();
} else {
die('did not save properly');
}
} else {
die('whoops');
}
}
function save() {
$user = $this->redirectWithoutPermission('product.manage','/products',true);
if ($this->data) {
$this->Prod->data = $this->data;
if ($this->Prod->validates()) {
$this->Prod->save();
$gotoURL = isset($this->data['Navigation']['goto'])?$this->data['Navigation']['goto']:'/';
$gotoURL = str_replace('%%Prod.id%%', $this->data['Prod']['id'], $gotoURL);
if (isset($this->data['Navigation']['flash'])) {
$this->Session->setFlash($this->data['Navigation']['flash']);
}
if (isset($this->params['url']['ext']) && $this->params['url']['ext']=='ajax') {
$value = array(
'success'=>true
,'redirect'=>$gotoURL
);
print $this->Json->encode($value);
} else {
$this->redirect($gotoURL);
}
} else {
$value = array(
'success'=>false
,'message'=>"You have invalid fields."
,'reason'=>'invalid_fields'
,'fields'=>array(
'Prod'=>$this->Prod->invalidFields()
)
);
print $this->Json->encode($value);
}
} else {
$this->redirect('/products');
}
die();
}
I had hoped to learn whether or not some convention applied to this situation, but from testing I've found the functions are not called which is really the answer to the question I asked.