如何根据结果更改数字

时间:2014-02-07 04:32:12

标签: php

我对前几天得到帮助的事情提出了跟进问题(No Table Three Column Category Layout)。 脚本如下:

$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$id = (isset($parent_node['cat_id'])) ? $parent_node['cat_id'] : $id;
$catalist = '';
if ($parent_node['left_id'] != 1)
{
    $children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
    $childarray = array($id);
    foreach ($children as $k => $v)
    {
        $childarray[] = $v['cat_id'];
    }
    $catalist = '(';
    $catalist .= implode(',', $childarray);
    $catalist .= ')';
    $all_items = false;
}

$NOW = time();

/*
specified category number
look into table - and if we don't have such category - redirect to full list
*/
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE cat_id = " . $id;
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$category = mysql_fetch_assoc($result);

if (mysql_num_rows($result) == 0)
{
    // redirect to global categories list
    header ('location: browse.php?id=0');
    exit;
}
else
{
    // Retrieve the translated category name
    $par_id = $category['parent_id'];
    $TPL_categories_string = '';
    $crumbs = $catscontrol->get_bread_crumbs($category['left_id'], $category['right_id']);
    for ($i = 0; $i < count($crumbs); $i++)
    {
        if ($crumbs[$i]['cat_id'] > 0)
        {
            if ($i > 0)
            {
                $TPL_categories_string .= ' &gt; ';
            }
            $TPL_categories_string .= '<a href="' . $system->SETTINGS['siteurl'] . 'browse.php?id=' . $crumbs[$i]['cat_id'] . '">' . $category_names[$crumbs[$i]['cat_id']] . '</a>';
        }
    }

    // get list of subcategories of this category
    $subcat_count = 0;
    $query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . $id . " ORDER BY cat_name";
    $result = mysql_query($query);
    $system->check_mysql($result, $query, __LINE__, __FILE__);
    $need_to_continue = 1;
    $cycle = 1;
    $column = 1;

    $TPL_main_value = '';
    while ($row = mysql_fetch_array($result))
    {
        ++$subcat_count;

        if ($cycle == 1)
        {
            $TPL_main_value .= '<div class="col'.$column.'"><ul>' . "\n";

        }
        $sub_counter = $row['sub_counter'];
        $cat_counter = $row['counter'];
        if ($sub_counter != 0)
        {
            $count_string = ' (' . $sub_counter . ')';
        }
        else
        {
            if ($cat_counter != 0)
            {
                $count_string = ' (' . $cat_counter . ')';
            }
            else
            {
                $count_string = '';
            }
        }
        if ($row['cat_colour'] != '')
        {
            $BG = 'bgcolor=' . $row['cat_colour'];
        }
        else
        {
            $BG = '';
        }

        // Retrieve the translated category name
        $row['cat_name'] = $category_names[$row['cat_id']];
        $catimage = (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '';
        $TPL_main_value .= "\t" . '<li>' . $catimage . '<a href="' . $system->SETTINGS['siteurl'] . 'browse.php?id=' . $row['cat_id'] . '">' . $row['cat_name'] . $count_string . '</a></li>' . "\n";

        ++$cycle;
        if ($cycle == 7)   // <---- here
        {

            $cycle = 1;
            $TPL_main_value .= '</ul></div>' . "\n";
    ++$column;
        }
    }

    if ($cycle >= 2 && $cycle <= 6)   // <----  here  minus 1
    {

        while ($cycle < 7)  //  <---- and here
        {
            $TPL_main_value .= '    <p>&nbsp;</p>' . "\n";
            ++$cycle;


        }
        $TPL_main_value .= '</ul></div>'.$number.'
' . "\n";

    }

我需要将生成的链接分成三列以适合我的html布局。 我们通过更改标有“//&lt; ---- here”的代码中的数字来完成此操作。 因为每次返回的链接数量可能不同,我试图弄清楚如何动态更改这些数字。我尝试使用

$number_a = mysql_num_rows($result);
$number_b = $number_a / 3;
$number_b = ceil($number_b);
$number_c = $number_b - 1;

然后用$number_b$number_c替换这些数字,但这不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如前所述,您可以使用mod (%) function来执行此操作。

基本上它的作用是在分裂后得到余数。所以,如果你说11 % 3,你将获得2,因为这是除法后的余数。然后,您可以使用它来检查数字何时可被3整除(余数将为零),并在代码中插入结束</div>

以下是有关如何使用它在每3列之后插入换行符的简化示例:

$cycle = 1;
$arr = range (1, 20);
$len = sizeof ($arr);

for ( ; $cycle <= $len; $cycle++)
{
    echo "{$arr[$cycle - 1]}     ";

    if ($cycle % 3 == 0)
    {
        echo "\n";
    }
}

echo "\n\n";