php-general Digest 27 Feb 2010 07:05:54 -0000 Issue 6612
Topics (messages 302407 through 302419):
Post and Redirect
302407 by: Shawn McKenzie
302410 by: Ashley Sheridan
Help preserving sentence structure
302408 by: John Tamm-Buckle
302411 by: Ashley Sheridan
302415 by: Ryan Sun
Registry class question.
302409 by: Peter van der Does
Re: Excel Spreadsheets and PHP
302412 by: Ian Robertson
Re: ctype_print, the British Pound and other non-ASCII characters
302413 by: Bob
302414 by: Bob
Re: inexplicable behaviour of pre- and post-increment operators
302416 by: clancy_1.cybec.com.au
Header function
302417 by: Nick allan
Uninstalling PHP?
302418 by: Austin Powers
302419 by: John Black
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
I remembered seeing this question on the list several times in the past,
so I thought I would post something I just hacked up for someone.
As we know, we can user header() to redirect the browser, but of course
we can't redirect the browser and have it post data to the new page. If
you need to do this it will require javascript. Here's a quick and
dirty function:
function http_post_redirect($url='', $data=array(), $doc=false) {
$data = json_encode($data);
if($doc) {
echo "<html><head></head><body>";
}
echo "
<script type='text/javascript'>
var data = eval('(' + '$data' + ')');
var jsForm = document.createElement('form');
jsForm.method = 'post';
jsForm.action = '$url';
for (var name in data) {
var jsInput = document.createElement('hidden');
jsInput.setAttribute('name', name);
jsInput.setAttribute('value', data[name]);
jsForm.appendChild(jsInput);
}
document.body.appendChild(jsForm);
jsForm.submit();
</script>";
if($doc) {
echo "</body></html>";
}
exit;
}
--
Thanks!
-Shawn
http://www.spidean.com
--- End Message ---
--- Begin Message ---
On Fri, 2010-02-26 at 13:26 -0600, Shawn McKenzie wrote:
> I remembered seeing this question on the list several times in the past,
> so I thought I would post something I just hacked up for someone.
>
> As we know, we can user header() to redirect the browser, but of course
> we can't redirect the browser and have it post data to the new page. If
> you need to do this it will require javascript. Here's a quick and
> dirty function:
>
> function http_post_redirect($url='', $data=array(), $doc=false) {
>
> $data = json_encode($data);
>
> if($doc) {
> echo "<html><head></head><body>";
> }
> echo "
> <script type='text/javascript'>
> var data = eval('(' + '$data' + ')');
> var jsForm = document.createElement('form');
>
> jsForm.method = 'post';
> jsForm.action = '$url';
>
> for (var name in data) {
> var jsInput = document.createElement('hidden');
> jsInput.setAttribute('name', name);
> jsInput.setAttribute('value', data[name]);
> jsForm.appendChild(jsInput);
> }
> document.body.appendChild(jsForm);
> jsForm.submit();
> </script>";
>
> if($doc) {
> echo "</body></html>";
> }
> exit;
> }
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
Someone mentioned on the whatwg mailing list that most browsers are
capable of understanding multipart content, so that you could send down
two types of content a bit like you'd send a multipart email.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
Hi, new to this list...
I'm adapting some code that generates text and want to keep the syntax. In
the original version the output was iterated from an array in <pre> tags.
That was fine for what it was, but it's now a word press plugin and the
<pre> tags get in the way. How do I keep the sentence structure without
using <pre>s? Also, I'm quite new to PHP in general... :) Any help would
be much appreciated!
Below is an extract of the code with some highlighting for emphasis:
function wpm_display_generator() {
if (isset($_POST['submit_button'])) {
// the form was submitted to this script - generate
// and display
require_once 'lib/generator.php';
require_once "lib/util.php";
require_once "lib/vocabulary.php";
require_once "lib/widgets.php";
$vocab = Vocabulary::getInstance();
$cats = $vocab->getCategories();
$catName = '';
$sentence = '';
if (isset($_POST['category_choice'])) {
$catName = $cats[$_POST['category_choice']];
$gen = new wordGenerator($_POST['category_choice'],
$_POST['length_choice']);
$lines = $gen->generate();
foreach ($lines as $line) {
$sentence .= "<pre>" . $line . "</pre>";
}
}
require 'sentence.php';
}
else {
require 'generator_form.php';
}
}
Many thanks,
John
--
Out now:
Kvist 002 - Raglani - Web of Light - 12"
Kvist 003 - EOD - untitled - 12"
Kvist 004 - JD Emmanuel - Solid Dawn - CD
Kvist 005 - Guillaume Gargaud - Here - CD
Coming soon:
Kvist 006 - Tom Hamilton - Pieces for Kohn/Formal & Informal Music - CD
www.kvistrecords.com
--- End Message ---
--- Begin Message ---
On Fri, 2010-02-26 at 14:11 -0600, John Tamm-Buckle wrote:
> Hi, new to this list...
>
> I'm adapting some code that generates text and want to keep the syntax. In
> the original version the output was iterated from an array in <pre> tags.
> That was fine for what it was, but it's now a word press plugin and the
> <pre> tags get in the way. How do I keep the sentence structure without
> using <pre>s? Also, I'm quite new to PHP in general... :) Any help would
> be much appreciated!
>
> Below is an extract of the code with some highlighting for emphasis:
>
> function wpm_display_generator() {
>
> if (isset($_POST['submit_button'])) {
>
> // the form was submitted to this script - generate
> // and display
> require_once 'lib/generator.php';
> require_once "lib/util.php";
> require_once "lib/vocabulary.php";
> require_once "lib/widgets.php";
>
> $vocab = Vocabulary::getInstance();
> $cats = $vocab->getCategories();
> $catName = '';
> $sentence = '';
> if (isset($_POST['category_choice'])) {
> $catName = $cats[$_POST['category_choice']];
> $gen = new wordGenerator($_POST['category_choice'],
> $_POST['length_choice']);
> $lines = $gen->generate();
> foreach ($lines as $line) {
> $sentence .= "<pre>" . $line . "</pre>";
> }
> }
>
> require 'sentence.php';
> }
> else {
> require 'generator_form.php';
> }
> }
>
>
> Many thanks,
>
> John
>
<pre> tags don't do anything to mark up a sentence. If you need to
convert the hard returns to a <br/> tag, then use nl2br()
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
http://us.php.net/manual/en/function.htmlentities.php
--- End Message ---
--- Begin Message ---
Hi,
I've build a registry class to store settings I need to use in several
other classes.
Currently I've set it up with a static array in the registry class and
using two methods to access the settings and values
storeSetting($key,$value) {
$this->_settings[$key] = $value;
}
getSetting($key) {
return $this->_settings[$key];
}
The question is what the pros and cons are compared to setting a new
property with the value, like:
storeSetting($key,$value) {
$this->$key = $value;
}
and then instead of calling getSetting, you just use
$this->Registry->property
--
Peter van der Does
GPG key: E77E8E98
IRC: Ganseki on irc.freenode.net
Twitter: @petervanderdoes
WordPress Plugin Developer
Blog: http://blog.avirtualhome.com
Forums: http://forums.avirtualhome.com
Twitter: @avhsoftware
--- End Message ---
--- Begin Message ---
Thank you all very much for your replies.
I learned about a few new approaches.
I didn't see it come up yet, so I'll post the URL of what I have been using.
php_writeexcel -
http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/
I've been able to pull off quite a bit with this class and actually have
referenced this Perl page for documentation since this PHP class was ported
from a Perl class -
http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-0.37/WriteExcel/doc/WriteExcel.html
So, many thanks for your replies and also thanks to Johann and his 3 Bettinas,
wherever you are, haha.
-----Original Message-----
From: Jay Blanchard [mailto:[email protected]]
Sent: Sunday, February 21, 2010 9:44 PM
To: Ian Robertson; [email protected]
Subject: RE: [PHP] Excel Spreadsheets and PHP
[snip]
What are you using, if anything, to create Excel spreadsheets with PHP?
[/snip]
PHP
http://www.evolt.org/node/26896
--- End Message ---
--- Begin Message ---
Hello, Nathan.
I'm glad to hear that someone else can reproduce the problem with
en_GB.UTF-8. I was worried it was some bad luck quirk that I was never
going to get to the bottom of.
I tried using en_US.utf8 (and also en_US.UTF-8) in setlocale (and it did
not return false, so again looks like the locale is found and accepted).
But I still got a return of false from ctype_print for non-ASCII
characters. So even with en_US I'm getting bad behaviour.
When you switch back to en_US.UTF-8 (or en_US.utf8) do you get true from
ctype_print as expected? (I'm hoping that you don't suddenly find
ctype_print refuses to behave properly under all locales.)
Output from `locale` shows that all types are 'en_GB.UTF-8' except LC_ALL
which is blank (as I believe it should be).
Do you know how I can dig further? I don't know anything about debugging
PHP or Linux, so I don't know how to trace the source of this strange
result.
--- End Message ---
--- Begin Message ---
In php.i18n, an interesting discussion about this problem has appeared.
It looks like the problem is Ubuntu and not PHP, as a short chunk of code
written in C and using the native isprint equivalent to ctype_print also
returns false for the British Pound symbol.
--- End Message ---
--- Begin Message ---
A week ago Dasn asked a question about converting arrays, and I quoted one
possible way of
achieving his task, using the operation:
$i = 0; while ($i < $k) { $b[$a[$i++]] = $a[$i++]; }
I added the comment that "I have always been wary of using statements like this
because I
was unsure when the incrementing would occur, so I tried it."
I received several CC e-mails replying to this post, including one rather
critical comment
to the effect that pre-and post-increment were all quite simple, and I really
ought to
learn the fundamentals before I started trying to do anything elaborate.
I posted a reply to these e-mails, but as neither they, nor my reply, or any
follow-up
discussion ever appeared in the discussion group I will repost this reply. (I
did have a
power failure at this time, so it is conceivable that any follow-up was lost as
a result
of a glitch in my mailer, but I think it is more likely that there was a glitch
in the
discussion group server.)
Unfortunately things aren't nearly as simple as this writer believes. The rule
I have
always used is that if you use the same variable as an index on both sides of
an assign
statement it is not safe to change the value of the index within the statement.
While I
have achieved the result I wanted in the example above (using PHP 5.1.6 --
there is no
guarantee it would work with other implementations of PHP) the results of doing
this in
the general case can be quite inexplicable.
The particular case which prompted my comment was the one where you want to
copy part of
one array into the corresponding elements of another array. In accordance with
my rule, I
normally write:
$i = 0; $j=count($a); while ($i < $j) { $b[$i] = $a[$i]; ++$i; }
It is tempting to try to put the increment into the assignment statement.
Clearly the
value of $a[$i] has to be read before it can be written to $b[$i], so the
logical
expression would be:
while ($i < $j) { $b[$i++] = $a[$i]; } A.
However if you try this, you get $b[1] = $a[0], and so on. But if you try the
alternative:
while ($i < $j) { $b[$i] = $a[$i++]; } B.
You get $b[0] = $a[1], and so on (as you would expect).
Out of curiosity, I then tried:
$i = -1; $j=count($a) - 1; while ($i < $j) { $b[$i] = $a[++$i]; }
C
This gave the desired result, and seemed moderately logical. However when I
tried:
$i = -1; $j=count($a) - 1; while ($i < $j) { $b[++$i] = $a[$i]; }
D
This gave exactly the same result. It is quite impossible to explain the
results in cases
A and D from the definitions of the pre-and post-increment operator, so I think
I will
stick to my safe rule!
--- End Message ---
--- Begin Message ---
Hi all
Has anyone got any ideas why the following isn't giving me correct filename
in the ie save dialogue
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="PurchaseReq.doc"');
I get the save dialogue, but with preq.doc instead of PurchaseReq.doc
Preq.php is the calling php file. It has worked before so I'm not sure what
I've changed to have it stop working.
Thanks in advance for any suggestions.
Regards Nick
--- End Message ---
--- Begin Message ---
Three weeks ago I was working through the Lynda.com "PHP with MySQL
Training" because I wanted to begin using PHP (surprise, surprise).
Anyway, on this video course the teacher explains that because installing
PHP and MySQL is so well understood on a Mac that we may as well just follow
his steps and do it manually. Well, he is installing a different version of
PHP and MySQL to the ones that I was able to download and while what he was
saying way somewhat similar I am guessing that there is a difference
somewhere, and (well) it's not working.
I AM A COMPLETE NOVICE WITH LINUX/FREEBSD. It had not been my intention to
learn the intricacies of Linux. However, I am now neck deep in a mire of
confusion that even MAMP can't seem to sort out for me.
It is purely a guess that I need to start again from a complete clean setup
(reformatting my hard disk and reinstall OS X again) but that is pretty much
out of the question.
I guess my question is:
"How can I completely uninstall PHP so that I can start again?"
Thanks.
--- End Message ---
--- Begin Message ---
On 02/27/2010 06:09 AM, Austin Powers wrote:
It is purely a guess that I need to start again from a complete clean setup
(reformatting my hard disk and reinstall OS X again) but that is pretty much
out of the question.
I guess my question is:
"How can I completely uninstall PHP so that I can start again?"
Hello,
Someone should be able to help you if you can provide us with the steps
you tried and where you failed.
--
John
Der unmündige Untertan ruft die Polizei. Der mündige Bürger informiert
seinen Anwalt.
[Nikolaus Cybinski]
--- End Message ---