doucuo9126 2014-02-19 17:28
浏览 48
已采纳

当从多个表中提取时,PHP while循环和if语句问题

I have 2 tables with a unique ID. I have a table with one of the columns being a date field. I am attempting to filter the field by today's date (which it works) from all the rows with "todays" date I want to get the cell info for #key. Once I have that ID, I want to match it with #headkey. So $headkey == $key. Once I filter that, I want to see if any of the fields match = Delivery for the ItemID column. For some reason I have an infinite loop. I played around with the logic but can't seem to get it to work. Any ideas?

 $TransactionSql = "SELECT * FROM apcshead WHERE DateInvoiced > 0 ORDER BY DateInvoiced DESC";
 $ItemsSql = "SELECT * FROM apcsitem";

 $rs=odbc_exec($conn,$TransactionSql);
while($row = odbc_fetch_array($rs)) 
{   
    //Grabbing Transaction info     
    $DateInvoiced = odbc_result($rs,"DateInvoiced");
    $ApcsheadKey = odbc_result($rs,"Key");

    $DateInvoiced = new DateTime($DateInvoiced);
    $DateInvoiced_date = $DateInvoiced->format('m-d-Y');
    //$TimeStamp_time = $TimeStamp->format('h:i:s');

    if ($DateInvoiced_date == $today)
        {

            $ItemsRs=odbc_exec($conn,$ItemsSql);
            while($row = odbc_fetch_array($ItemsRs)) 
            {
            $HeadKey = odbc_result($ItemsRs,"HeadKey");
            $ItemID = odbc_result($ItemsRs,"ItemID");

            if ($ItemID == 'Delivery')
                {                       
                    echo 'Delivery';
                    echo '<br />';
                }
            }
        }
}

*UPDATE:*I modified the code again. Now what if does is it spits out 1 row with the date and then like 100 echo Delivery and then goes back and spits out another date and the same thing. Still not sure what is going on.

 $TransactionSql = "SELECT * FROM apcshead WHERE DateInvoiced > 0 ORDER BY DateInvoiced DESC";
 $ItemsSql = "SELECT * FROM apcsitem";

 $rs=odbc_exec($conn,$TransactionSql);
    while($row = odbc_fetch_array($rs)) 
{   
$DateInvoiced = odbc_result($rs,"DateInvoiced");
$DateInvoiced = new DateTime($DateInvoiced);
$DateInvoiced_date = $DateInvoiced->format('m-d-Y');

 echo $DateInvoiced_date;
 echo '<br />';

if ($DateInvoiced_date == $Today)
    {
    echo $DateInvoiced_date;
    echo '<br />';

        $ItemsRs=odbc_exec($conn,$ItemsSql);
        while($row = odbc_fetch_array($ItemsRs)) 
        {
        $ItemID = odbc_result($ItemsRs,"ItemID");

            if ($ItemID == 'Delivery')
                {
                echo 'Delivery';                
                }  
        }   
    }
}   

展开全部

  • 写回答

1条回答 默认 最新

  • doushi7761 2014-02-20 16:19
    关注

    I solved the issue using the INNER JOIN command. It is great because I don't have to do nested loops :) Learned how to do it from W3Schools. http://www.w3schools.com/sql/sql_join_inner.asp

    This is my current SQL Statement:

     $TransactionSql = "SELECT apcshead.Key, apcshead.DateInvoiced, apcshead.InvNum, apcsitem.Headkey, apcsitem.ItemID FROM apcshead INNER JOIN apcsitem ON apcshead.Key=apcsitem.Headkey";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部