简体   繁体   English

多个选择语句

[英]Multiple Select Statements

I am trying to create a list of items in which every fourth item in the list is an item that is on from New York and On Sale. 我正在尝试创建一个项目列表,其中列表中的每四个项目都是纽约和On Sale上的项目。 The rest of the items in the list are the items that are from New York no matter if they are on sale or not. 列表中的其余项目都是来自纽约的项目,无论是否出售。

How can I properly use two select statements to break the list up into the order that I want? 如何正确使用两个select语句将列表分成所需的顺序? Below is what I'm using now. 以下是我现在使用的内容。 It creates a list of paragraphs but does not understand what the variables $article and $article2 are. 它创建了一个段落列表,但不了解变量$article$article2是什么。

<ul>

<?
    $sort = id;
    $sql = "SELECT * FROM `Catalog` WHERE state = 'New York' and in_stock = 'yes' ORDER BY $sort";
    $result = mysql_query($sql);

    $sql2 = "SELECT * FROM `Catalog` WHERE state = 'New York' and sale_item = 'yes' ORDER BY $sort ";
    $result2 = mysql_query($sql2);

    $i = 0;

    while ( $article = mysql_fetch_array($result) || $article2 = mysql_fetch_array($result2) ) { 

        if ($i == 0) {
            echo '<li>This item number is on sale:'.article2[id]. '</li>';
                $i++;       
        } else if ($i == 3) {
                echo '<li>This item number is regular price'.article[id]. '</li>';
                $i = 0;
        } else {
            echo '<li>This item number is regular price'.article[id].'</li>';
            $i++;
        }
    }
    ?>  

</ul>

Based on Dave's suggestion I'm trying it this way. 根据Dave的建议,我正在尝试这种方式。 But php still doesn't know how to interpret $itemsOnSale or $items. 但是php仍然不知道如何解释$ itemsOnSale或$ items。

<ul>
<?   
$items = array();
$itemsOnSale = array();
$sql = "SELECT * FROM `Catalog` WHERE state = 'New York' AND in_stock = 'yes' ORDER BY $sort";
$result = mysql_query($sql);
while ( $row = mysql_fetch_assoc($result) ) {

    if ( $row['sale_item'] == 'yes' ) {
        $itemsOnSale[] = $row;  
    } else {
        $items[] = $row;
    }

        if ($i == 0) {
               echo '<li>This item number is on sale:'.$itemsOnSale[id]. '</li>';
               $i++;       
        } else if ($i == 3) {
             echo '<li>This item number is regular price'.$items[id]. '</li>';
             $i = 0;
        } else {
             echo '<li>This item number is regular price'.$items[id].'</li>';
             $i++;
        }

}

  ?>
</ul>

A better method would be to pull all items in a single query, store them in separate arrays, then reference them as necessary. 更好的方法是在单个查询中提取所有项目,将它们存储在单独的数组中,然后根据需要引用它们。 In its current form your second query pulls sale items but doesn't check to see if they are in stock. 在当前形式下,您的第二个查询会拉出销售商品,但不检查是否有库存。

$items = array();
$itemsOnSale = array();
$sql = "SELECT * FROM `Catalog` WHERE state = 'New York' AND in_stock = 'yes' ORDER BY $sort";
$result = mysql_query($sql);
while ( $row = mysql_fetch_assoc($result) ) {
    if ( $row['sale_item'] == 'yes' ) {
        $itemsOnSale[] = $row;  
    } else {
        $items[] = $row;
    }
}

Then construct your HTML list, pulling an array item from $itemsOnSale every 4th loop. 然后构造您的HTML列表,每第4个循环从$ itemsOnSale中提取一个数组项。

It looks like you omitted the $ in front of your variable names: 好像您在变量名前面省略了$:

echo '<li>This item number is regular price'.article[id].'</li>'; should be echo '<li>This item number is regular price'.$article[id].'</li>'; 应该为echo '<li>This item number is regular price'.$article[id].'</li>';

As an aside, it looks like you're trying to use what's called MARS -- Multiple Active Result Sets. 顺便说一句,您似乎正在尝试使用所谓的MARS-多个活动结果集。 Not all database drivers support this. 并非所有的数据库驱动程序都支持此功能。

This is one of your problems: 这是您的问题之一:

while ( $article = mysql_fetch_array($result) || $article2 = mysql_fetch_array($result2) ) { 

If a result is found from $result , the condition will be met and $article2 never gets populated. 如果从$result找到$result ,则将满足条件,并且不会填充$article2 You should re-think the flow of your while loop. 您应该重新考虑while循环的流程。

You can try something like 您可以尝试类似

while ( $article = mysql_fetch_array($result) && $article2 = mysql_fetch_array($result2) ) {

but this will finish once either $article1 or $article2 is empty. 但这将在$article1$article2为空时完成。 However you can add another while loop afterwards with just 但是您可以while之后添加另一个while循环,只需

while( $article = mysql_fetch_array($result) ) { // keep posting regular items }

but there's a chance you may have more sale items than regular items (and everything wouldn't get posted.) There's lots of ways to do this. 但是您可能有比常规商品更多的销售商品(并且所有商品都不会发布。)有很多方法可以做到这一点。

Also, you have to reference variables using $ . 另外,您必须使用$引用变量。

echo '<li>This item number is on sale:'.$article2['id']. '</li>';

You also had an extra closing bracket in your original code, check your parsing. 您的原始代码中还有一个额外的结束括号,请检查您的解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM