php-general Digest 13 Apr 2009 14:29:23 -0000 Issue 6065

Topics (messages 291407 through 291421):

Re: pear mdb2 and null
        291407 by: Michael A. Peters
        291419 by: Bastien Koert

Re: what to use instead of foreach
        291408 by: Leon du Plessis
        291418 by: Mark Kelly
        291420 by: PJ

Re: Connecting to dBase using ODBC on Mac OS X
        291409 by: Matt Neimeyer

Re: What was the unix timestamp of last week, Monday 12:00 am?
        291410 by: Robert Cummings

Re: Generate XHTML (HTML compatible) Code using DOMDocument
        291411 by: Michael A. Peters
        291412 by: Michael Shadle
        291414 by: Michael A. Peters
        291415 by: Michael A. Peters
        291416 by: Michael A. Peters

Nagios Monitoring
        291413 by: Waynn Lue
        291417 by: Yannick Mortier

try - catch is not so clear to me...
        291421 by: Lamp Lists

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 ---
Michael A. Peters wrote:
Phpster wrote:


On Apr 11, 2009, at 21:38, "Michael A. Peters" <[email protected]> wrote:

I've run into a small issue with mdb2.

I have a mysql database with a field set to longtext not null.

inserting "" into that field works just dandy when using the mysql_ functions.

However, when using mdb2 - it converts "" to NULL which is NOT what I want to have happen, and the result is that the execute() fails because the database table does not accept NULL for that field.

Why does mdb2 turn "" into NULL for a text type when MySQL knows there is a difference? How do I suppress that?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Why not set a default in the field then, as am empty string and let the db handle field properly? Having a Not Null with no default is bad db design.

I need it to error when an attempt to create a record without setting that field is attempted, but setting the field to an empty string is fine.

Attempting to insert data without defining that field indicates there is not sufficient information to create a record. Setting that field to a zero length string however indicates that there is enough information to create a record. Assuming that no information is the same as an zero length string is not OK.

Call it bad design if you want, by MySQL knows the difference between NULL and an empty string, so should my database abstraction layer.


Even if the default is set to '' - pear mdb2 refuses to do the insert.
So it looks like pear mdb2 does not know the difference between NULL and a zero length string.

Hopefully that is configurable somewhere.

--- End Message ---
--- Begin Message ---
[snip]
>> I need it to error when an attempt to create a record without setting that
>> field is attempted, but setting the field to an empty string is fine.
>>
>> Attempting to insert data without defining that field indicates there is
>> not sufficient information to create a record. Setting that field to a zero
>> length string however indicates that there is enough information to create a
>> record. Assuming that no information is the same as an zero length string is
>> not OK.
>>
>> Call it bad design if you want, by MySQL knows the difference between NULL
>> and an empty string, so should my database abstraction layer.
>>
>>
> Even if the default is set to '' - pear mdb2 refuses to do the insert.
> So it looks like pear mdb2 does not know the difference between NULL and a
> zero length string.
>
> Hopefully that is configurable somewhere.
>
  </snip]

Shouldn't this be really handled by the application layer, not the db layer?
If you are using the db to show the error code for the text field, wouldn't
it be simpler to handle that in the validation code, and then use the
default in the table to insert the data?


-- 

Bastien

Cat, the other other white meat

--- End Message ---
--- Begin Message ---
You may try something basic like:

$b = 1;
foreach ($my_array as $a)
{  
    echo " $a ";

    //Send new line to browser
    if ($b++ == 3) { echo "<br>"; $b = 1; }
}

Or there are some different ways to approach this also like:
for ($a =& current($my_array); $a; $a = next($my_array))
{  
    //Format 1
    echo " $a "; 
    $a = next($my_array);

    //Format 2
    /* you may add checks here to see if $a contains data */
    echo "  ~ $a ~ "; $a = next($my_array);

    //Format 3 + NEW LINE
    /* you may add checks here to see if $a contains data */
    echo "  ~~ $a ~~<br> ";
}

This way you have some added control over the iteration through the array,
and you can play around with when & how to display what.

Regards.

-----Original Message-----
From: PJ [mailto:[email protected]] 
Sent: 12 April 2009 08:57 PM
To: [email protected]
Subject: [PHP] what to use instead of foreach

