我基本上有一个表单,一旦按下提交按钮,它就会从MySQL传递数据。
我有一个从MySQL检索到的数组,想要为每个元素调用相同的函数。我在过去的14个小时内尝试过for循环,我无法让它为数组中的每个元素运行相同的函数。
<?php
foreach($pClass->retrieveAllInfo() as $data)
{
echo '
<form method="post" action"">
<input type="hidden" name="post_url" value="'.$data['p_Url'].'">'.$data['p_Url'].'
<input type="hidden" name="post_description" value="'.$data['p_Description'].'">'.$data['p_Description'].'
<input type="hidden" name="post_image" value="'.$data['p_Image'].'">'.$data['p_Image'].'
<ul class="items">';?>
<?php
foreach($pClass->retrieveBoards() as $data)
{
echo '
<li><input type="checkbox" name="post_board_id[]" value="'.$data['boardiD'].'">'.$data['boardName'].'</li>
';
}
?>
<?php
echo '
</ul>
<input name="post_info" type="submit" value="Post">
</form>';
?>
在同一档案中,我有以下内容..
if(isset($_POST['post_info']))
{
pinLogin(); //calls and runs the function
foreach($_POST['post_board_id'] as $data)
{
echo '<pre>'; var_dump($data); echo '</pre>'; //just retrieving the arrays
}
}
function pinLogin() // logs into website with headers
{
$ch = // ch is a variable from cURL
$cookie = '/cookie.txt'; //cookie session
$url = 'http://www.website.com/'; // website to post data to
pinPost($ch, $cookie, $url);
}
function pinPost($ch, $cookie, $url) // posts the information below to the website above
{
$boardId = // here needs to be the board id that needs to be runned for each array..
$postDesc = $_POST['post_description'];
$postUrl = $_POST['post_url'];
$postImage = $_POST['post_image'];
}
所以基本上,当你按下post_info
按钮时,它会运行pinLogin()
函数,该函数使用从表单发送的信息运行pinPost()
,事情是{{1 }};是一个数组,$_POST['post_board_id']
得到以下结果:
var_dump
我基本上需要将每个数组发布为string(18) "136234026166934321"
string(18) "286119451265381250"
string(18) "106468047379795203"
string(18) "468022654964640361"
string(18) "409757334785529893"
string(18) "409757334785575605"
string(18) "490681390589888313"
内的$boardId
,然后为所有其他数组运行pinPost()
。为了让它变得非常容易理解,这正是我想要做的。
当我点击帖子按钮时,它会抓取第一个数组pinPost()
,将其作为string(18) "136234026166934321"
提交给pinPost()
,然后它将返回到开头并运行第二个数组,$boardId
直到所有数组都被提交,几乎只是将相同的信息发布到不同的板上,这正是我想要做的。
我试过做一个for循环并在里面运行string(18) "286119451265381250"
但是我无法将Board ID传递给pinPost()
函数,所以它不会运行。
答案 0 :(得分:2)
因此,您可以使用一些不同的技术来实现这一目标。我的偏好是使用 array_walk ,因为它的回调接受数组的键和值。 array_map 也可以在这里使用,但它只能在回调中使用数组的值。
$postBoards = $_POST['post_board_id'];
function pinLogin()
{
$ch = // ch is a variable from cURL
$cookie = '/cookie.txt'; //cookie session
$url = 'http://www.website.com/';
// Run the pinPost callback for each element of $postBoards
// with your custom user data from this function
array_walk("pinPost", $postBoards, array($ch, $cookie, $url);
}
function pinPost($boardId, $boardKey, $data)
{
// You have your boardId for each submitted board available here
$postDesc = $_POST['post_description'];
$postUrl = $_POST['post_url'];
$postImage = $_POST['post_image'];
// $data contains your pinPost data for example
$ch = $data[0];
}
这使得pinPost
函数成为$postBoards
数组的每个元素的回调函数,并且在您调用pinLogin()
后将按顺序为每个数组条目运行。
答案 1 :(得分:1)
您可以尝试更新代码,如下所示
/**
* logs into website with headers
*/
function pinLogin() {
$ch = // ch is a variable from cURL
$cookie = '/cookie.txt'; //cookie session
$url = 'http://www.website.com/'; // website to post data to
/**
* here iterate over all of your board id's and pass them to pin post function. The pinPost function
* will be run 18 times
*/
foreach($_POST['post_board_id'] as $boardId) {
/**
* @var $boardId string this is the 18 characters long stringg $boardid
*/
pinPost($ch, $cookie, $url, $boardId);
}
}
/**
* posts the information below to the logged in website
*
* @param $ch
* @param $cookie
* @param $url
* @param $boardId id of the board you want to pin post to (NEW PARAMETER)
*/
function pinPost($ch, $cookie, $url, $boardId) {
//$boardId is already here
$postDesc = $_POST['post_description'];
$postUrl = $_POST['post_url'];
$postImage = $_POST['post_image'];
/* do your stuff here */
}
答案 2 :(得分:-1)
变量名称可能存在问题。 尝试这样的事情:
foreach ($pClass->retrieveAllInfo() as $data) {
foreach ($pClass->retrieveBoards() as $boards) {
}
}