Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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_Mysql - Fatal编程技术网

获取所有列的Php函数

获取所有列的Php函数,php,mysql,Php,Mysql,大家好, 我已经编写了一个函数,如果给定的时间与我的表记录匹配,则显示表中的某个图像。以下是我的职责: public function image_rank_per_hour($minhour) { $sql = "SELECT * FROM phpvms_ranks ORDER BY minhours ASC"; $result = DB::get_results($sql);

大家好, 我已经编写了一个函数,如果给定的时间与我的表记录匹配,则显示表中的某个图像。以下是我的职责:

public function image_rank_per_hour($minhour)
        {
            $sql = "SELECT * FROM phpvms_ranks 
                        ORDER BY minhours ASC";
            $result = DB::get_results($sql);
            $hours = floor($minhour / 3600);
            if(intval($hours) >= 0 AND intval($hours) < 5)
                {
                    return $result[0]->rankimage;
                }
            if(intval($hours) >= 5 AND intval($hours) < 10)
                {
                    return $result[1]->rankimage;
                }
            if(intval($hours) >= 10 AND intval($hours) < 35)
                {
                    return $result[2]->rankimage;
                }
            if(intval($hours) >= 35 AND intval($hours) < 95)
                {
                    return $result[3]->rankimage;
                }
            if(intval($hours) >= 95 AND intval($hours) < 170)
                {
                    return $result[4]->rankimage;
                }
            if(intval($hours) >= 170 AND intval($hours) < 300)
                {
                    return $result[5]->rankimage;
                }
            if(intval($hours) >= 300 AND intval($hours) < 1000)
                {
                    return $result[6]->rankimage;
                }
            if(intval($hours) >= 1000 AND intval($hours) < 5000)
                {
                    return $result[7]->rankimage;
                }
            if(intval($hours) >= 5000)
                {
                    return $result[8]->rankimage;
                }

        }
公共功能图像每小时排名($minhour)
{
$sql=“从phpvms\u列中选择*
按小时ASC订购”;
$result=DB::get_results($sql);
$hours=楼层($minhour/3600);
如果(intval($hours)>=0且intval($hours)<5)
{
返回$result[0]->rankimage;
}
如果(intval($hours)>=5且intval($hours)<10)
{
返回$result[1]->rankimage;
}
如果(intval($hours)>=10且intval($hours)<35)
{
返回$result[2]->rankimage;
}
如果(intval($hours)>=35和intval($hours)<95)
{
返回$result[3]->rankimage;
}
如果(intval($hours)>=95和intval($hours)<170)
{
返回$result[4]->rankimage;
}
如果(intval($hours)>=170且intval($hours)<300)
{
返回$result[5]->rankimage;
}
如果(intval($hours)>=300且intval($hours)<1000)
{
返回$result[6]->rankimage;
}
如果(intval($hours)>=1000且intval($hours)<5000)
{
返回$result[7]->rankimage;
}
如果(整数($hours)>=5000)
{
返回$result[8]->rankimage;
}
}
它实现了我试图实现的功能,因为我的表是静态的,不会改变,但我希望能够获取其他列。($result[0]->rank)


谢谢

我不太清楚您想要什么,但可能的解决方案是什么

public function image_rank_per_hour($minHour)
  {
    $queryData = getData();
    $result = searchResults($queryData, $minHour);

    // You now have the result object for the given range.
    // and can return the entire thing, or whatever part 
    // of it you need for example:
    // return $result;
    // return $result->rankImage;
    // return $result->rank;
    return ['rank' => $result->rank, 'image' => $result->rankImage];
  }

  protected function getData()
  {
    // Side note, it is more efficient to name your columns instead of just using *.
    $sql = "SELECT * FROM phpvms_ranks 
      ORDER BY minhours ASC";
    return DB::get_results($sql);
  }

  protected function searchResults($data, $minHour)
  {
    // potentially could be defined at class level
    $rangeResource = [
      ['key' => 0, 'min' => 0,'max' => 5],
      ['key' => 1, 'min' => 5, 'max' => 10],
      ['key' => 2, 'min' => 10, 'max' => 35],
      ['key' => 3, 'min' => 35, 'max' => 95],
      ['key' => 4, 'min' => 95, 'max' => 170],
      ['key' => 5, 'min' => 170, 'max' => 300],
      ['key' => 6, 'min' => 300, 'max' => 1000],
      ['key' => 7, 'min' => 1000, 'max' => 5000],
      ['key' => 8, 'min' => 5000, 'max' => null],
    ];
    // Get formatted hours to check against.
    $hours = getHours($minHour);

    // Iterate over resource to find matching hour range.
    $result = null;
    foreach ($rangeResource as $range) {
      // If not in range go to next iteration.
      if (!checkRange($hours, $range['min'], $range['max'])) {
        continue;
      }
      // Set result and end loop.
      $result = $data[$range['key']];
      break;
    }

    return $result;
  }

  protected function getHours($minHour)
  {
    $hours = floor($minhour / 3600);
    return intval($hours);
  }

  protected function checkRange($value, $min, $max = null)
  {
    if (!empty($max)) {
      return ($value >= $min && $value < $max);
    }
    return ($value >= $min);
  }
公共功能图像每小时排名($minHour)
{
$queryData=getData();
$result=searchResults($queryData,$minHour);
//您现在拥有给定范围的结果对象。
//并且可以返回整个东西,或者任何部分
//您需要的信息,例如:
//返回$result;
//返回$result->rankImage;
//返回$result->rank;
返回['rank'=>$result->rank,'image'=>$result->rankImage];
}
受保护的函数getData()
{
//请注意,命名列比使用*更有效。
$sql=“从phpvms\u列中选择*
按小时ASC订购”;
返回DB::get_results($sql);
}
受保护的函数搜索结果($data,$minHour)
{
//可能是在类级别定义的
$rangeResource=[
['key'=>0,'min'=>0,'max'=>5],
['key'=>1,'min'=>5,'max'=>10],
['key'=>2,'min'=>10,'max'=>35],
['key'=>3,'min'=>35,'max'=>95],
['key'=>4,'min'=>95,'max'=>170],
['key'=>5,'min'=>170,'max'=>300],
['key'=>6,'min'=>300,'max'=>1000],
['key'=>7,'min'=>1000,'max'=>5000],
['key'=>8,'min'=>5000,'max'=>null],
];
//获取格式化的小时数以进行检查。
$hours=getHours($minHour);
//迭代资源以查找匹配的小时范围。
$result=null;
foreach($rangeResource作为$range){
//如果不在范围内,则转到下一次迭代。
如果(!checkRange($hours,$range['min'],$range['max'])){
继续;
}
//设置结果和结束循环。
$result=$data[$range['key']];
打破
}
返回$result;
}
受保护函数getHours($minHour)
{
$hours=楼层($minhour/3600);
返回intval(小时);
}
受保护的函数检查范围($value、$min、$max=null)
{
如果(!空($max)){
返回($value>=$min&&$value<$max);
}
返回($value>=$min);
}

如果您
$result[0]->rank
有问题吗?这样我只得到{$result[0]->rank}。我想要两个。是否要返回整个数组的
$result
?它在第29行和第43行给出了以下错误,这是一个开括号。解析错误:语法错误,第43行/home1/msflight/public_html/pilots/phpvms/core/common/PilotListData.class.php中出现意外的“[”,您使用的是什么版本的php?如果是5.4之前的版本,则必须将括号符号更改为array();表示法。我只是回顾并检查了一下,我没有看到任何遗漏的分号或结束括号,但我可能忽略了一些东西。