I have an array of CLOB datatypes. In my sample data, there are two items in the array. The array is embedded in a FOR loop that will loop until all of the data is read from the array (in this case, twice). I have a function that reads the CLOB data into a string, then returns the string, literally just:
function getCLOB($clob)
{
$str = $clob->read($clob->size());
return $str;
}
My problem is that it reads in the first thing I send it fine and I can get that to display back in the FOR loop. However, when I send the second one, the string is empty. I put in some echos to see what was happening with the data and the $clob variable has data once it's in the function, but the $str variable is empty after the first line of the function. Does anyone have any ideas on why it works once but not the second time?
Related
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php
I have a 15,000 row PHP array. I need to iterate through each row to generate a 15,000 row Javascript array. Each row of the PHP array has a 5% chance of containing one or more HTML special characters like ó that I need to replace with the equivalent javascript hex. There are about 50 HTML special characters I have to look out for and replace, so I'd use str_replace(array_of_HTML_targets, array_of_hex_replacements, haystack). Is it more efficient to:
Go through each line of the PHP array, search for an ampersand, and if one exists do the search and replace (considering this will apply for only 5% of the rows)
Execute the search and replace on the entire array
Concatenate the array into one giant string and execute the search and replace on the giant string
Other idea? Please specify
Btw, reason for 15,000 PHP array is this is a data visualization app.
Since you already need to dump your PHP data into a string (probably JSON), you might as well work on the final string, like so:
$json = json_encode($your_php_array);
$unhtmlref = preg_replace_callback("/&#(x[0-9a-f]+|\d+);/",function($m) {
if( $m[1][0] == "x") $m[1] = substr($m[1],1);
else $m[1] = dechex($m[1]);
return sprintf("\\u%04s",$m[1]);
},$json);
This is safe, because HTML character codes don't have any special meaning in a JSON string.
That said, I have a function in my JavaScript "utility belt" that does something similar:
function unHTMLref(str) {
// take a string and return it, with all HTML character codes parsed
var div = document.createElement('div');
div.innerHTML = str.replace(/</g,"<");
return div.firstChild.nodeValue;
}
So basically you can either parse before, or after. Personally, I'd prefer "after" because it shifts some of the "grunt" work to the browser, allowing the server to do more important things.
$("#doStatus").click(function()
{
var serialized = $("#formStatus").serialize()
$.get("<?echo $site['url'];?>modules/yobilab/tuitting_core/classes/doStatusBox.php", serialized);
window.setTimeout('location.reload()', 1000);
return false;
});
This code will return something like this:
tuitting=test&f%5B%5D=2&f%5B%5D=4&f%5B%5D=8&t%5B%5D=2&t%5B%5D=3&t%5B%5D=4
Where tuitting(the first value) is the value of the textarea, then we have:
-f this is the name of somecheckboxes that have the same names but different values
-t other checkboxes, same names, different values
As you may see the serialized function for "f" and "t" returns strange values, when they should only be simple number, like tuitting=test&f=1&f=3&t=9&t=10
Now when the ajax call the doStatusBox.php file nothing happens.
The doStatusBox.php file is made by a simple foreach php loop.
It will take the below variables:
$_GET['tuitting']
$_GET['t']
$_GET['f']
Foreach `$_GET['t'] and $_GET['f'] the loop will insert the relative values into the database.
This is not working at all. The php foreach can not recognize the results given by the jQuery serialize function and does not do simply anything.
What is the problem?
I should use another function instead of foreach?
Or the problem is in the jQuery serialized function?
Please help me.
`
Since we cannot see your HTML code, I can't say for sure what the problem is, but I suspect it's one of two problems:
(most likely) - your checkbox fields need to have a [] after the name. Example: name="t[]". If all of your checkboxes are name="t", then when multiple are posted, PHP will only see one of them. However, if you put brackets at the end of the name PHP will recognize the collection of values as an array that you can loop through.
(less likely) - the PHP script that receives this data should run urldecode() on the data. Example: $tuitting = urldecode($_GET['tuitting']);
Hope this helps!
Look at the following code:
foreach($_SESSION['cart'] as $r => $stringrow) {
$rowarray=explode("#",$stringrow);
}
$_SESSION['cart'] contains the row of concatenated values the separator is "#". Which I got from the previous page (as you can see they are inserted in a session.) I am exploding $stringrow to get an array called $rowarray. Everything is fine till here. The problem is this $rowarray[] contains two array elements, each of which contain concatenated imageurls separated by "#". The concatenated image urls are in $rowarray[11] and $rowarray[12]. I am again exploding $rowarray[11] and $rowarray[12]. trying to access each imageurl. But it's not happening. till now I have used innumerable echos to see the content of the imageurl array but every time it's showing 'Array'. as the value.
To view the contents of arrays, try using either var_dump($array) or print_r($array). They'll show you the type and contents of whatever variable you feed to them. It makes debugging oddly nested arrays much easier.