使用查询和查询表循环。 MySQL的&安培; PHP [英] looping with a query and lookup table. mysql & php

查看:92
本文介绍了使用查询和查询表循环。 MySQL的&安培; PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点难度搞清楚一个循环。

他们不是我的强项。 ;)

它使用一个类查找作为一个查找表,看起来像这样:(留下了大量的行为简洁)

 类查找{
   保护$查找=阵列(
    阵列('rider_count'= GT;'1','heat_count'=>'1','riders_in_heat_1'=>'1'),
    阵列('rider_count'= GT;'2','heat_count'=>'1','riders_in_heat_1'=>2),
    阵列('rider_count'= GT;'3','heat_count'=>'1','riders_in_heat_1'=>'3'),
    阵列('rider_count'= GT;'4','heat_count'=>'1','riders_in_heat_1'=>'4'),
    阵列('rider_count'= GT;'5','heat_count'=>'1','riders_in_heat_1'=>'5'),
    阵列('rider_count'= GT;'6','heat_count'=>'1','riders_in_heat_1'=>'6'),
    阵列('rider_count'= GT;'7','heat_count'=>'1','riders_in_heat_1'=>'7'),
    阵列('rider_count'= GT;'8','heat_count'=>'2','riders_in_heat_1'=>'4','riders_in_heat_2'=>'4'),
    阵列('rider_count'= GT;'9','heat_count'=>'2','riders_in_heat_1'=>'5','riders_in_heat_2'=>'4'),
    阵列('rider_count'= GT;'10','heat_count'=>'2','riders_in_heat_1'=>'5','riders_in_heat_2'=>'5'),
    阵列('rider_count'= GT;'11','heat_count'=>'2','riders_in_heat_1'=>'6','riders_in_heat_2'=>'5'),
    阵列('rider_count'= GT;'12','heat_count'=>'2','riders_in_heat_1'=>'6','riders_in_heat_2'=>'6'),
    阵列('rider_count'= GT;'13','heat_count'=>'2','riders_in_heat_1'=>'7','riders_in_heat_2'=>'6'),
    阵列('rider_count'= GT;'14','heat_count'=>'2','riders_in_heat_1'=>'7','riders_in_heat_2'=>'7')
   );
  公共功能选择($领域,$值)
  {
    $名单=阵列();
    的foreach($这个 - >查找为$计数)
    {
      如果($计数[$场] == $值)
      {
        $名单[] = $计数;
      }
    }
    返回$清单;
  }
}$类=新的查找();