foreach does not allow for different formatting for output...
What could be used as a workaround?
example:
echo $some_result, "<br>"; // will print all results in 1 column
echo $some_result, ","; // will print all results comma-separated in 1 row

But how do you get result1, result2 & result3 // with <br> at end ?

-- 
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- [email protected]
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
Hi.

On Sunday 12 April 2009, PJ wrote:
> foreach does not allow for different formatting for output...

[snip]

> But how do you get result1, result2 & result3 // with <br> at end ?

$lastIndex = count($a) - 1; // Adjust for zero-indexing.
$outputString = '';
foreach ($a as $index => $value) {
    if ($index != $lastIndex) {
        $outputString .= "$value, ";
    } else {
        $outputString = rtrim($outputString,', '); // Strip last comma.
        $outputString .= " & $value<br />";
    }
}
echo $outputString;

Like that? If your array is associative you can drop the $lastIndex calc 
and adjust the loop to update a counter instead. 

HTH

Mark

--- End Message ---
--- Begin Message ---
Mark Kelly wrote:
> Hi.
>
> On Sunday 12 April 2009, PJ wrote:
>   
>> foreach does not allow for different formatting for output...
>>     
>
> [snip]
>
>   
>> But how do you get result1, result2 & result3 // with <br> at end ?
>>     
>
> $lastIndex = count($a) - 1; // Adjust for zero-indexing.
> $outputString = '';
> foreach ($a as $index => $value) {
>     if ($index != $lastIndex) {
>         $outputString .= "$value, ";
>     } else {
>         $outputString = rtrim($outputString,', '); // Strip last comma.
>         $outputString .= " & $value<br />";
>     }
> }
> echo $outputString;
>
> Like that? If your array is associative you can drop the $lastIndex calc 
> and adjust the loop to update a counter instead. 
>
> HTH
>
> Mark
>
>   
Thanks for the suggestion, Mark. I've already experimented with count;
you're close, but there is still a small glitch and that's in count();
foreach doesn't give a damn about count so you can't use that - it is
reset once inside the foreach loop. And, the "as $index" cannot relate
to the $lastIndex because the array index (which would be the index in
the "as $index") can be any number and is not sequential. There are a
couple of other ways, one is suggested by Jim Lucas in the thread
"extract varying data from array with different formatting"; another is
to change the SELECT query to use a ranking reference - I think it may
be a bit more complex, though.
Any other thoughts? ;-)  I'll appreciate them.

-- 
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- [email protected]
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


--- End Message ---
--- Begin Message ---
I know this isn't exactly what you were probably looking for but...

If you have a Windows machine available I would recommend taking a
look at the ODBTP project at http://odbtp.sourceforge.net. ODBTP
stands for Open DataBase Transport Protocol. The short version is that
you add a client module to PHP which allows you to connect to a server
on the windows machine. Using this client/server you can connect to
any ODBC source on the windows machine from the Mac.

I know it's not perfect (since you need Windows running) but we've
used it to add Web acessibility to a legacy VFP app with some success.

Caveat: These are, all things considered, relatively small
installations. By small I mean 5-100 users per intranet accessing
100-20,000 customer records. I could not imagine using this solution
for any major, publicly accessible web site. (We have one client that
tracks 300,000+ records and performance is NOTICABLY slow)

Pro: Allows you to access any odbc compliant database from any web
server that you can compile the client for.

Con: Requires Windows (works reasonably well with a Parallels
installation though...) Only as stable as the underlying ODBC driver
(the VFP driver is single threaded and locks up after a while... but
restarting the ODBTP service frees it all up.)

Overall: Good for transitioning from a "Legacy" application or for
infrequent tasks like importing from "windows only" file formats (like
VFP).

We've used this in several installations where the client doesn't want
to lose their legacy app, "refuses" to upgrade and wants to provide
web access to sales people on the road. We've seen the best
performance using PHP under IIS on Windows connecting to the same
machine. Our worst case for performance is an installation that uses a
Mac OSX 10.4 XServe Web Server connecting to a Windows 2003 Server
which then accesses VFP data files on a Novell Server of some flavor.
(From that client I learned that apparently Windows ALWAYS tries the
Microsoft network file redirectors before it will try any available
Novell network file redirectors. At least that's what the Client's IT
department tells me whenever we relay user complaints about the speed
at that site)

