December 23, 2012

How to print just a few from an array instead of all in php

Question by Marqeeu Rahfis

This is part of the working code that shows the entire array;

$files = filelist("./",1,1); // call the function
shuffle($files);
foreach ($files as $list) {//print array
echo "<a href="" . $list['name'] . "$startDir"><h4> " . $list['name'] . " </h4></a>";
//    echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ."<br>";

How do I modify it so that it only displays 10 or 15 list instead of all?

Answer by Starx

If you know the keys or indexes of the array you can do what KingCrunch is doing a lot faster by simple for loop

for($i=0; $i<=14; $i++) {
   // echo $file[$i];
}
December 20, 2012

Call to undefined method PHP_CodeCoverage_Filter::getInstance()

Question by beanland

I’ve got a fresh copy of PHPUnit installed on my system (Ubuntu 11), but whenever I type phpunit in the console I get the following error:

PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/bin/phpunit on line 39

I have PHPUnit’s code coverage installed, as far as I know:

>sudo pear install phpunit/PHP_CodeCoverage

phpunit/PHP_CodeCoverage is already installed and is the same as the released version 1.1.1

install failed

Why am I getting this error and how can I fix it?

Answer by David Harkness

The executable script that loads PHPUnit must not have been updated when going to 3.6.x. Reinstall it.

sudo pear uninstall phpunit/PHPUnit
sudo pear install phpunit/PHPUnit

If this doesn’t work, make sure PEAR itself is up-to-date.

Answer by Starx

For some, Anthony’s solution will not work fully because of the Unknown remote channel: pear.symfony.com or phpunit/PHPUnit requires package "channel://pear.symfony.com/Yaml".

SO here is the upgraded solution that solves this:

sudo apt-get remove phpunit

sudo pear channel-discover pear.phpunit.de

sudo pear channel-discover pear.symfony-project.com

sudo pear channel-discover components.ez.no

sudo pear channel-discover pear.symfony.com

sudo pear update-channels

sudo pear upgrade-all

sudo pear install pear.symfony.com/Yaml

sudo pear install --alldeps phpunit/PHPUnit

sudo pear install --force --alldeps phpunit/PHPUnit
December 9, 2012

Get count of columns MySQL

Question by user1868569

How can i get the count of columns in my table, searched google through, but can’t find a straight answer.

Thank you in advance.

Answer by Starx

Such information are located in INFORMATION_SCHEMA you can query a table called COLUMNS to get the number of columns in a database.

This query will give you the column count.

SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DB_NAME' AND table_name = 'TABLE_NAME'
...

Please fill the form - I will response as fast as I can!