Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组限制?_Php - Fatal编程技术网

PHP数组限制?

PHP数组限制?,php,Php,朋友们,我是php新手。我在玩数组。因此,我无法使用数组解决一个登录问题。在这个程序中,我想显示数组限制。我有很多数据,它们来自数据库。但是我想显示限制数据,比如(仅10篇文章)。我知道MYSQL查询可以显示限制数据。但我正在尝试使用数组。所以,请帮助解决这个逻辑,谢谢 这是代码 $reverse = array_reverse($showpost, true); foreach ($reverse as $key=>$postvalue) { $latestpost = exp

朋友们,我是php新手。我在玩数组。因此,我无法使用数组解决一个登录问题。在这个程序中,我想显示数组限制。我有很多数据,它们来自数据库。但是我想显示限制数据,比如(仅10篇文章)。我知道MYSQL查询可以显示限制数据。但我正在尝试使用数组。所以,请帮助解决这个逻辑,谢谢

这是代码

$reverse = array_reverse($showpost, true);

foreach ($reverse as $key=>$postvalue) {
    $latestpost = explode(',', $postvalue['postcategory']);

    if (in_array("3", $latestpost)) {
        // result here...

    }

}
我已将类别保存为这种格式(1,2,3,4,5,6)。这就是我使用explode()函数的原因。
我的数据库字段名称是(post\u id、postname、postcategory、postdisc等)。

不确定我是否正确理解了该问题,但我认为它可能与


您可以使用
array\u chunk()
函数

代码:

<?php
$reverse = array_reverse($showpost, true);

foreach($reverse as $key=>$postvalue){
    $latestpost = explode(',', $postvalue['postcategory']);
    $chunks = array_chunk($latestpost, 3);
    print_r($chunks);
}
?>

如果您只想显示10行,而
这里的结果…
是显示每篇文章的地方,那么类似的方法可能会起作用:

$postsShown = 0;
$reverse = array_reverse($showpost, true);

foreach ($reverse as $key => $postvalue) {

    if ($postsShown >= 10) {
        break;
    }

    $latestpost = explode(',', $postvalue['postcategory']);

    if (in_array("3", $latestpost)) {

        // result here...

       $postsShown++;
   }
}

所有这些都是使用
$postsShown
变量计算显示的帖子数量,并在显示新帖子时递增。当该变量达到10(即显示10个POST)时,将使用
break
命令终止循环。

也许您可以使用会话变量来节省用户记录日志的次数,并在服务器上验证它

使用此代码,您可以创建会话变量:

session_start();
$_SESSION['name_of_my_variable'] = 'value of my session variable';
session_start();
$_SESSION['log_counter'] = 1;
例如,每次用户尝试登录时,您都可以增加会话变量中计数器的值:

session_start();
$_SESSION['name_of_my_variable'] = 'value of my session variable';
session_start();
$_SESSION['log_counter'] = 1;
第一次需要创建会话变量时:

session_start();
$_SESSION['name_of_my_variable'] = 'value of my session variable';
session_start();
$_SESSION['log_counter'] = 1;
在接下来的尝试中,您可以按如下方式递增计数器:

session_start();
$log_counter = $_SESSION['log_counter'];
$log_counter++;
$_SESSION['log_counter'] = $log_counter;
检查用户是否已达到限制:

session_start();
if ($_SESSION['log_counter'] == 10)
{
    echo "Limit reached";
}
这是最后的代码

// init session variables
session_start();

// Check if exist the session variable
if (isset($_SESSION['log_counter']))
{
    // Enter here if the session variable exist

    // check if the log_counter is equals to the limit
    if ($_SESSION['log_counter'] == 10)
    {
        echo "Limit reached";
    }else
    {
       // increase the counter
       $log_counter = $_SESSION['log_counter'];
       // this increate 1 more to the log_counter session variable
       $log_counter++;
       // here we save the new log_counter value
       $_SESSION['log_counter'] = $log_counter;
    }
}else
{
    // Enter here if not exist the session variable

    // this create the log_counter session variable
    $_SESSION['log_counter'] = 1;
}

但是如何使用此函数显示数据。因为$output['postname']不起作用。很抱歉,您的逻辑不起作用…但是如何使用$chunks['postnane']显示我的数据此方法不起作用。$chunks是3个值对中的数组只需使用foreach循环或使用索引,就像如果要打印第一对,然后对下一个数组使用$chunks[0]那样,只需增加索引1、2