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;
}
Related
I am trying to fetch the data from database with compare of some fields using get_where but every time it's returning data even where condition does not satisfied...
My Code is:
function check_sec_token()
{
$sec_token=$this->session->userdata('sec_token');
$email_id=$this->session->userdata('email_id');
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
return $query->result();
if($rows<1)
{
return false;
}
else
{
return true;
}
}
if $sec_token and $email_id is null then it returns all rows from the database..i also tried
function check_sec_token()
{
$sec_token=$this->session->userdata('sec_token');
$email_id=$this->session->userdata('email_id');
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
return $this->db->last_query();
if($rows<1)
{
return false;
}
else
{
return true;
}
}
it's showing query
Select * from user_reg where sec_token=0 and email_id=0
Try this:
...
if ($sec_token == NULL && $email_id == NULL)
{
$query=$this->db->get($this->_registration);
}
else
{
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
}
...
Notice:
return $query->result();
This piece of code stops running the rest of your code! and your condition on row count is not considered!
you can do this instead:
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
if($rows<1)
{
return false;
}
else
{
return $query->result();
}
Here if $sec_token and $email_id are null or "0" then query you will fetch all records.It will give all rows because when "where condition not satisfied then it fetch all records" try it.Also check your session is start or not and your values is set or not in it properly.
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.
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
I have a class with a couple of methods
deleteUploadedFile() and currentUploadedFiles().
currentUploadedFiles(), basically loops over a session array and displays it on screen, simple as. Code sample:
function currentUploadedFiles()
{
if(isset($_SESSION['fileArray']) && $this->count > 0)
{
echo '<p style="clear:both">Current files uploaded list:</p>';
echo '<ol>';
foreach($_SESSION['fileListing'] as $key => $value )
{
echo '<li>'. $value .' [Remove File]</li>';
}
echo "</ol>\n\r";
echo "<p> Current file size allowance: ". $this->_returnRemainingSessionFileSize() ." of 8 MB";
} else {
echo '<p style="clear:both">No files have been uploaded yet</p>';
}
if($this->deleteUploadedFile() === true)
{
echo '<p>File has now been deleted from our records.</p>';
}
}
the deleteUploadedFile() method, basically when form is submitted it deletes file from the server and removes the entry from the session array. Sample code:
function deleteUploadedFile()
{
(int) $id = $_GET['id'];
(bool) $deleted = false;
if (file_exists($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$_SESSION['fileSize'] -= $this->_checkSessionFileSize($id);
if (unlink($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$deleted = true; //'<p>File has now been deleted from our records.</p>';
unset($_SESSION['fileArray'][$id]);
unset($_SESSION['fileListing'][$id]);
}
}
return $deleted;
}
my controller, basically checks if file id# isset, then checks if the array id# isset, then calls the deleteUploadedFile() method and then calls the currentUploadedFiles() method.
Question is, why when I var_dump $deleted var in deleteUploadedFile() I get bool(true) but inside the currentUploadedFiles() method I get bool(false). Sounds like I'm messing up the scope somehow?
Looks like $deleted is in the local scope of the delete function.
Something like the following should work.
class theClass
{
function __construct()
{
$this->deleted = false
}
function delete()
{
$this->deleted = true;
}
function upload()
{
var_dump($this->deleted);
}
}