2014-05-16 30 views
0

我在我的数据库中有逗号分隔列。该列称为“访问”,它处理数字如1,2,3,4,5,6,7。我需要比较会话的值的要求称为kt_department,其中一个逗号分隔值,例如kt_department的值为4,然后其余数据将显示在页面中。但是,如果不是,它将继续到下一行,但如果再次找不到,它应该继续到下一行,直到它发现逗号分隔的值。但是如果他们什么都没有发现,将会出现如“无可用记录”的消息。而且找到的记录应该是数字。如何查找session var是否在逗号分隔的列中?

我试图让我自己;

<?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); ?> 

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

+0

正常化您的数据库并摆脱逗号分隔第一http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad –

+0

你可以尝试'$ news_query =“SELECT * FROM备忘录WHERE(访问IN'%{$ accessrights}%'AND status ='1'AND idcategory ='1')”;' – dcclassics

回答

0

为了提供一个实际的回答你的问题(随意注意夏尔马):

获取用户名和密码的问题你摆脱你的数据库。即使它是本地主机,它会伤害我的眼睛。

回答你的问题。我强烈建议你用accesskt_dept这两列创建第二个表 - 或者你想要给他们打电话。这样,你可以连接访问整数与kt_dept - 意味着一个kt_dept有零到许多access

有了这imlementation你只需要运行一个查询像 SELECT kt_dept FROM newtable的:如果你得到任何行回或不访问=“{$访问}” 和检查。