$taskid=$this->privacy();
if($taskid){I returning something in one function and then by reference to this return,I am fetching data from another table in second function using if condition in such a way that if data(return) found in first table then execute else leave empty
But I data found in first table then every thing is fine,hower data not found in first table then it shows as Notice: Undefined variable: task in....
I think error is in if else condition.Please help or suggest any alternative approach.
Plz also note that that I am working on pricacy table
first function returning rows
function privacy()
{
$session=new session();
$sql2=mysqli_query($this->db->connection,"SELECT * from privacy where viewerid='$session->userid'");
while($row2=mysqli_fetch_array($sql2)){
$task[]=$row2['task'];
}
return $task;
}
function 2.. if found something then do something else do nothing(but error appears as undefined variable)
function showactivityo4apply()
{
$session=new session();
$taskid=$this->privacy();
if($taskid){
foreach($taskid as $fvid){
$sql=mysqli_query($this->db->connection,"SELECT * FROM activity where id='$fvid'");
while($row=mysqli_fetch_array($sql)){
$name_id=$row['sub_id'];
echo $name;
}
}
}
else{}
}
if($taskid=$this->privacy()) is assigning a value of $this->privacy() to $taskid
I think you mean
if($taskid == $this->privacy())
If condition have == sign not = sign
if($taskid==$this->privacy()){
}
else
{
}
= means assign value to another
== check the value to other variable
it is showing notice because php is not getting value in array on key "task" use isset() function to check whether data is available or not
use like this
$task[] = (isset($row2['task'])?$row2['task']:'';
if value is not set in $row2['task'] then it will assign blank value in $task[] array.
and one more change you have to do
instead of if($taskid) condition use
if( count($taskid) > 0){
}
so that if record not found in array it will not inter in to the loop
please change this line of your code like:
$sql2=mysqli_query($this->db->connection,"SELECT * from privacy where
viewerid='$session->userid'");
=========================================
to
=============================
$sql2=mysqli_query($this->db->connection,"SELECT * from privacy where
viewerid='" . $session->userid . "'");
=======================
conclution: Here $session->userid is not found thats why you not get result.
Related
I'm attempting to edit a custom function and struggling to get the row value for a particular custom column ('rank_td') to compare against its other columns (rank_lw and rank_lm), all inside a HTML table.
Tried a fair few variations and can't get it going.
Any ideas?
function custom_value($cellValue, $dataColumnHeader, $rank_td_value) {
if($dataColumnHeader == "rank_lw" || $dataColumnHeader == 'rank_lm'){
$row['rank_td']->$cellValue = $rank_td_value;
if($rank_td_value == $cellValue){
$styleColor = 'color:blue;';
}else if($rank_td_value < $cellValue){
$styleColor = 'color:green;';
}else{
$styleColor = 'color:red;';
}
return $class_name.'<span style="'.$styleColor.'">'.$cellValue.'</span>';
}
return $cellValue; }
You are not calling the global variable $row which exists outside the scope of this function - hence my comment that it doesn't look like it belongs here. If you want to be able to access a variable from outside a function you need to either pass that variable in, or declare it using the global keyword. Here is a very basic example of this:
$row['some_value'] = "value1";
function scopeTest($var1) {
$row['some_value'] = $var1;//local variable $row created
}
function scopeTestTwo($var1) {
global $row;//variable outside the function
$row['some_value'] = $var1;
}
scopeTest("jam");
print_r($row);//Array ( [some_value] => value1 )
scopeTestTwo("jam");
print_r($row);//Array ( [some_value] => jam )
in your code you also have this
return $class_name.'<span....
but $classname is not defined so either it is redundant and you should remove it (because this will cause a php notice and anyway redundant code is, well, redundant) or it should be defined somewhere which means something has been missed out
I have 2 arrays and i want to make a 3rd array after comparison of the 2 arrays. Code is as follows:
foreach($allrsltntcatg as $alltests)
{
foreach($alltests as $test)
{
foreach($allCatgs as $catg)
{
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
$catcounts[$catg['testcategoryname']] +=1;
}
}
}
}
It, although returns the right answer, it also generates a PHP error and says undefined index and prints all errors and also the right answer.
I just want to avoid the array out of bound error. Kindly help me
Problem is in if condition correct like below : You have to initialize array first and than you can increment value
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
if (isset($catcounts[$catg['testcategoryname']]))
$catcounts[$catg['testcategoryname']] +=1;
else
$catcounts[$catg['testcategoryname']] =1;
}
When the array try to add some arithmetic operation of undefined index such as $catg['testcategoryname'] in the $catcounts array then the warning generates. Before add the number you have to check the index is present or not, and of not then just assign value otherwise add into it.
So do it in this way just if condition-
if(....){
if(array_key_exists($catg['testcategoryname'], $catcounts))
$catcounts[$catg['testcategoryname']] +=1; // Add into it
else
$catcounts[$catg['testcategoryname']] = 1; // Assign only
}
More about array key exists--See more
$catg['testcategoryname'] should represent an index in $catcounts array.
I'm trying to create a method that counts the empty mysql columns.
My code is as follows:
public function countCompletion() {
$userData = $this->find(Session::get('user'));
$userData = $this->_data;
$completion = 0;
foreach($userData as $item) {
if(empty($item)) {
$completion++;
}
}
die($completion);
}
The problem is, when I die $completion it just shows nothing, while it should show 2.
According to the documentation, when die (or exit) is called with an integer argument the argument is not printed but is instead passed back to the operating system as an exit code. To get the value to display you will need to return it, then print it from the calling routine.
first check with "echo" inside "foreach" to display column content then you can do a loop to incremente number of empty column
foreach($userData as $item) {
echo 'data: '.$item.'<br>';
if(empty($item)) {
$completion++;
}
}
bacause you have to verify that your function is executing successfully
you can use "var_dump()" also to test what variable contains
var_dump($completion);
die();
I created a function that looks for all rows (records) in a specific column,
generates a random number and then checks if it already exists withing that column, if the generated random number exists restart the function (recursive)
if it doesn't exist then insert it as new.
$verifs = mysqli_query($conlink, 'SELECT verif FROM memebers');
$records = array();
while($record = mysqli_fetch_array($verifs,MYSQLI_NUM)) {
$records[] = $record[0];
}
function randomize(){
$rand= rand(1,5);
if(in_array($rand, $GLOBALS['records'])){
randomize();
}else{
return $rand;
}
}
mysqli_query($conlink, "INSERT INTO `memebers`(`verif`) VALUES (".randomize().")");
i expected that a number MUST be added EACH time i refresh my php page
and it was the case, but when i reach 3 or 4 records, refreshing the page doesn't add any ! so the return statement must prevent the function from being recursive.
am i having anything wrong with my logic
thanks
It appears to me that you are not using the callback function correctly. If you didn't know, when you use a function within a function, you actually have to pass that function inside the parameters. Functions that have a function in the parameter are called callback functions and you can read more about them in the following post: What is a callback function and how do I use it with OOP
I would then change your randomize function to the following code:
function randomize($callbackRandomize){
$rand= rand(1,5);
if(in_array($rand, $GLOBALS['records'])){
return $callbackRandomize($callbackRandomize);
}else{
return $rand;
}
}
and also change your last line of code to:
mysqli_query($conlink, "INSERT INTO `memebers`(`verif`) VALUES (".randomize('randomize').")");
Let me know if that worked for you.
I'm trying to pass a value between 2 pages through a $_SESSION variable then empty it as follows:
I'm assigning the session variable with a value on one PHP page:
$_SESSION["elementName"]="a_373";
And trying to store it in a variable on another page as follows:
if (!empty($_SESSION["elementName"])) {
$elemName=$_SESSION["elementName"];
$_SESSION["elementName"]="";
} else {
$elemName="";
}
The value of $elemName is always empty when I print it out. However, I get the correct printout when I remove the $_SESSION["elementName"]=""; line from the above code.
Edit: I'm printing $elemName and not $_SESSION["elementName"] - print($elemName);
I'm on a shared hosting account with PHP 5.3.2 and register_globals set to off (as per phpinfo();).
I need to reset/empty the session variable once I get the value it has, but it's not working and this has been baffling me for the last couple of days. Any ideas why? Thanks!
EDIT:
Additional clues: I tested with the session's var_dump before the if statement and set another value for $elemName in the else section as follows:
var_dump($_SESSION["elementName"]);
$elemName="x";
if (isset($_SESSION["elementName"]) && !empty($_SESSION["elementName"])) {
$elemName=$_SESSION["elementName"];
$_SESSION["elementName"]="";
} else {
$elemName="None";
}
print("<br />".$elemName);
I got this result:
string(5) "a_373"
None
Try using isset($_SESSION["elementName"]) and unset($_SESSION["elementName"]) instead.
Check the Below code and test it
if (isset($_SESSION["elementName"]) && $_SESSION["elementName"]!="") {
$elemName=$_SESSION["elementName"];
$_SESSION["elementName"]="";
} else {
$elemName="";
}
Empty only check value is empty or not, But by isset we can check varaible exit and does not content empty value
Are you printing out $elemName? If yes than there shouldn't be any blank output unless and until your else condition returns true, but if you are printing $_SESSION["elementName"] than you'll get no output as you are making it blank
$_SESSION["elementName"]="";
Correct way to remove a session var completely is to use unset($_SESSION["elementName"])
Though if you want to make it empty, == '' is enough
Also be cautious while using unset() because after you unset the session and later use that index to print, it will show you undefined index error.
Update(As #nvanesch Commented)
I guess your else condition is getting satisfied, because you are
using $elemName=""; in your else, thus you are not returned with any
output
Also if (!empty($_SESSION["elementName"])) { will return false if you are not using session_start() at the very top of your page
I once created this class in order to be able to have this ability, and it works wonderfully:
<?php
class Flash {
public function set($key, $message) {
$_SESSION["flash_$key"] = $message;
}
public function get($key) {
$message = $_SESSION["flash_$key"];
unset($_SESSION["flash_$key"]);
return $message;
}
public function has($key) {
return isset($_SESSION["flash_$key"]);
}
}
It's pretty similar to what you're trying to do, so I'm not sure why it's not working for you, but you may want to give it a try. You obviously need to have the session already started.