如何定位session var是否在逗号分隔列中?

时间:2014-05-16 14:42:13

标签: php html mysql

我的数据库中有逗号分隔列。该栏名为“访问权限”。它处理数字,如1,2,3,4,5,6,7。我需要比较会话值的要求称为kt_department,其中一个逗号分隔值,例如kt_department的值为4,然后其余数据将显示在页面中。但如果没有,它将进入下一行,但如果没有找到它,它应该继续到下一行,直到它发现它的值以逗号分隔。但如果他们什么都没找到,就会出现"没有可用记录" 这样的消息。并且找到的记录应该是重要的。

我试图怀疑自己;

<?php 
            $news_connect = mysqli_connect("localhost","root","pwd", "dbase");
            if (mysqli_connect_errno())
                {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
            $kt_dept  = $_SESSION['kt_department'] . " ";
            $accessrights = explode(',', $row_news['accessrights']);
            $news_query = "SELECT * FROM memo
                           WHERE (access LIKE '%{$accessrights}%' AND status='1' AND idcategory='1')";
            $news_result = mysqli_query($news_connect, $news_query);
            $row_news = mysqli_fetch_array($news_result);
            echo $kt_dept; 
            ?>
            <div style="margin-top:30px;">
            <table width="98%"  border="0" cellspacing="5" cellpadding="0" class="curveborderboxnobg">
                  <tr>
                    <td height="70" colspan="2" class="brandtitle" style="padding-left:20px;line-height:60px; font-size:18px;"><img src="images/icons/news-announcement.png" width="20" height="20" style="margin-right:10px;"><strong>News and Announcement </strong></td>
                  </tr>
                  <tr>
                    <td height="40" colspan="2" style=" padding:0px 20px 0 20px;">
                    <table width="95%"  border="0" style="margin-left:45px;" class="bortopbot" cellpadding="0" cellspacing="5">
                      <tr>
                        <td height="30" class="tabledetails" style="padding-top:20px;" ><strong><a href="news_bulletin.php?idmemonewsbulletin=<?php echo $row_news['idmemo']; ?>" class="lightbox" data-width="800" data-height="500"><?php echo $row_news['title']; ?></a></strong></td>
                      </tr>
                      <tr>
                        <td class="tabledetails" style="padding-bottom:20px;"><span style="font-size:12px;"><?php echo $row_news['shortdesc']; ?></span></td>
                      </tr>
                    </table>
                    </td>
                  </tr>
                  <tr>
                    <td height="40" class="tabledetails" style="padding-left:10px;">&nbsp; </td>
                    <td class="tabledetails" style="padding-right:20px; ">
                    <table width="263" border="0" align="right" class="tabledetails">
                      <tr>
                        <td align="center"><div align="right"><a href="news_bulletin_list.php?newsbul=<?php echo $row_news['idcategory']; ?>"><strong>View All </strong></a></div></td>
                      </tr>
                    </table>
                    </td>
                  </tr>
              </table>            
            </div>
                <table width="98%"  border="0" cellspacing="5" style="margin-top:30px; " cellpadding="0" class="curveborderboxnobg">
                  <tr>
                    <td height="70" class="brandtitle" style="padding-left:20px;line-height:60px; font-size:18px;"><img src="images/icons/news-announcement.png" width="20" height="20" style="margin-right:10px;"><strong>News and Announcement </strong></td>
                  </tr>
                  <tr>
                    <td height="40" class="bortopbot" style=" padding:0px 20px 0 20px;">
                      <div align="center">- No News and Announcement - </div></td>
                  </tr>
              </table>
              <?php mysqli_close($news_connect); ?>

谢谢你们。请帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

为您的问题提供实际答案(随意注意Sharma):

从问题中删除数据库的用户名和密码。即使它是本地主机,它也会伤到我的眼睛。

回答你的问题。我强烈建议您创建第二个包含accesskt_dept列的表 - 或者您想要调用它们的内容。这样,您就可以使用kt_dept连接访问整数 - 这意味着一个kt_dept具有零到多access

通过这种方式,你只需运行一个类似的查询     SELECT kt_dept FROM newTable WHERE access ='{$ access}' 并检查是否有任何行返回。