Please check this below code. After for loop remaining code is not executing. It suppose to print "Helo", but it is not printing any thing.
for($i=0;$i<10;$i++)
{
$minrate=$obj_iScripts->min_avg_rate($roomnumber[$id_array[$i]], $amount_ary[$id_array[$i]], $totalrooms);
$all_min_price[]=$minrate;
if($_SESSION['star'][$id_array[$i]]>=1 && $_SESSION['star'][$id_array[$i]]<=5)
{
//include 'searchresult_table.php';
}
}
echo "Helo";
code:
public function min_avg_rate($roomnumber,$rates,$totalrooms)
{
$ary_name='iArray';
$total=0;
for($i=1;$i<=$totalrooms;$i++)
{
${$ary_name.$i}=array();
$temp=max($rates);
for($j=0;$j<count($roomnumber);$j++)
{
if($roomnumber[$j]==$i)
{
if($temp>$rates[$j])
$temp=$rates[$j];
${$ary_name.$i}=$temp;
}
}
$total=$total+${$ary_name.$i};
}
return $total/$totalrooms;
}
From what code you have posted -the min_avg_rate() function within your class would seem to be malfunctioning
Related
I am getting null value for function getRecommendation($matrix,$getName), the data obtained are working fine
still learining php
<?php
require('connection.inc.php');
require('recommend.php');
$userID=$_SESSION['USER_ID'];
$reco=mysqli_query($con,"select userrating.*,product.id,product.productName from userrating,product where userrating.userID='$userID' and product.id=userrating.productID");
$matrix=array();
while($rec=mysqli_fetch_array($reco))
{
$users=mysqli_query($con,"select users.name from users where users.id=$rec[userID]");
$username=mysqli_fetch_array($users);
$matrix[$username['name']][$rec['productName']]=$rec['rating'];
$getName= $username['name'];
}
var_dump(getRecommendation($matrix,$getName));
?>
Here is the function code as well, it is for a recommendor system , fur now i jst need this code not to return null value
$value)
{
if(array_key_exists($key,$matrix[$product2]))
{
$similar[$key]=1;
}
}
if($similar==0)
{
return 0;
}
foreach($matrix[$product1] as $key=>$value)
{
if(array_key_exists($key,$matrix[$product2]))
{
$sum=$sum+pow($value - $matrix[$product2][$key],2);
}
}
return 1/(1+sqrt($sum));
}
function getRecommendation($matrix,$prod)
{
foreach($matrix as $otherProduct=>$value)
{
if($otherProduct!=$prod){
$sim=similarityDistance($matrix,$prod,$otherProduct);
var_dump($sim);
}
}
}
?>
You have to actually return something from the function getRecommendation. (That's why you get NULL now. The functions isn't returning anything).
function getRecommendation($matrix,$prod)
{
foreach($matrix as $otherProduct=>$value)
{
if($otherProduct!=$prod){
$sim=similarityDistance($matrix,$prod,$otherProduct);
var_dump($sim);
}
}
//I don't what you want to return. Maybe this, but it has to be something..
return $sim;
}
I have a page whit ads, and i set the page currency in "RON" and i convert to show also in "Euro" but in the loop is very slow.. I tried to include the script form other php but stil the same... I tried many currency changer but all have the same problem.. slow the page down.. and if i put the code directly in to the loop then tells me an error: that the class could not be repeated.
here is the php currency what i used:
<?php
class cursBnrXML
{
var $currency = array();
function cursBnrXML($url)
{
$this->xmlDocument = file_get_contents($url);
$this->parseXMLDocument();
}
function parseXMLDocument()
{
$xml = new SimpleXMLElement($this->xmlDocument);
$this->date=$xml->Header->PublishingDate;
foreach($xml->Body->Cube->Rate as $line)
{
$this->currency[]=array("name"=>$line["currency"], "value"=>$line, "multiplier"=>$line["multiplier"]);
}
}
function getCurs($currency)
{
foreach($this->currency as $line)
{
if($line["name"]==$currency)
{
return $line["value"];
}
}
return "Incorrect currency!";
}
}
//#an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
?>
You can modify the cursBnrXML class to cache parsed currency so that you do not have to loop over entire collection again each look up.
<?php
class cursBnrXML
{
private $_currency = array();
# keep the constructor the same
# modify this method
function parseXMLDocument()
{
$xml = new SimpleXMLElement($this->xmlDocument);
$this->date=$xml->Header->PublishingDate;
foreach($xml->Body->Cube->Rate as $line)
{
$this->currency[$line["currency"]]=array(
"value"=>$line,
"multiplier"=>$line["multiplier"]
);
}
}
# modify this method too
function getCurs($currency)
{
if (isset($this->_currency[$currency]))
{
return $this->_currency[$currency]['value'];
}
return "Incorrect currency!";
}
}
//#an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
?>
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 ;-)
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>";
}
}
?>
I obtain data from the model in an array format, I would like to know why I can't print_r the result without using exit() function right after it ?
this works:
function someview()
{
$result=$this->User->getdatafrommodel();
print_r($result);
exit();
if(!empty($result))
{
//do something
}
else
{
$this->redirect(array('action'=>'usernotexist'));
}
}
function usernotexist()
{
$this->loadSkin();
}
this prints an empty Array.
function someview()
{
$result=$this->User->getdatafrommodel();
print_r($result);
if(!empty($result))
{
//do something
}
else
{
$this->redirect(array('action'=>'usernotexist'));
}
}
function usernotexist()
{
$this->loadSkin();
}
Could someone tell me why so it is ?
In the second block of code, as results is empty this line is called in the else block:
$this->redirect(array('action'=>'usernotexist'));
So your output would appear to be a different page without the exit() statement.