[英]How to count each field in php
I have the below code: 我有以下代码:
function cat_fields($fields)
{
global $cfg;
$fields['cat_furl'] = str_replace('%cat_safename%', $fields['cat_safename'], $cfg->getVar('urlformat_cat'));
$fields['cat_furl'] = $cfg->getVar('site_url') . str_replace('{cat_safename}', $fields['cat_safename'], $fields['cat_furl']);
$fields['cat_flink'] = '<a id="a' . count($fields) . '" href="' . $fields['cat_furl'] . '" title="' . $fields['cat_title'] . '">' . $fields['cat_name'] . '</a>';
return $fields;
}
And i am trying to count each field of $fields['cat_flink'] = '<a id="a' . count($fields) . '"
I tried with id="a' . count($fields) . '"
but it's returning the total number of field instead of counting them. 而且我试图计算$fields['cat_flink'] = '<a id="a' . count($fields) . '"
每个字段$fields['cat_flink'] = '<a id="a' . count($fields) . '"
我尝试使用id="a' . count($fields) . '"
$fields['cat_flink'] = '<a id="a' . count($fields) . '"
id="a' . count($fields) . '"
但它返回的是字段总数,而不是对其进行计数。 How can i count them EACH
. 我怎么能指望他们EACH
。
Should be 应该
<a id="a1"....>
<a id="a2"....>
Instead it's echoing me 相反,它在呼应我
<a id="15"....>
<a id="15"....>
..... and so on
Please help me on this matter. 请帮我解决这个问题。
You're trying to count all the fields in your array; 您正在尝试计算数组中的所有字段; Quoted from the PHP Manual : 引自PHP手册 :
Counts all elements in an array, or something in an object. 计算数组中的所有元素或对象中的某些元素。
To get indices (ie the numbers you're looking for), you will have to set a starting number and loop over all your fields until you've got them all, and increment that number everytime your walk through the loop. 要获取索引(即您要查找的数字),您将必须设置一个起始数字并遍历所有字段,直到获得所有索引,并在每次遍历循环时递增该数字。
Your method however, seems to be for only one iteration, which means you would have to store your number elsewhere. 但是,您的方法似乎仅用于一次迭代,这意味着您必须将数字存储在其他位置。 A good way to do this would be adding the number as a parameter to your function: 一个好的方法是将数字作为参数添加到函数中:
function cat_fields($fields, $number)
{
global $cfg;
$fields['cat_furl'] = str_replace('%cat_safename%', $fields['cat_safename'], $cfg->getVar('urlformat_cat'));
$fields['cat_furl'] = $cfg->getVar('site_url') . str_replace('{cat_safename}', $fields['cat_safename'], $fields['cat_furl']);
$fields['cat_flink'] = '<a id="a' . $number . '" href="' . $fields['cat_furl'] . '" title="' . $fields['cat_title'] . '">' . $fields['cat_name'] . '</a>';
return $fields;
}
Having done that you should edit the loop (I presume) you're using to call the function multiple times: 完成后,您应该编辑要多次调用该函数的循环(我想):
for ($i = 1; $i < count($yourArray); $i++) {
// code
$foo = cat_fields($yourArray, $i);
// more code
}
Hope that helps. 希望能有所帮助。
EDIT : 编辑 :
I've edited the pastebin from lines 131-141: 我已经从第131-141行编辑了pastebin:
if(($rs = $db->execute($sql)) && (!$rs->EOF))
{
$i = 1;
while(!$rs->EOF)
{
$rs->fields = seoscripts_prepare_cat_fields($rs->fields, $i);
$tpl->assign_array_d('tool_category_row', $rs->fields);
$tpl->parse_d('tool_category_row');
$rs->MoveNext();
$i += 1;
}
}
Counting like that count($fields)
returns the numbers of fields you have you need to add a integer like i=0;
像count($fields)
这样的count($fields)
将返回您需要添加i=0;
的整数的字段数i=0;
and increase it i++;
和增加它i++;
Try this 尝试这个
function cat_fields($fields)
{
global $cfg;
$fields['cat_furl'] = str_replace('%cat_safename%', $fields['cat_safename'], $cfg->getVar('urlformat_cat'));
$fields['cat_furl'] = $cfg->getVar('site_url') . str_replace('{cat_safename}', $fields['cat_safename'], $fields['cat_furl']);
$str = '';
$i = 0;
foreach($fields as $field)
{
$str . = '<a id="a' . $i . '" href="' . $fields['cat_furl'] . '" title="' . $fields['cat_title'] . '">' . $fields['cat_name'] . '</a>';
$i++;
}
$fields['cat_flink'] = $str;
return $fields;
}
Try this 尝试这个
function cat_fields($fields)
{
global $cfg;
$fields['cat_furl'] = str_replace('%cat_safename%', $fields['cat_safename'], $cfg->getVar('urlformat_cat'));
$fields['cat_furl'] = $cfg->getVar('site_url') . str_replace('{cat_safename}', $fields['cat_safename'], $fields['cat_furl']);
$i=0;
foreach($fields as $field)
{
$fields['cat_flink'] = '<a id="a'.$i.'" href="' . $field['cat_furl'] . '" title="' . $field['cat_title'] . '">' . $field['cat_name'] . '</a>';
i++;
}
return $fields;
}
if you are counting an array then 如果您要计算数组,则
echo count($array); 回声计数($ array);
will give the count of every element in the array. 将给出数组中每个元素的计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.