I am learning php. I have some code where I am trying to post 2 variables and 2 arrays from one php page to another page, the recieving page works fine but the first page is cropping data after a few characters (it hasnt reached max lenght or anywhere close)-
Here, $array_name and $array_qty are two dynamic arrays. I have verified that echo $c gives me exactly what I want.
<?php
$serialized_name=serialize($array_name);
$serialized_qty=serialize($array_qty) ;
$c="count=".$count ."&&Sum=" . $a . "&&serialized_name=". $serialized_name . "&&serialized_qty=". $serialized_qty;
echo $c;
?>
echo $c gives me-
count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:"vanilla";i:1;s:7:"vanilla";i:2;s:21:"very berry strawberry";i:3;s:7:"vanilla";i:4;s:7:"vanilla";i:5;s:7:"vanilla";i:6;s:7:"vanilla";}&&serialized_qty=a:7:{i:0;s:1:"2";i:1;s:1:"1";i:2;s:1:"1";i:3;s:1:"1";i:4;s:1:"1";i:5;s:1:"3";i:6;s:1:"3";}
However, this gives me cropped output of $c=
<p><a href="Checkout.php?<?php echo $c ?>" >Checkout</a> </p>
The cropped output that i get from the above line is-
http://localhost/myRestaurant/Checkout.php?count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:
I think I should have gotten this-
http://localhost/myRestaurant/Checkout.php?count=6&&Sum=45.91&&serialized_name=a:7:{i:0;s:7:%22vanilla%22;i:1;s:7:%22vanilla%22;i:2;s:21:%22very%20berry%20strawberry%22;i:3;s:7:%22vanilla%22;i:4;s:7:%22vanilla%22;i:5;s:7:%22vanilla%22;i:6;s:7:%22vanilla%22;}&&serialized_qty=a:7:{i:0;s:1:%222%22;i:1;s:1:%221%22;i:2;s:1:%221%22;i:3;s:1:%221%22;i:4;s:1:%221%22;i:5;s:1:%223%22;i:6;s:1:%223%22;}
I know get is not the best most secure way but I think this should have worked. Any tips on what I am doing wrong and how to fix it will be appreciated.
Your quotation mark will end the attribute prematurely. Escape your quotation marks:
<?php echo htmlentities($c); ?>
You should use POST for that kind of shopping cart product, but anyway, if you want to just create a URL out of your arrays, you can't just serialize arbitrary data, you'll need to encode the data or it will break because of special chars.
Just use for encoding:
$serialized_name = base64_encode(serialize($array_name));
$serialized_qty = base64_encode(serialize($array_qty));
For retrieveing the data, in Checkout.php, do the opposite:
$unserialized_name = unserialize(base64_decode($_GET["serialized_name"]));
$unserialized_qty = unserialize(base64_decode($_GET["serialized_qty"]));
Related
I am attempting to make a gallery that calls the image names from a flat file database using the PHP 'fgets' function. There are different sections in the gallery, each with it's own default image, and a small list of images that the users can select from. Everything is working fine, except for one button.
I have one button on the page that is supposed to reset all the galleries to their default images using Javascript OnClick. It works exactly as I want it to, with one small hitch: It copies the line break at the end of the line allong with the characters on the line, breaking the Javascript.
The offending code:
function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
//This should output the proper javascript, but does not
<?php
$a = fopen('c.txt','r');
if (!$a) {echo 'ERROR: Unable to open file.'; exit;}
$b = fgets($a);
echo "document.getElementById('i1').src='$b';";
fclose($a);
?>
}
How it outputs:
function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
document.getElementById('i1').src='00.jpg
';}
As you can see, the ending quotation mark and the semi-colon falls on the next line, and this breaks the button.
With the files I'm using now, I can get around this problem by changing, "fgets($a)" to, "fgets($a, 7)" but I need to have it grab the entire line so that if the client decides to enter a file with a longer name, it does not break the gallery on them.
Use rtrim().
Specifically:
rtrim($var, "\r\n");
(To avoid trimming other characters, pass in just newline.)
Your best bet is to use the php trim() function. See http://php.net/manual/en/function.trim.php
$b = trim(fgets($a));
$b = fgets($a);
$b = preg_replace("/[\n|\r]/",'',$b);
I have a URL that contains a department name that will pull records from the database.
The URL looks like this: submissions.php?department=Settings,%20Security%20&%20Payments which is the equivalent of Settings, Security & Payments
My query needs to pull records from the table where the department is equal to Settings, Security & Payments.
How can convert that GET variable back to what I would expect it to be?
I tried html_entity_decode but it ignores the & and only gave me everything prior to that.
Whats the best way to do that?
Side note, if it was my data I would make it simple and pull it by ID but we dont have a table that has ID's for the departments.
Try urldecode()
You can see the manual here. http://uk3.php.net/urldecode
<?php
$string = "submissions.php?department=Settings,%20Security%20&%20Payments";
$decoded = urldecode($string);
echo "Original string: $string\n";
echo "Decoded string: $decoded\n";
?>
http://codepad.org/Bq1Gt30s
Use urldecode($your_URLstring)
I'm trying to mix <?php echo do_shortcode('[...]') with a field from Advanced Custom Fields within Wordpress.
So basically what I'm trying to do is give the user a text field in the page edit screen where she can paste in the ID of a youtube vide. This field will then update my do_shortcode to display the correct video.
I'm not sure what I'm doing wrong considering I've done this several times before and been succesful. I do have a feeling I'm not escaping the string correctly?
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . the_field("youtube_video") . '" width="640" height="480" anchor="Play Video"]'); ?>
Anyone able to lead me in the right direction? :)
EDIT
The code above returns q_cHD1lcEOo with multiple spaces in front of it as well as this: "Error! You must specify a value for the Video ID, Width, Height parameters to use this shortcode!" That's why I was thinking I'm not escaping it correctly as these are all specified.
I'll add that if I remove the_field("...") and replace it with just an ID it displays perfectly.
SECOND EDIT
Since I was not supposed to echo it, I was using the wrong function to get the field. Instead of using the_field() which prints the value, I was supposed to use get_field() to simply return it to the string.
Your question is somewhat unclear, but I'm also 20 hours without sleep.
Anyways, as far as mixing PHP within a PHP string, there's numerous ways to do it..
You can use concatenation or { } within the string itself.
For example, say we want to echo out the property of an object within a string.
We could do the following
echo "This is my property " . $object->property;
Or, we can do this
echo "This is my property {$object->property}";
You can even do cool things like access associative arrays within strings like so
echo "This is my property {$object->property['cool']}";
Hopefully this leads you in the ride direction.
At first glance it looks like you should be using get_field instead of the_field. the_field will print without being prompted, whereas get_field will return its value, which is what you want.
I see you've also mentioned whitespace at the start, you should consider wrapping the function in trim.
See below:
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . trim(get_field("youtube_video")) . '" width="640" height="480" anchor="Play Video"]'); ?>
So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php
i have a wordpress plugin that sends a request using file_get_contents() to a url and in-turn receive an image and four variables which ofcoz are already processed so they are just four words. How to i break down this string (using php) so i can take turn the four words into variable again as soon as they are returned, and use them on that page, (page C. )
here is a diagram of what i want to do
http://itmastersworld.com/my_problem.jpg
I have tried placing a form to be part of the string returned but apparently there is no way of manupilating the data inside the form
<form><input id="test" name="avariable" type="hidden" value="<?php echo $xxxx; ?>" /></form>
it works but i cant find a way to use the value="" without submiting the form from the server(using stuff like
$yes = $_REQUEST['avariable']
,) the form is introduced in part B that is, and appears in part c.
Help ???? I basically need my php variable created in part B.!!
Since you already have control on the page B, you can send them with some delimiter and split them? For example lets say the output is
word1,,,word2,,,word3,,,word4
Your code can be
<?php
$vars = explode(",,,", file_get_contents('your_url');
// echo $vars[1];
// echo $vars[2];
// echo $vars[3];
// echo $vars[4];
?>