Hope this helps.

Matt

On Thu, Apr 9, 2009 at 10:53 AM, Rahul S. Johari
<[email protected]> wrote:
> Ave,
>
> Does anyone have any knowledge on connecting a FoxPro table (.dbf, dbase)
> using ODBC on a Mac OS X? I've been googling but not much is turning up.
> Some information is available on ODBC Connections using PHP ... very little
> on Mac OS X ... and absolutely none to do with a FoxPro dBase table.
>
> Thanks.
>
> ---
> Rahul Sitaram Johari
> Founder, Internet Architects Group, Inc.

--- End Message ---
--- Begin Message ---
On Sun, 2009-04-12 at 20:12 -0600, René Fournier wrote:
> I'm trying to write a [simple] function, such that:
> 
> function earlier_unix_timestamp () {  
>       $now = mktime();
>       [...]
>       return $then;  // e.g., 1238983107
>       }
> 
> Anyone have something already made? There seem to be many ways to skin  
> this cat, with date() arithmetic, etc., but the exceptions (Jan 1,  
> first day of the month, etc.) are driving me crazy.

<?php

function earlier_unix_timestamp()
{
    strtotime( 'last monday' );
}

?>

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


--- End Message ---
--- Begin Message ---
Raymond Irving wrote:
Hello,

After talking with Michael about how to generate XHTML code using the DOM I 
came up with this little function that I'm thinking of using to generate XHTML 
code that's HTML compatible:

function saveXHTML($dom) {
    $html = $dom->saveXML(null,LIBXML_NOEMPTYTAG);
    $html = str_replace('&#13;','',$html);
    $html = preg_replace('/<\?xml[^>]*>\n/','',$html,1);
    $html = 
preg_replace('/<\!\[CDATA\[(.*)\]\]><\/script>/s','//<![CDATA[\1//]]></script>',$html);
    $html = 
preg_replace('/><\/(meta|link|base|basefont|param|img|br|hr|area|input)>/',' 
/>',$html);
    return $html;
}

What do you think?


__
Raymond Irving


I found this in my old files - don't know if any of it is useful to you -

function HTMLify($buffer) {
   $xhtml[] = '/<script([^<]*)\/>/';
   $html[]  = '<script\\1></script>';

   $xhtml[] = '/<div([^<]*)\/>/';
   $html[]  = '<div\\1></div>';

   $xhtml[] = '/<a([^<]*)\/>/';
   $html[]  = '<a\\1></a>';

   $xhtml[] = '/\/>/';
   $html[]  = '>';

// DOMDocument never produces white space between / and > on self closing tags
//   $xhtml[] = '/\/\s+>/';
//   $html[]  = '>';

   return preg_replace($xhtml, $html, $buffer);
   }

I think I actually had extended the function beyond the replacements there, but I don't seem to have them anymore.

What really would be nice is to patch the libxml2 library to add an option to change it's default behaviour of screwing up utf8 on html export and then be able to pass that option to saveHTML() so the utf8 issue would go away.

That's way beyond my skill though. Has anyone brought up the issue with the libxml developers?
--- End Message ---
--- Begin Message ---
On Sun, Apr 12, 2009 at 8:07 AM, Raymond Irving <[email protected]> wrote:

>    $html = 
> preg_replace('/<\!\[CDATA\[(.*)\]\]><\/script>/s','//<![CDATA[\1//]]></script>',$html);

question -

the output of this would be

<script type="text/javascript"><![CDATA    js code ... ]]></script> right?

is the cdata truly necessary? I typically use XHTML 1.0 transitional
and I don't have problems validating.

--- End Message ---
--- Begin Message ---
Michael Shadle wrote:
On Sun, Apr 12, 2009 at 8:07 AM, Raymond Irving <[email protected]> wrote:

   $html = 
preg_replace('/<\!\[CDATA\[(.*)\]\]><\/script>/s','//<![CDATA[\1//]]></script>',$html);

question -

the output of this would be

<script type="text/javascript"><![CDATA    js code ... ]]></script> right?

is the cdata truly necessary? I typically use XHTML 1.0 transitional
and I don't have problems validating.


