duanju8431 2014-04-03 15:48
浏览 28
已采纳

PHP SELECT循环

I have a School database with two courses, each course has 12 possible subject fields (co_subj...) that can contain the subject ID or NULL, in this case course ID 1 has 3 subjects, and course 2 has only 1:

Courses

I need PHP to create a <div> for each subject found on a course, and don't create nothing in any NULL case. Querys:

$select = mysql_query("SELECT * FROM course_conf JOIN course_type ON ct_id=co_fk_ct_id ORDER BY co_name");

And then a while makes PHP check every course's field:

while($registroBbdd = mysql_fetch_array($select))
                {
                    $class="";
                    $courseId=$registroBbdd['co_id'];
                    $courseName=$registroBbdd['co_name'];
                    $courseType=$registroBbdd['ct_name'];

The doubt comes right now, trying to solve the most efficient way the <div> creating I mentioned before. The only way I find to solve this is creating a conditional "IF" structure printing the if a value is found, and nothing if NULL is found, like this:

if($registroBbdd['co_subj1'] != NULL){
     echo "<div>'.$registroBbdd['co_subj1'].'</div>}
else if ($registroBbdd['co_subj2'] != NULL){
     echo "<div>'.$registroBbdd['co_subj2'].'</div>}
.............}

Is there any looping way to make this? In order to avoid the whole "if" structure creation.

  • 写回答

2条回答 默认 最新

  • douzhan8395 2014-04-03 15:54
    关注

    You could have the SQL statement do the work. For example:

    SELECT *, (CASE WHEN co_subj1 IS NOT NULL THEN co_subj1 ELSE co_subj2 END) AS subject 
    FROM course_conf JOIN course_type ON ct_id=co_fk_ct_id ORDER BY co_name
    WHERE co_subj1 IS NOT NULL OR co_subj2 IS NOT NULL
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