Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

[TIP / TRICK] viewArray debug function


mattice

Recommended Posts

Fist of all: This is a tip for php coders, not a trick for your shop.

It is only handy if you hack into your osCommerce shop alot.

If not you can stop reading here ;)

 

I found this function in the annotated manual of php.net long time ago,

I do not recall the author but it wasn't me.

What I did do was alter it for php commandline use as well.

 

 

What does this do?

It displays the content of an array in a nice visual format, no matter how many nested arrays there are which is ideal for debugging.

 

So how do I use it?

You add the viewArray code to includes/functions/general.php

on both the admin / catalog side.

Now whenever you are working with arrays (most of the time in osC)

and want to know what they contain at a certain point in your script you can simply call:

 

viewArray($whatever_array);

 

Try it in product_listing.php for instance and be amazed...

 

<?php

///////

// Extremely handy function to view (n-dimentional) arrays



function viewArray($arr) {

       echo '<table cellpadding="0" cellspacing="0" border="1">';

  foreach ($arr as $key1 => $elem1) {

       echo '<tr>';

       echo '<td>'.$key1.' </td>';

  if (is_array($elem1)) { extArray($elem1); }

else { echo '<td>'.$elem1.' </td>'; }

       echo '</tr>';

 }

echo '</table>';

}



function extArray($arr) {

               echo '<td>';

               echo '<table cellpadding="0" cellspacing="0" border="1">';

       foreach ($arr as $key => $elem) {

                       echo '<tr>';

                       echo '<td>'.$key.' </td>';

         if (is_array($elem)) { extArray($elem); }

   else { echo '<td>'.htmlspecialchars($elem).' </td>'; }

       echo '</tr>';

  }

echo '</table>';

echo '</td>';

}

 

And my commandline version which you need to include in your

actual commandline script.... Note that this is called c_viewArray as I do

include application_top in my commandline scripts now and then in which case it needs to be called differently.

 

///////////////

// PHP commandline version: 



function c_viewArray($arr) {

  foreach ($arr as $key1 => $elem1) {

       echo "n" . '['.$key1.']--------------------------' . "n";

  if (is_array($elem1)) { c_extArray($elem1); }

else { echo ' -> '.$elem1 . "n"; }

  }

}



function c_extArray($arr) {

       foreach ($arr as $key => $elem) {

                       echo "t" . '['.$key . "]";

         if (is_array($elem)) { c_extArray($elem); }

   else { echo  ' -> '. $elem . "n"; }

   }

}

?>

 

I use this function on a daily basis and thought I'd share it,

as it has been very helpfull to me.

 

Mattice

"Politics is the art of preventing people from taking part in affairs which properly concern them"

Link to comment
Share on other sites

  • 2 weeks later...

nice mattice...

but can you make something for me

 

instead of display array in main page, just show a icon_debug_tag and open a new windows to display it using onmouseover or onclick ????

 

it would really help :)))))

Link to comment
Share on other sites

nice mattice...

but can you make something for me

 

instead of display array in main page, just show a icon_debug_tag and open a new windows to display it using onmouseover or onclick ????

 

it would really help :)))))

 

You should not ask questions in the Tips & Tricks forum....

 

I'll give you a hint though:

 

try changing all echo in to $output .=

Don't forget to return $output in the first function.

That should work. If not PM / mail me over it.

"Politics is the art of preventing people from taking part in affairs which properly concern them"

Link to comment
Share on other sites

i have made some change and publish it as debug package

 

mainly, instead of displaying data inside the same page, it display only a small icon and when you click on it display value in a popup

 

and in admin you decide to allow or not debug, so no more need to remove the debug line after

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...