Difference between revisions of "Php"

From Wiki2
Line 1: Line 1:
==bits of code I always forget==
==bits of code I always forget==
====[http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/  howto store data in object instead of array]====
====[http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/  howto - store data in object instead of array]====
====howto - read command line arguments====
====howto - read command line arguments====
<syntaxhighlight>  
<syntaxhighlight>  
Line 13: Line 13:
</syntaxhighlight>  
</syntaxhighlight>  


====howto MYSQL -> JSON====
====howto MYSQL -> JSON====
<syntaxhighlight>
<syntaxhighlight>
$sth = mysql_query("SELECT ...");
$sth = mysql_query("SELECT ...");
Line 23: Line 23:
</syntaxhighlight>
</syntaxhighlight>


====howto see if a character is in a string====
====howto - see if a character is in a string====
you need the ! to test if $somechar==";" (strcmp returns 0 if true)
you need the ! to test if $somechar==";" (strcmp returns 0 if true)
*if(!strcmp($somechar, ";")) {
*if(!strcmp($somechar, ";")) {
Line 43: Line 43:
$firephp->log($a, 'nearrtest');
$firephp->log($a, 'nearrtest');
</poem>
</poem>
===misc===
==misc==


===refs===
==refs==
[http://www.php-editors.com/articles/simple_php_classes.php good class tutorial]
====[http://www.php-editors.com/articles/simple_php_classes.php good class tutorial]====


http://www.weberdev.com/Manuals/PHP/function.get-meta-tags.html
http://www.weberdev.com/Manuals/PHP/function.get-meta-tags.html


authentication script http://sitebuilt.net/w/index.php?title=Comp&action=edit&section=9
authentication script http://sitebuilt.net/w/index.php?title=Comp&action=edit&section=9

Revision as of 10:20, 17 January 2013

bits of code I always forget

howto - store data in object instead of array

howto - read command line arguments

<syntaxhighlight> <?php echo count($argv); if (count($argv)<2){ //if no command line argument echo("script requires command line argument with name of db to backup"); exit(); } $db = $argv[1]; ?> </syntaxhighlight>

howto - MYSQL -> JSON

<syntaxhighlight> $sth = mysql_query("SELECT ..."); $rows = array(); while($r = mysql_fetch_assoc($sth)) {

   $rows[] = $r;

} print json_encode($rows); </syntaxhighlight>

howto - see if a character is in a string

you need the ! to test if $somechar==";" (strcmp returns 0 if true)

  • if(!strcmp($somechar, ";")) {
as seen in assess/wikiquiz/wikivo2db.php

to get from text area w/o losing linebreaks

  • $apage=nl2br(stripslashes($_POST[apage]));

to iterate through the lines in a string

  • foreach (preg_split("/(\r?\n)/", $apage) as $line)

howto - populate multi-dimensional arrays

/*multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

$firephp->log($a, 'nearrtest');

misc

refs

good class tutorial

http://www.weberdev.com/Manuals/PHP/function.get-meta-tags.html

authentication script http://sitebuilt.net/w/index.php?title=Comp&action=edit&section=9