The problem is that validating xhtml does not necessarily render properly in some browsers *cough*IE*cough*

That's why I prefer to send html 4.01 to those browsers.

Would this function work for sending html and solve the utf8 problem?

function makeHTML($document) {
   $buffer = $document->saveHTML();
   $output = html_entity_decode($buffer,ENT_QUOTES,"UTF-8");
   return $output;
   }

I'll try it and see what it does.

--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:


function makeHTML($document) {
   $buffer = $document->saveHTML();
   $output = html_entity_decode($buffer,ENT_QUOTES,"UTF-8");
   return $output;
   }

I'll try it and see what it does.


Huh - not tried above yet - but with

$test = $myxhtml->createElement('p','שלום');
$xmlBody->appendChild($test);

both saveXML() and saveHTML() do the right thing.

However if I have the string

<p>שלום</p>

and load it into a DOM -

With loadHTML() the utf8 is lost regardless of whether I use saveXML() or saveHTML()

With loadXML() the utf8 is preserved regardless of whether or not I use saveXML() or saveHTML()

php 5.2.9
libxml2 2.6.26-2.1.2.7 (CentOS 5.3)

I wonder if the real utf8 problem people experience is really with loadHTML() and not with saveHTML() ??
--- End Message ---
--- Begin Message ---
Michael A. Peters wrote:


I wonder if the real utf8 problem people experience is really with loadHTML() and not with saveHTML() ??


Go to http://www.clfsrpm.net/xss/dom_script_test.php

The page was meant to test something else but enter some UTF-8 into the textarea (well formed xhtml) -

With IE - html 4 is the only thing it will output but with FireFox and Opera, by default it outputs xhtml but you can check a box to force html.

When it outputs html it uses saveHTML()
When it outputs xhtml it uses saveXML()

The source is linked there so you can see.

Anyway - it looks saveHTML() works fine with UTF8 as long as loadXML() was used to load the data.

I've tried both Hebrew and Polytonic Greek.
It outputs correctly regardless of html or xhtml - but only after I changed the code to load the textarea as xml opposed to html.
--- End Message ---
--- Begin Message ---
Hey guys,

I'm looking to write a nagios plugin that essentially monitors whether or
not a database query returns a value > 0 at any given point.  I was hoping
to write this in PHP, but I haven't found too many examples of Nagios
plugins in PHP (
http://www.barryodonovan.com/index.php/2007/11/02/asterisk-pri-nagios is the
best one I've found so far).  Just wondering if anyone has any experience
writing them?

Waynn

--- End Message ---
--- Begin Message ---
2009/4/13 Waynn Lue <[email protected]>:
> Hey guys,
>
> I'm looking to write a nagios plugin that essentially monitors whether or
> not a database query returns a value > 0 at any given point.  I was hoping
> to write this in PHP, but I haven't found too many examples of Nagios
> plugins in PHP (
> http://www.barryodonovan.com/index.php/2007/11/02/asterisk-pri-nagios is the
> best one I've found so far).  Just wondering if anyone has any experience
> writing them?
>
> Waynn
>

I'm not a nagios expert but as far as I know a plugin can be anything
executable that returns the necessary values. So basically you could
just write a php script and make it executable with

#!/path/to/php

write the script here

You can find more information how to use the php cli here:

http://php.net/features.commandline

And here you can find the needed return values:

http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN78

I hope this helps!

-- 
Currently developing a browsergame...
http://www.p-game.de
Trade - Expand - Fight

Follow me on twitter!
http://twitter.com/moortier

--- End Message ---
--- Begin Message ---
hi to all!

actually, the statement in the Subject line is not 100% correct. I understand 
the purpose and how it works (at least I think I understand :-)) but to me it's 
so complicated way?

let's take a look in example from php.net(http://us3.php.net/try)


<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>  
I would do the same thing, I think, less complicated:

<?php
function inverse($x)
{
    if (!$x) {
        echo 'Division by zero';
    }
    else return 1/$x;

}

echo inverse(5);
echo inverse(0);

// Continue execution
echo 'Hello world';
?>

I know this is "too simple, maybe not the best example", but can somebody 
please explain "the purpose" of try/catch?

Thanks.

-LL


      

--- End Message ---

Reply via email to