Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Handy little development functions.  (Read 3402 times)

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
Handy little development functions.
« on: January 26, 2009, 01:07:32 PM »

It is always handy for dev and trouleshooting to be able to see what is being placed into variables.
So, here is 2 that I place at bottom of snews.php when hacking, modding, troubleshooting.
**NOTE** the second uses the first. The first can be used by itself.
Code: [Select]
<?php
function printr($array){
   if(
$array){
echo '<pre>';
   if(
is_array($array)){
print_r($array);}else{echo $array;}
echo '</pre>';}
  return ;
}
function 
var_list(){//usage -- var_list('VARNAME',$VAR,'VARNAME',$VAR...etc);
   
$args func_num_args();
 if (
$args>0){$i=0;
 
$varName=func_get_args();
 while (
$i<$args){
  echo  
$varN='<h3>'.$varName[$i].'</h3>';
   
printr($varName[$i+1]);
    
$i=$i+2;}}
   return;
}
?>
« Last Edit: February 16, 2009, 10:27:56 AM by philmoz »
Logged
Of all the things I have lost, it is my mind that I miss the most.

centered

  • Guest
Re: Handy little development functions.
« Reply #1 on: January 26, 2009, 01:50:27 PM »

We need some coding standards. Spaces and line breaks please. 

Code: [Select]
function printr($array){
if ($array) {
echo '<pre>';
    if (is_array($array)) {
print_r($array);
} else {
echo $array;
}
echo '</pre>';
}
  return ;
}

//usage -- var_list('VARNAME',$VAR,'VARNAME',$VAR...etc);
function var_list() {
$args = func_num_args();
  if ($args > 0) {
$i = 0;
  $varName = func_get_args();
  while ($i < $args) {
  echo $varN = '<h3>'.$varName[$i].'</h3>';
    print_r($varName[$i+1]);
    $i = $i + 2;
}
}
    return;
}
« Last Edit: January 26, 2009, 01:57:55 PM by equilni »
Logged

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
Re: Handy little development functions.
« Reply #2 on: February 16, 2009, 10:27:10 AM »

Ok, here's another one.
This one will show the table names, and fields of the db, which is real handy when doing any sql query writing.

Code: [Select]
<?php
function show_tables(){
   
$dbname db('dbname');
   
$sql "SHOW TABLES FROM $dbname";
   
$result mysql_query($sql);

if (!
$result) {
    echo 
"DB Error, could not list tables\n";
    echo 
'MySQL Error: ' mysql_error();
    exit;
}

while (
$row mysql_fetch_array($result)) {
    echo 
"Table: {$row[0]}<br />\n";
    
$tablename=$row[0];

    
$result2 mysql_query("DESCRIBE ".$row[0]);
if (!
$result2) {
    echo 
'Could not run query: ' mysql_error();
    exit;
}
if (
mysql_num_rows($result) > 0) {
    while (
$row2 mysql_fetch_assoc($result2)) {

    
$col_names[$tablename][]=$row2['Field'];
    }
       
printr($col_names[$tablename]);
}
}
?>
This also uses the printr() function from first thread.
« Last Edit: February 16, 2009, 10:31:12 AM by philmoz »
Logged
Of all the things I have lost, it is my mind that I miss the most.

mdj

  • Full Member
  • ***
  • Karma: 44
  • Posts: 226
    • mdj.us
Re: Handy little development functions.
« Reply #3 on: March 16, 2009, 02:07:53 PM »

We need some coding standards. Spaces and line breaks please. 

Such as no tabs... please  :)

Perhaps follow PHP's PEAR standards, or the Zend docs

http://framework.zend.com/manual/en/coding-standard.html

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Handy little development functions.
« Reply #4 on: March 11, 2010, 03:43:45 AM »

Stemming from this post here: http://snewscms.com/forum/index.php?topic=8974.0
This can (and probably should) come after the snews.php include call in the index.php.

** Note, most of this code is untested in live environments and you may not need all of it.

Wrapping global variables in functions.
Code: [Select]
<?php

// Global wrapping
// Returns the url in an array
function get_uri() {
static $uri;
if (!$uri) {
if ( empty( $_GET ) ) return; 
$uri explode('/'clean($_GET['category'])); 
$uri unset_empty_value$uri ); // Needs unset_empty_value from here:  http://snewscms.com/forum/index.php?topic=8672.0
if ( empty( $uri ) ) return;
}
return $uri;
}

function post_id() {
global $_ID;
return $_ID;
}

function post_title() {
global $_TITLE;
return $_TITLE;
}

function position() {
global $_POS;
return $_POS;
}

function cat_id() {
global $_catID;
return $_catID;
}

function cat_name() {
global $_NAME;
return $_NAME;
}

function parent_name() {
global $_XNAME;
return $_XNAME;
}

function description() {
global $categorySEF$_DESCR;
$desc = !$categorySEF
s('website_description'
$_DESCR;
return $desc;
}

function keywords() {
global $categorySEF$_KEYW;
$keys = !$categorySEF 
s('website_keywords'
$_KEYW;
return $keys;
}

?>


Additional current post (single article or page) data
Code: [Select]
<?php

function 
curr_post_info() {
if (!post_id()) return;
static  $post;
if (!$post) {
$q 'select *, 
(SELECT count(id) FROM comments WHERE articleid = '
.post_id().') as cmt_num 
FROM articles
WHERE id = '
.post_id();
$result mysql_query($q);
while ( $r mysql_fetch_assoc($result) ) {
$post $r;
}
}
return $post;
}

function post_content() {
$post curr_post_info();
return $post['text'];
}

function post_date() {
$post curr_post_info();
$date '';
if (isset($post['date'])) {
$date date(s('date_format'), strtotime($post['date']));
}
return $date;
}

?>


Is this true???
Code: [Select]
<?php

function 
is_lang_var$var ) {
if ( l($var) ) {
return TRUE;
}
}

function is_setting$name ) {
if ( s($name) ) {
return TRUE;
}
}

function is_admin_page() {
global $categorySEF;
if ( _ADMIN &&
check_category($categorySEF) ||
  check_category($_GET['action'])
 )
) {
return TRUE;
}
}

function is_article() {
if ( position() == ) {
return TRUE;
}
}
function is_page() {
if ( position() == ) {
return TRUE;
}
}
function is_category() {
if ( !position() ) {
return TRUE;
}
}
function is_commentable() {
$post curr_post_info();
if ( $post['commentable'] == 'YES' ) {
return TRUE;
}
}
function has_comments() {
$post curr_post_info();
if ( $post['cmt_num'] > ) {
return TRUE;
}
}

?>

Helpers
Code: [Select]
<?php

// Helper functions
function curr_url() {
return 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

// Based on http://herdian.ferdianto.com/2009/05/21/simple-routing-library
// Ex: dispatch('!^$!', 'user_function'); calls for home
// Ex: dispatch('!^contact$!', 'user_function'); calls for contact page
// Ex: dispatch( '!^uncategorized(.*)$!', 'user_function' ); calls for category and subsequent subcats/articles
function dispatch$pattern$callback ) {
$path str_replace(dirname($_SERVER['PHP_SELF']), ''$_SERVER['REQUEST_URI']);
$path ltrim($path'/');
if (preg_match($pattern$path$match)) {
if (function_exists($callback)) {
return call_user_func($callback$match);
}
}
}

?>

Logged