English 中文(简体)
Turning a spreadsheet into array and loop and call a function
原标题:

This is related to generate groups in BuddyPress.

I have a spreadsheet with (in this case) a group name, group description and slug.

I need to grab the information from the file, turn it into an array, then loop through it and call groups_create_group() every time.

I can find that function in bp-groups.php (http://www.nomorepasting.com/getpaste.php?pasteid=35217). It tells me all the parameters you need to fill in.

I m quite new to this and looking for how I can do this. Do you know how I can grab this information and turn it into an array? An loop it through and call groups_create_group() every time?

If you have any handy links regarding this as well I ll appreciate it.

最佳回答

As long as you have the function groups_create_group available (ie. The required file has been included) you should be able to do something like this

<?php

$groups = array();

if (($handle = fopen("groupData.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $group = array( group_id  =>  SOME ID ,  name  => $data[0],  description  => $data[1],  slug  => $data[2],  date_created  => gmdate( "Y-m-d H:i:s" ),  status  =>  public  );
        $groups[] = $group;
    }   
    fclose($handle);
}

foreach ($groups as $group) {
    groups_create_group($group);
} 

Note, the call to groups_create_group in the pasted code you provided was explicitly calling another method to sanitize the slug before passing it to the function. So you may want to change the assignment to the $group variable to this:

 $group = array( group_id     =>  SOME ID , 
                 name         => $data[0], 
                 description  => $data[1], 
                 slug  => groups_check_slug(sanitize_title(esc_attr($data[2]))), 
                 date_created  => gmdate( "Y-m-d H:i:s" ), 
                 status  =>  public  
 );
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签