I am receiving the last sort value out of 3 tables, the function works perfectly but I can't +1 the value that I return from the function. I do not get ANY errors, do you guys see what I am doing wrong?
What did I try?
Using intval in the function
Using brackets around the calculation
A lot more
Code link: http://pastecloud.net/ZDR2JgSbIN
die(lastSort());
Displays 1, or whatever the last value is.
$last = lastSort();
$new = $last+1;
die($new);
Displays a white page
What is wrong?
Referencing to this answer:
die() is the same as exit(), looking at the exit docs it takes 1
parameter, $status, the parameter information states.
...If status is an integer, that value will be used as the exit status
and not printed.
So, if you want to die() an integer, you should first convert it to a string. Like this way: die( (string)$new );.
If you are absolutely sure that the function return the correct value, you can try to convert the result to an int.
$last = intval(lastSort());
$new = $last+1;
But if lastSort returns a array (empty or non-empty), intval will return 0 or 1 respectively. Which could mess up you whole logic.
Related
This question already has an answer here:
strpos not working for a certain string
(1 answer)
Closed 1 year ago.
I'm retrieving some data from a MySQL database. For highlighting purposes, I want to modify the displayed data using PHP. A span class is added which will apply some text colouring and letter-spacing. My data is stored in $example.
This is my PHP code so far, $kw is the variable which contains the string to be replaced:
$kw = "Fun:"
if(strpos($example,$kw)) {
$example = str_replace($kw,"<span class='new'>Fun:</span>",$example);
}
This works fine for $example = "text text; Fun: text";. As for something like $example = "Fun: text";, "Fun:" is not replaced as expected, str_replace is not applied. Literally nothing happens.
When I try to replace $kw = "un:"; instead, it works fine, besides The "F" in "Fun:" is now missing the highlighting.
So if the text to be replaced is at the beginning of the search string, nothing happens. It seems that str_replace starts looking at the second character of the string instead of the beginning. I cannot figure out why.
I checked the array where the query results are stored, but found no hints which could help me solve this issue. I also printed the result from strpos for both cases. As for the first example, I got an integer > 0, for the second example the result gave 0. This seems to be fine, since in example 1 $kw is somewhere inbetween other text whereas in example 2 it is at the beginning of the line.
Did anyone ever came across this issue or am I too blind to see the solution here?
Function strpos return position of element.
If element has first position, than it's return 0 (zero).
But if statement represent zero as false. That why it's doesn't work.
Just change your if statement, like this:
//old
if(strpos($example,$kw)) {
//new
if (strpos($example,$kw) !== false) {
read more about strpos in docs
https://www.php.net/manual/ru/function.strpos.php
By the way read this
Null vs. False vs. 0 in PHP
I'm not too sure what's going on here but I'm trying to echo a value from mysql and when I do, it just shows double for some reason
Code:
$result = MySqlQuery('SELECT value FROM table WHERE id=1');
$value = mysqli_fetch_assoc($result);
echo implode($value);
It displays 7373, the value is 73 in the DB.
I also tried echoing * instead of value, it also displays the entire row double.
Removing the echo there just displays nothing anymore so it's not like it's being echoed through another function either so I'm confused
Also the MySqlQuery() function is used by pretty much everything else on the site where it doesn't display double results as well
mysqli_fetch_array returns an array with twice as many elements as the columns you select by default (each column is represented twice). I assume that the mysqli_fetch_assoc in your code is a typo.
To solve the problem, either use mysqli_fetch_assoc instead or pass one of MYSQLI_ASSOC and MYSQLI_NUM as the second parameter to mysqli_fetch_array. As a rule of thumb, use mysqli_fetch_assoc unless you know you need something else.
I have firePHP so i know exactly what the variables are, but I can't figure out why this code doesn't change it.
I receive from a mySQL call $query (which if returned produces [{"type":"2"}])
I have 4 potential types, and things can be multiple types (i.e. [{"type":"1"},{"type":"2"}])
Now I want to read this data and run various other functions based on the type it has, that is: if it's only type 2, call function TWO, if it's type 1 and 2 call function ONE and function TWO. I thought this would be easiest if i moved all the data into another array.
Here is the code I currently have:
$result = array('message'=>false, 'money'=>false, 'glasses'=>false, 'exclamation'=>false);
if (in_array('1',$query)) {$result['message'] = true;}
if (in_array('2',$query)) {$result['money'] = true;}
if (in_array('3',$query)) {$result['glasses']=true;}
if (in_array('4',$query)) {$result['exclamation']=true;}
echo json_encode($result);
This however does not update the $result array (as I can tell all of the values of $message are false in firePHP.... Thus I assume something is wrong with my if statements, but what?
I´m not sure about the value of $query, but if it is something like:
array [0 => '{"type":"2"}']
You would have to use:
in_array('{"type":"2"}',$query)
as that is the value of your variable.
Is it because the results returned in $query are arrays of arrays, and thus in_array is only searching at the top level and not sub-levels? It seems like what you want is to recursively search $query.
I have tested this on my development computer, but now I have uploaded everything to the production server and I cant read out the value of the cookie.
I think the problem lies in the Serialization and Unserialization.
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000; //20 days
$ad_arr = unserialize($_COOKIE['watched_ads']); // HERE IS THE PROBLEM
$arr_elem = count($ad_arr);
if (in_array($ad_id, $ad_arr) == FALSE){
if ($arr_elem>10){
array_shift($ad_arr);
}
$ad_arr[]=$ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
When I echo this: count($ad_arr) I receive the expected nr, 1 in this case, so there is a value there. But when I echo the value: echo $ad_arr[0]; I get nothing. Completely blank. No text at all.
Anybody have a clue?
if you need more info about something let me know...
Turns out it was stripslashes which was needed here.
Did a stripslashes() first and it worked unserializing the cookie.
You should understand that count returns 1 for most non-array values, including an empty string.
> php
<?php
echo count("");
^Z
1
So, to debug it, try var_dump'ing the $_COOKIE superglobal itself.
I would guess, that your $ad_arr is no array. You can check this with the "is_array()" function or by calling:
var_dump($ad_arr);
It sould have "array" in the output and not "string" (as Artefacto just already mentioned).
Another little tip:
If you want to store a associative array, you can use an encoded JSON String which use can save using the json_encode() gunction:
setcookie('watched_ads', json_encode($ad_arr), $expir, '/');
And loading the data you can use the opposite function json_decode():
$ad_arr = json_decode($_COOKIE['watched_ads'], true);
Adding true as a second paramter, you will get a associative array and not an object. Using the JSON format is also a good idea saving complex data within a database.
A and a last tip: "!in_array()" works just as fine as "in_array() == FALSE"
I can't explain this. I have the following:
$time += $res['timezone']; (The array equates to -5*3600 (EST))
return gmstrftime('%c',$time);
When I echo $res['timezone'], I get "-5*3600" which is correct. When I put the array value in front of the time variable, I get the incorrect time. If I comment out the array value and replace it with -5*3600, I get the correct result. Why??
because the string "-5*3600" and the expression -5*3600 aren't the same thing. You could try to put eval around the array value, like so:
$time += eval($res['timezone']); //(The array equates to -5*3600 (EST))
return gmstrftime('%c',$time);
Note that this is a very bad idea, as it is both slow and insecure. If you want to store -5*3600 in the array, then calculate the value and store the result in the array:
$res['timezone'] = -5*3600;