如果 ISSET 不起作用?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此行也更改
为此
,无需在循环内调用 mysql_query,使用它可以防止混淆。
Change this line
to this
also, there is no need to call mysql_query inside your loop, use this to prevent confusion.