如果 ISSET 不起作用?

发布于 2024-11-19 08:31:12 字数 1332 浏览 4 评论 0原文

IN DB

row1 (type column=apple), (stage column=1)
row2 (type column=orange), (stage column=NULL)

IN PHP

$query = mysql_query("SELECT * from fruits");

$row = mysql_fetch_array($query);

    $num = mysql_num_rows($query);
    $i=0;
    $storeMyData = array();
    while($row = mysql_fetch_array($query))
    {
        if(isset($row['type'])){
            $type = "-" . $row['type'];
        } else {
            $type = NULL;
        }

        if(isset($row['stage'])){
            $stage = "STAGE " . $row['stage'];
        } else {
            $stage = NULL;
        }
        $fruit = implode (", ", array_filter(array($type, $stage)));

    // This will show 2 rows of data
    $storeMyData[] = $fruit;  

    // store current data in array
    $i++;
    }

    /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)

    /* or join the data using string concatenation */
    $allFinalData2 = "";

    /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)
    {
        $allFinalData2 = $allFinalData2.$prevData ;  // keep on concatenating
    }

    echo $allFinalData2;

最终输出显示为“Apple,Stage 1”“Orange,Stage”。

如果 stage 为 NULL,如何在没有 Stage 一词的情况下显示“Apple,Stage 1”“Orange” 数据库中的 row2(橙色)。

IN DB

row1 (type column=apple), (stage column=1)
row2 (type column=orange), (stage column=NULL)

IN PHP

$query = mysql_query("SELECT * from fruits");

$row = mysql_fetch_array($query);

    $num = mysql_num_rows($query);
    $i=0;
    $storeMyData = array();
    while($row = mysql_fetch_array($query))
    {
        if(isset($row['type'])){
            $type = "-" . $row['type'];
        } else {
            $type = NULL;
        }

        if(isset($row['stage'])){
            $stage = "STAGE " . $row['stage'];
        } else {
            $stage = NULL;
        }
        $fruit = implode (", ", array_filter(array($type, $stage)));

    // This will show 2 rows of data
    $storeMyData[] = $fruit;  

    // store current data in array
    $i++;
    }

    /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)

    /* or join the data using string concatenation */
    $allFinalData2 = "";

    /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)
    {
        $allFinalData2 = $allFinalData2.$prevData ;  // keep on concatenating
    }

    echo $allFinalData2;

The final output shows as "Apple, Stage 1" "Orange, Stage".

How can I show it "Apple, Stage 1" "Orange" without the word Stage if stage is NULL for
row2 (orange) in DB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-11-26 08:31:12

将此行也更改

while($i < $num)

为此

while($row = mysql_fetch_array($query))

,无需在循环内调用 mysql_query,使用它可以防止混淆。

$type = "-" . $row['type'];
$stage = "STAGE " . $row['stage'];

Change this line

while($i < $num)

to this

while($row = mysql_fetch_array($query))

also, there is no need to call mysql_query inside your loop, use this to prevent confusion.

$type = "-" . $row['type'];
$stage = "STAGE " . $row['stage'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文