Перемешать заданный массивPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Перемешать заданный массив

Сообщение Anonymous »

У меня есть облако тегов, которое в настоящее время настроено так, чтобы сначала отображался самый популярный элемент с использованием (строка 205):
// arsort($array_end);

который переворачивает массив. Если я оставлю это без внимания, список будет отображаться в алфавитном порядке.
Я бы хотел, чтобы результаты отображались в случайном порядке. Я думал о php-shuffle, но не знаю, как это сделать. Буду признателен, если кто-нибудь сможет мне помочь, спасибо!
См. php ниже:
//activate plugin WP function

//Checking search meter dependencies
global $wpdb, $table_prefix;
$sql = "SHOW TABLES LIKE '{$table_prefix}searchmeter_recent'";
$results = $wpdb->get_results($sql);
if (!$wpdb->num_rows )
{
die( '
This plugin will not work unless Search Meter plugin is installed and activated.
' );
}

register_activation_hook( __FILE__, 'initializeSearchTagCloud');

//activat plugin WP function
register_deactivation_hook( __FILE__, 'deactivateSearchTagCloud');

//set initial values when the plugin is activated
function initializeSearchTagCloud()
{
$search_tag_cloud=new searchTagCloud();
$search_tag_cloud->initializeSearchTagCloud();
}

//delete DB options when the plugin is activated
function deactivateSearchTagCloud() {
delete_option("searchTagCloudOption");
}

class searchTagCloud
{
public $widgetText;
public $numberSearches;
public $max_size;
public $min_size;
public $days_to_display;
var $error;

//constuctor function
function __construct()
{
$this->min_size=12;
$this->max_size=32;
$this->widgetText = 'What people is searching?';
$this->total_tags=10;
$this->show_author_credit=0;
$this->days_to_display=30;
//the size of the tag cloud is missed
}

//initialize options
//size of the smallest tag
//maximum size of the biggest tag
//Personalized text for the tag cloud
//how many links to display
function initializeSearchTagCloud()
{
global $wpdb, $table_prefix;
$wpdb->query("ALTER TABLE `{$table_prefix}searchmeter_recent` ADD COLUMN visible INT( 1 ) NOT NULL DEFAULT '1'");

$initializeOptions = array(
"min_size" => $this->min_size,
"max_size" => $this->max_size,
"total_tags" => $this->total_tags,
"widgetText" => $this->widgetText,
"days_to_display" => $this->days_to_display,
"show_author_credit" => $this->show_author_credit,
);
add_option("searchTagCloudOption", $initializeOptions, '', 'yes');
//select recent searched terms
}

//get DB options for the Search Tag Cloud
function getSearchTagCloudOptions()
{
$myOptions = get_option('searchTagCloudOption');
$this->min_size=$myOptions['min_size'];
$this->max_size=$myOptions['max_size'];
$this->widgetText=$myOptions['widgetText'];
$this->total_tags=$myOptions['total_tags'];
$this->days_to_display=$myOptions['days_to_display'];
$this->show_author_credit=$myOptions['show_author_credit'];
}

//set Search Tag Cloud class values
function setSearchTagCloudValues($min_size,$max_size,$total_tags,$widgetText,$show_author_credit,$days_to_display)
{
$this->min_size=$min_size;
$this->max_size=$max_size;
$this->widgetText=$widgetText;
$this->total_tags=$total_tags;
$this->show_author_credit=$show_author_credit;
$this->days_to_display=$days_to_display;
}

//update Search Tag Cloud class values and DB options
function updateSearchTagCloud($array_post)
{
global $wpdb, $table_prefix;

$this->setSearchTagCloudValues($array_post['min_size'],$array_post['max_size'],$array_post['total_tags'],$array_post['widgetText'],$array_post['show_author_credit'],$array_post['days_to_display']);

//set the new options in the database
update_option("searchTagCloudOption", $array_post, '', 'yes');

//I set all to visible
$wpdb->query("UPDATE `{$table_prefix}searchmeter_recent` SET visible=1");

if(is_array($array_post['checkbox_visible']))
{
foreach($array_post['checkbox_visible'] as $index=>$value)
$wpdb->query("UPDATE `{$table_prefix}searchmeter_recent` SET visible=0 WHERE terms = '{$value}'");
}
return __("Options Saved Correctly");
}

//Function to select from search meter tables in the database the most common searches.
function select_searches_for_tagcloud()
{
// List the most recent successful searches.
global $wpdb, $table_prefix;
$this->getSearchTagCloudOptions();
$count = intval($this->total_tags);

//first I need to know how many invisible tags we have

//select terms, COUNT( * ) AS total FROM `wp_searchmeter_recent` WHERE datetime>='2010-07-05' GROUP BY `terms` LIMIT 0,15

//$datebeginning = date()-dÌas
$datebeginning = date('Y-m-d', mktime(0, 0, 0, date("m"),date("d")-$this->days_to_display, date("Y")));

$tags = $wpdb->get_results(
//select recent searched terms
" SELECT terms, visible, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE datetime>='{$datebeginning}' AND
hits>0 AND
visible=1
GROUP BY `terms`
LIMIT {$count}");

return $tags;
}

function selectPopularSearchesforAdmin()
{
// List the most recent successful searches.
global $wpdb, $table_prefix;
$this->getSearchTagCloudOptions();
$count = intval($this->total_tags);

//first I need to know how many invisible tags we have

//select terms, COUNT( * ) AS total FROM `wp_searchmeter_recent` WHERE datetime>='2010-07-05' GROUP BY `terms` LIMIT 0,15

//$datebeginning = date()-dÌas
$datebeginning = date('Y-m-d', mktime(0, 0, 0, date("m"),date("d")-$this->days_to_display, date("Y")));

$invisible_tags = $wpdb->get_results(
" SELECT terms, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE visible=1 AND
`datetime`>='{$datebeginning}' AND
hits>0
GROUP BY `terms`
LIMIT {$count}");

// I have to show the tags plus the invisible ones
$count = $count + count($invisible_tags);

$tags = $wpdb->get_results(
//select recent searched terms
" SELECT terms, visible, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE datetime>='{$datebeginning}' AND
hits>0
GROUP BY `terms`
LIMIT {$count}");

return $tags;
}

//function that creates the tag cloud and prints it.
function popular_searches_tag_cloud($args)
{
$results=$this->select_searches_for_tagcloud();

if(count($results)>0)
{
foreach($results as $index)
{
$array_end[$index->terms]=$index->total;
}

// arsort($array_end);

// largest and smallest array values
$max_qty = max(array_values($array_end));
$min_qty = min(array_values($array_end));

// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}

// set the font-size increment
$step = ($this->max_size - $this->min_size) / ($spread);

//set the counter for the loop at 0
$counter=0;

// loop through the tag array
if(count($array_end)>0)
{
$html='';
$html.=''.$this->widgetText.'';
foreach ($array_end as $key => $value)
{
if($countertotal_tags)
{
$counter++;
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($this->min_size + (($value - $min_qty) * $step));

$html.= '' . $key . ' ';
}
else
break;
}

if($this->show_author_credit==1)
$html.='
WP plugin by Marketing Online
';

$html.='';
echo $html;
}
}
}
}
/*end class--------------------------------*/

//setting the admin page
//create admin->settings page
//create Settings Section to configure plugin values
if (is_admin() ){ // admin actions
add_action('set_twitter_keyword_values','set_twitter_keyword');
add_action('admin_menu','admin_setSearchTagCloud');
add_action('admin_init','searchTagCloudSettings' );
} else {
// non-admin enqueues, actions, and filters
}

//adding the page in the admin section
function admin_setSearchTagCloud() {
add_options_page('Popular Searches Tag Cloud Options', 'Popular Searches Tag Cloud', 8,__FILE__, 'searchTagCloudOptions');
}

//register form fields
function searchTagCloudSettings() { // whitelist options
register_setting('search-tag-cloud-options', 'widgetText', 'wp_filter_nohtml_kses');
register_setting('search-tag-cloud-options', 'max_size', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'min_size', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'total_tags', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'checkbox_visible');
register_setting('search-tag-cloud-options', 'show_author_credit', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'days_to_display', 'checkValueisInt');
}

function searchTagCloudOptions()
{
$html= '';
$html= '';
settings_fields('search-tag-cloud-options');
$html.= ''. __("Popular Searches Tag Cloud Options: Manage Options").'';
if($_POST['type-submit']=='Y')
{
$message=updateSearchTagCloudForm($_POST);
if($message!='')
$html.= '
'.$message.'
';
else
$html.= '
'.__("Options Saved").'
';
$myOptions=get_option('searchTagCloudOption');
}
else
$myOptions=get_option('searchTagCloudOption');

$html.= ''.__('Set the header for the Popular Searches Tag Cloud to be visible: ').'';
$html.= '

';
$html.= ''.__('Size of the biggest tag: ').'';
$html.= '

';
$html.= ''.__('Size of the smallest tag: ').'';
$html.= '

';
$html.= ''.__('Number of searches to display: ').'';
$html.= '

';
$html.= ''.__('You want to show searches from the last : ').'';
$html.= ' days

';
$html.=getMostPopularSearchesAdmin($results, 15, false);
$html.= '

'.__('Display developer credits in the Widget: ').'';
$html.= '

';
$html.= '';
$html.= '
';

//here I need the list of all searches, order by total
$html.= '';
$html.= '';

echo $html;
}

//function to show common searches to edit in the admin page. Completes the admin form.
function getMostPopularSearchesAdmin(){

$searchcloud=new searchTagCloud();
$results=$searchcloud->selectPopularSearchesforAdmin();

if (count($results)) {
$html='';
$html.='';
$html.='TermSet not Visible';
if ($do_include_successes) {
$html.='Results';
}
$html.='';
$class= '';
$counter=0;
foreach ($results as $result) {
$html.='';
$html.='Search Meter plugin is installed and activated. -- widget
' );
}
else
{
$search_tag_cloud=new searchTagCloud();
$search_tag_cloud->initializeSearchTagCloud();
}
$searchcloud=new searchTagCloud();
$searchcloud->popular_searches_tag_cloud($tags,$args);
}

function setSearchTagCloudControl()
{
echo '
To configure options go to "Settings > Popular Searches Tag Cloud" in this admin panel
';
}


Подробнее здесь: https://stackoverflow.com/questions/283 ... iven-array
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»