我的PHP:

 < PHP        //获取事件条目
      功能getEntries($标识码,$限制,$偏移)
      {
        全球$分贝;
        $ getentries = $ DB-GT&; prepare(选择entry_id FROM tbl_event_entries WHERE事项标识=:EVENT_ID AND标识码=:标识码LIMIT:偏移:限制);
        $ getentries-> bindValue(':EVENT_ID',$ _GET [事项标识']);
        $ getentries-> bindValue(':标识码',$标识码);
        $ getentries-> bindValue(':极限',$限制);
        $ getentries-> bindValue(':偏移',$偏移);
        $ getentries->执行();
        而($ R = $ getentries->取(PDO :: FETCH_ASSOC))返回$ R ['entry_id'];
      }
        //得到每类条目的数量
        //取得类的事件
        $ geteventclasses = $ DB-GT&; prepare(选择标识码从tbl_event_classes WHERE事项标识=:事项标识);
        $ geteventclasses-> bindValue(':EVENT_ID',$ _GET [事项标识']);
        $ geteventclasses->执行();
        而($ R = $ geteventclasses->取(PDO :: FETCH_ASSOC))
        {
        $ getentriesperclass = $ DB-GT&; prepare(选择entry_id FROM tbl_event_entries WHERE标识码=:标识码和事项标识=:事项标识);
        $ getentriesperclass-> bindValue(':标识码',$ R ['标识码']);
        $ getentriesperclass-> bindValue(':EVENT_ID',$ _GET [事项标识']);
        $ getentriesperclass->执行();
        $ r2count = $ getentriesperclass-> rowCount时();
        $计数[$ R ['标识码'] = $ r2count;
        }
          的foreach($算作$类=> $ rider_count)
          {
            $名单= $类 - >选择('rider_count',$ rider_count);            回声类。 $类; ridercount。 $列表[0] ['rider_count'];热。 。$列表[0] ['heat_count']升温,包括:其中; BR> \\ N的;
            为($ i = 1; $ I< = $名单[0] ['heat_count']; $ I ++)
            {
              如果($列表[0] ['heat_count'] 0)
              {
                为($ RIH = 1; $ RIH< = $名单[0] ['_ riders_in_heat'$ I]; $ RIH ++)
                {
                  $偏移量= 1;
                  回声<立GT;热。 $岛:。 getEntries($类,$列表[0] ['_ riders_in_heat'$ I],$偏移)< /李>中。
                }
                $抵消= $抵消+ $列表[0] ['_ riders_in_heat'$我。]
              }            }
              回声< / UL>中;
          }
       ?>

这最终将打造一个更新查询分配heat_nbr和heat_position每个entry_id。

的任务是从一个标识码采取rider_count,打破它,以便我们只有每个热种族7骑手一个最大均匀地分布车手每个热量。

查找是如何确定的分配是如何发生的。这部分似乎可以正常使用。我只是停留在如何让分配到一个位置的每个骑手。

我已经尝试了几种不同的方法和我得到的答案,这是尽可能接近。

在正确的方向轻推将大大AP preciated!

请参阅什么,我至今这里的输出:

http://home.garyeterry.com/midam/createheats.php? EVENT_ID = 113

感谢

表结构:

  CREATE TABLE IF NOT EXISTS`tbl_event_entries`(
 `entry_id` INT(11)NOT NULL AUTO_INCREMENT,
 `event_id` INT(1)DEFAULT NULL,
 `racer_id` INT(4)DEFAULT NULL,
 `class_id` INT(1)DEFAULT NULL,
 `racing_nbr` VARCHAR(4)DEFAULT NULL,
 `machine_cc` INT(2)DEFAULT NULL,
 `brand_id` INT(1)DEFAULT NULL,
 `overall_finish` INT(1)DEFAULT NULL,
 `xtra_int1` VARCHAR(10)DEFAULT NULL,
 `heat_nbr` INT(1)DEFAULT NULL,
 `heat_position` INT(1)DEFAULT NULL,
 `heat_row` INT(1)DEFAULT NULL,
 `heat_finish` INT(1)DEFAULT NULL,
  PRIMARY KEY(`entry_id`)
  UNIQUE KEY`entry_id`(`entry_id`)
)ENGINE = InnoDB的默认字符集= UTF8 AUTO_INCREMENT = 165;


解决方案

今天早上我坚持了下来,知道环路是我的问题,我知道是接近我所需要的。的$一点按摩在我的主循环失调和它的工作,因为我需要它。 ;)

再次感谢那些提供帮助。下面是code的作品。

 < PHP
在session_start();
require_once../db.class.php
require_once../functions.class.php$ F =新功能();
$ EVENT_ID = $ _ POST ['身份证'];
标题(内容类型:应用程序/ JSON的;字符集= UTF-8);
// - 函数名称:getLookup
// - params:一个价值$
// - 目的:让车手的数量和回报的车手和热分布
功能getLookup($值)
{
    全球$分贝;
    $语句= $ DB-GT&; prepare(SELECT * FROM tbl_lookup WHERE rider_count =:值);
    $ stmt-> bindValue(':价值',$值);
    $ stmt->执行();
    返回$ R = $ stmt->使用fetchall(PDO :: FETCH_ASSOC);
}
// - 函数名称:getEntries
//获取事件条目
// - 目的:让项,以确定比赛的订单
功能getEntries($标识码,$限制,$偏移)
{
    全球$分贝;
    全球$ EVENT_ID;
    $ getentries = $ DB-GT&; prepare(选择entry_id FROM tbl_event_entries WHERE事项标识=:EVENT_ID AND标识码=:标识码LIMIT:偏移:限制);
    $ getentries-> bindValue(':EVENT_ID',$事项标识);
    $ getentries-> bindValue(':标识码',$标识码);
    $ getentries-> bindValue(':极限',$限制);
    $ getentries-> bindValue(':偏移',$偏移);
    $ getentries->执行();
    而($ R = $ getentries->取(PDO :: FETCH_ASSOC))返回$ R ['entry_id'];
}
// - 函数名称:getEntriesPerClass
//得到每类条目的数量
// - 目的:得到每类的条目来养活查找函数
功能getEntriesPerClass()
{
    全球$分贝;
    $ getentriesperclass = $ DB-GT&; prepare(选择tbl_event_entries.class_id,COUNT(tbl_event_entries.entry_id)AS车手,tbl_moto_order.moto_nbr
                                从tbl_event_entries
                                JOIN tbl_moto_order ON tbl_event_entries.event_id = tbl_moto_order.event_id AND tbl_event_entries.class_id = tbl_moto_order.class_id
                                WHERE tbl_event_entries.event_id =:EVENT_ID
                                GROUP BY tbl_event_entries.class_id
                                ORDER BY moto_nbr;);
    $ getentriesperclass-> bindValue(':EVENT_ID',$ _ POST ['身份证']);
    $ getentriesperclass->执行();
    $数=阵列();
    而($ R = $ getentriesperclass->取(PDO :: FETCH_ASSOC))$计数[$ R ['标识码'] = $ R ['车手'];
    返回$计数;
}$计数= getEntriesPerClass();
$种族= 1; //设置为增值race_nbr领域,增量1时间为每年总热量比赛的foreach($算作$类=> $ rider_count)
{
    $名单= getLookup($ rider_count); //查找来获得每个热热的比赛数量和在线式UPS。
    //使用计数更新事件类表,所以我们不必像疯了似的打造循环打印
    $ assign_class_counts = $ DB-GT&; prepare(UPDATE tbl_event_classes SET nbr_of_heats =:nbr_of_heats,nbr_of_riders_in_heats =:nbr_of_riders_in_heats WHERE事项标识=:EVENT_ID AND标识码=:标识码);
    $ assign_class_counts-> bindValue(':nbr_of_heats',$名单[0] ['heat_count']);
    $ assign_class_counts-> bindValue(':nbr_of_riders_in_heats',$名单[0] ['rider_count']);
    $ assign_class_counts-> bindValue(':EVENT_ID',$ _ POST ['身份证']);
    $ assign_class_counts-> bindValue(':标识码,$级);
    $ assign_class_counts->执行();    $偏移= 0; //设置偏移脚主查询给我们的车手在每个散热的位置。用在getEntries查询。
    为($ i = 1; $ I< = $名单[0] ['heat_count']; $ I ++)
    {
        如果($列表[0] ['heat_count'] 0)
        {
            为($ RIH = 1; $ RIH< = $名单[0] ['_ riders_in_heat'$ I]; $ RIH ++)
            {
                $ assignheats = $ DB-GT&; prepare(UPDATE tbl_event_entries SET heat_nbr =:heat_nbr,heat_position =:heat_position,race_nbr =:race_nbr WHERE entry_id =:entry_id);
                $ assignheats-> bindValue(':heat_nbr',$ I);
                $ assignheats-> bindValue(':heat_position',$ RIH);
                $ assignheats-> bindValue(':race_nbr',$比赛);
                $ assignheats-> bindValue(':entry_id',getEntries($类,$列表[0] ['_ riders_in_heat'$ i],$偏移));
                $ assignheats->执行();
                $抵消++;
            }
            $比赛++;
        }
    }
}回声json_en code(阵列('状态'=>真));

I am having a bit of difficulty figuring out a loop.

They're not really my strong suit. ;)

It uses a class "lookup" as a lookup table that looks like this: (left out a lot of rows for brevity)

class lookup {
   protected $lookup = array(
    array('rider_count' => '1', 'heat_count' => '1', 'riders_in_heat_1' => '1'),
    array('rider_count' => '2', 'heat_count' => '1', 'riders_in_heat_1' => '2'),
    array('rider_count' => '3', 'heat_count' => '1', 'riders_in_heat_1' => '3'),
    array('rider_count' => '4', 'heat_count' => '1', 'riders_in_heat_1' => '4'),
    array('rider_count' => '5', 'heat_count' => '1', 'riders_in_heat_1' => '5'),
    array('rider_count' => '6', 'heat_count' => '1', 'riders_in_heat_1' => '6'),
    array('rider_count' => '7', 'heat_count' => '1', 'riders_in_heat_1' => '7'),
    array('rider_count' => '8', 'heat_count' => '2', 'riders_in_heat_1' => '4', 'riders_in_heat_2' => '4'),
    array('rider_count' => '9', 'heat_count' => '2', 'riders_in_heat_1' => '5', 'riders_in_heat_2' => '4'),
    array('rider_count' => '10', 'heat_count' => '2', 'riders_in_heat_1' => '5', 'riders_in_heat_2' => '5'),
    array('rider_count' => '11', 'heat_count' => '2', 'riders_in_heat_1' => '6', 'riders_in_heat_2' => '5'),
    array('rider_count' => '12', 'heat_count' => '2', 'riders_in_heat_1' => '6', 'riders_in_heat_2' => '6'),
    array('rider_count' => '13', 'heat_count' => '2', 'riders_in_heat_1' => '7', 'riders_in_heat_2' => '6'),
    array('rider_count' => '14', 'heat_count' => '2', 'riders_in_heat_1' => '7', 'riders_in_heat_2' => '7')
   );
  public function select ($field, $value)
  {
    $list = array();
    foreach ($this->lookup as $count)
    {
      if ($count[$field] == $value)
      {
        $list[] = $count;
      }
    }
    return $list;
  }
}

$classes = new lookup();

my php:

      <?php

        // get entries for the event
      function getEntries($class_id, $limit, $offset)
      {
        global $db;
        $getentries = $db->prepare("SELECT entry_id FROM tbl_event_entries WHERE event_id = :event_id AND class_id = :class_id LIMIT :offset, :limit");
        $getentries->bindValue(':event_id', $_GET['event_id']);
        $getentries->bindValue(':class_id', $class_id);
        $getentries->bindValue(':limit', $limit);
        $getentries->bindValue(':offset', $offset);
        $getentries->execute();
        while ($r = $getentries->fetch(PDO::FETCH_ASSOC)) return $r['entry_id'];
      }
        // get count of entries per class
        // get classes for the event
        $geteventclasses = $db->prepare("SELECT class_id FROM tbl_event_classes WHERE event_id = :event_id");
        $geteventclasses->bindValue(':event_id', $_GET['event_id']);
        $geteventclasses->execute();
        while ($r = $geteventclasses->fetch(PDO::FETCH_ASSOC))
        {
        $getentriesperclass = $db->prepare("SELECT entry_id FROM tbl_event_entries WHERE class_id = :class_id AND event_id = :event_id");
        $getentriesperclass->bindValue(':class_id', $r['class_id']);
        $getentriesperclass->bindValue(':event_id', $_GET['event_id']);
        $getentriesperclass->execute();
        $r2count = $getentriesperclass->rowCount();
        $counts[$r['class_id']] = $r2count;
        }
          foreach ($counts as $class => $rider_count)
          {
            $list = $classes->select('rider_count', $rider_count);

            echo "class: ".  $class ."; ridercount: " . $list[0]['rider_count'] ."; heats: ". $list[0]['heat_count'] ." heats, consisting of :<br>\n";


            for ($i = 1; $i <= $list[0]['heat_count']; $i++)
            {
              if ($list[0]['heat_count'] > 0)
              {
                for ($rih = 1; $rih <= $list[0]['riders_in_heat_'.$i]; $rih++)
                {
                  $offset = 1;
                  echo "<li>Heat ". $i ." : ". getEntries($class, $list[0]['riders_in_heat_'.$i], $offset) ." </li>";
                }
                $offset = $offset + $list[0]['riders_in_heat_'.$i];
              }

            }
              echo "</ul>";
          }
       ?>

This will eventually build an update query to assign "heat_nbr" and "heat_position" to each entry_id.

The task is to take rider_count from a class_id and break it up so that we only have a max of 7 riders per heat race and evenly distribute riders to each heat.

The lookup is how we determine how the distribution happens. That part seems working perfectly. I'm just stuck on how to get each rider assigned to a position.

I've tried several different methods and this is as close as I have gotten to the answer.

A nudge in the right direction would be much appreciated!

See the output of what I have so far here:

http://home.garyeterry.com/midam/createheats.php?event_id=113

Thanks

table structure:

CREATE TABLE IF NOT EXISTS `tbl_event_entries` (
 `entry_id` int(11) NOT NULL AUTO_INCREMENT,
 `event_id` int(1) DEFAULT NULL,
 `racer_id` int(4) DEFAULT NULL,
 `class_id` int(1) DEFAULT NULL,
 `racing_nbr` varchar(4) DEFAULT NULL,
 `machine_cc` int(2) DEFAULT NULL,
 `brand_id` int(1) DEFAULT NULL,
 `overall_finish` int(1) DEFAULT NULL,
 `xtra_int1` varchar(10) DEFAULT NULL,
 `heat_nbr` int(1) DEFAULT NULL,
 `heat_position` int(1) DEFAULT NULL,
 `heat_row` int(1) DEFAULT NULL,
 `heat_finish` int(1) DEFAULT NULL,
  PRIMARY KEY (`entry_id`),
  UNIQUE KEY `entry_id` (`entry_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=165 ;

解决方案

I kept at it this morning, knowing that the loop was my problem and knowing I was close to what I need. A little massaging of the $offset in my main loop and it's working as I need it to. ;)

Thanks again for those that offered help. Below is the code that works.

<?php
session_start();
require_once "../db.class.php";
require_once "../functions.class.php";

$f = new functions();
$event_id = $_POST['id'];
header("Content-Type:application/json; Charset=utf-8");


// -- Function Name : getLookup
// -- Params : $value
// -- Purpose : get count of riders and return distribution of riders and heats
function getLookup($value)
{
    global $db;
    $stmt = $db->prepare("SELECT * FROM tbl_lookup WHERE rider_count = :value");
    $stmt->bindValue(':value', $value);
    $stmt->execute();
    return $r = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// -- Function Name : getEntries
// get entries for the event
// -- Purpose : get entries to determine race orders
function getEntries($class_id, $limit, $offset)
{
    global $db;
    global $event_id;
    $getentries = $db->prepare("SELECT entry_id FROM tbl_event_entries WHERE event_id = :event_id AND class_id = :class_id LIMIT :offset, :limit");
    $getentries->bindValue(':event_id', $event_id);
    $getentries->bindValue(':class_id', $class_id);
    $getentries->bindValue(':limit', $limit);
    $getentries->bindValue(':offset', $offset);
    $getentries->execute();
    while ($r = $getentries->fetch(PDO::FETCH_ASSOC)) return $r['entry_id'];
}
// -- Function Name : getEntriesPerClass
// get count of entries per class
// -- Purpose : get entries per class to feed the lookup function  
function getEntriesPerClass ()
{
    global $db;
    $getentriesperclass = $db->prepare("SELECT tbl_event_entries.class_id, COUNT(tbl_event_entries.entry_id) AS riders, tbl_moto_order.moto_nbr
                                FROM tbl_event_entries
                                JOIN tbl_moto_order ON tbl_event_entries.event_id = tbl_moto_order.event_id AND tbl_event_entries.class_id = tbl_moto_order.class_id
                                WHERE tbl_event_entries.event_id = :event_id
                                GROUP BY tbl_event_entries.class_id
                                ORDER BY moto_nbr;");
    $getentriesperclass->bindValue(':event_id', $_POST['id']);
    $getentriesperclass->execute();
    $counts = array();
    while ($r = $getentriesperclass->fetch(PDO::FETCH_ASSOC)) $counts[$r['class_id']] = $r['riders'];
    return $counts;
}

$counts = getEntriesPerClass();
$race = 1; //set to add value to race_nbr field, increments 1 time for each total heat race

foreach ($counts as $class => $rider_count)
{
    $list = getLookup($rider_count); // lookup to get heat race counts and line ups for each heat.
    // update event classes table with counts so we don't have to loop like crazy to build the printouts
    $assign_class_counts = $db->prepare("UPDATE tbl_event_classes SET nbr_of_heats = :nbr_of_heats, nbr_of_riders_in_heats = :nbr_of_riders_in_heats WHERE event_id = :event_id AND class_id = :class_id");
    $assign_class_counts->bindValue(':nbr_of_heats', $list[0]['heat_count']);
    $assign_class_counts->bindValue(':nbr_of_riders_in_heats', $list[0]['rider_count']);
    $assign_class_counts->bindValue(':event_id',  $_POST['id']);
    $assign_class_counts->bindValue(':class_id', $class);
    $assign_class_counts->execute();

    $offset = 0; // set offset to feet the main query to give us the position in each heat for a rider. Used in the getEntries query.
    for ($i = 1; $i <= $list[0]['heat_count']; $i++)
    {
        if ($list[0]['heat_count'] > 0)
        {
            for ($rih = 1; $rih <= $list[0]['riders_in_heat_'.$i]; $rih++)
            {
                $assignheats = $db->prepare("UPDATE tbl_event_entries SET heat_nbr = :heat_nbr, heat_position = :heat_position, race_nbr = :race_nbr WHERE entry_id = :entry_id");
                $assignheats->bindValue(':heat_nbr', $i);
                $assignheats->bindValue(':heat_position', $rih);
                $assignheats->bindValue(':race_nbr', $race);
                $assignheats->bindValue(':entry_id', getEntries($class, $list[0]['riders_in_heat_'.$i], $offset));
                $assignheats->execute();
                $offset++;
            }
            $race++;
        }
    }
}

echo json_encode(array('status' => true));

这篇关于使用查询和查询表循环。 MySQL的&安培; PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