while或foreach循环ifs(While or foreach loops with ifs)

编程入门 行业动态 更新时间:2024-10-25 20:20:36
while或foreach循环ifs(While or foreach loops with ifs)

我想帮助你,因为我被困在这里:

SQL:

$testEvents = $db->prepare("SELECT * FROM events WHERE str_to_date(concat(`eventDate`, ' ', `time`), '%d-%m-%Y %H:%i')"); $testEvents ->execute();

PHP:

<?php while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) ) { if( ! is_numeric($test['eventName'] && is_numeric($test['teamA']) && is_numeric($test['teamB']))) { echo $test['eventName']; echo "<br>"; } } ?>

我的目标是尝试像这样做:

数据库映像

示例:如果eventName 不是 数字,并且teamA和teamB 不为NULL ,则显示eventName数据。

如果teamA和teamB 不是 数字 , eventName 数字,则显示teamA和teamB

图像 说明 说明

I would like your help because I'm stuck on here:

SQL:

$testEvents = $db->prepare("SELECT * FROM events WHERE str_to_date(concat(`eventDate`, ' ', `time`), '%d-%m-%Y %H:%i')"); $testEvents ->execute();

PHP:

<?php while ( $test = $testEvents->fetch(PDO::FETCH_ASSOC) ) { if( ! is_numeric($test['eventName'] && is_numeric($test['teamA']) && is_numeric($test['teamB']))) { echo $test['eventName']; echo "<br>"; } } ?>

My target is try to do like:

Database image

Example: If eventName IS NOT a number and teamA & teamB ARE NOT NULL , show eventName data.

AND

If teamA & teamB ARE NOT a number and eventName IS a number, show teamA and teamB

EXPLANATION IN IMAGE: Explanation

最满意答案

除了你奇怪的数据库设计......以回答你的问题,你可以试试这个...

我编写了一些测试数据来模拟你的数据库

$data = [ [ 'eventName'=>'1', 'teamA'=>'Magallanes', 'teamB'=>'Caracas', 'eventDate'=>'17-11-2016', 'eventTime'=>'11:11' ], [ 'eventName'=>'Manana', 'teamA'=>'123', 'teamB'=>'123', 'eventDate'=>'17-11-2016', 'eventTime'=>'12:12' ] ];

然后测试了代码来执行你的循环...你在正确的轨道但显然你只是放弃....

foreach($data as $test) { if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB'])) { echo $test['eventName']; echo " "; echo $test['eventDate']; echo " "; echo $test['eventTime']; echo "<br>"; } else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB'])) { echo $test['teamA']; echo " x "; echo $test['teamB']; echo " "; echo $test['eventDate']; echo " "; echo $test['eventTime']; echo "<br>"; } }

结果

Magallanes x Caracas 17-11-2016 11:11 Manana 17-11-2016 12:12

根据需要交换和更改内容。

Well apart from your strange database design... to answer your question you can try this...

I made up some test data to simulate your database

$data = [ [ 'eventName'=>'1', 'teamA'=>'Magallanes', 'teamB'=>'Caracas', 'eventDate'=>'17-11-2016', 'eventTime'=>'11:11' ], [ 'eventName'=>'Manana', 'teamA'=>'123', 'teamB'=>'123', 'eventDate'=>'17-11-2016', 'eventTime'=>'12:12' ] ];

Then tested out the code to perform your loop... You were on the right track but obviously you just gave up....

foreach($data as $test) { if( ! is_numeric($test['eventName']) && is_numeric($test['teamA']) && is_numeric($test['teamB'])) { echo $test['eventName']; echo " "; echo $test['eventDate']; echo " "; echo $test['eventTime']; echo "<br>"; } else if (is_numeric($test['eventName']) && ! is_numeric($test['teamA']) && ! is_numeric($test['teamB'])) { echo $test['teamA']; echo " x "; echo $test['teamB']; echo " "; echo $test['eventDate']; echo " "; echo $test['eventTime']; echo "<br>"; } }

The Results

Magallanes x Caracas 17-11-2016 11:11 Manana 17-11-2016 12:12

Swap and change things as you require.

更多推荐

本文发布于:2023-08-07 02:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:foreach   ifs   loops

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!