n000bie Posted December 1, 2010 Share Posted December 1, 2010 Hi, Code <?php $a = '20,44,72,73'; $b = explode(',', $a); $total = count($b); $c = 20; $d = in_array($c, $b); if ($d) { if ($total==1) { echo 'There is only 1 record and will be redirected to (its) detail page.';} else { echo "There are more than 1 record";} } ?> What I am trying to do is, if there is only 1 value in $a, do something if there is more than 1 value in $a do something else $a comes from a table field (this is the category id and is saved seperated with comma) This code works if the value of $a = 20 (or any other single number) It does not work if $a has more values seperated with comma. Can anyone help me please. Link to comment https://forums.phpfreaks.com/topic/220323-array-help/ Share on other sites More sharing options...
OldWest Posted December 1, 2010 Share Posted December 1, 2010 Are you saying the value of $a will be coming from a table? You might want to consider using array('value','value','value') And you can test the array with empty() .. And if you are pulling all of the data from a table, you will probably want to use foreach() Link to comment https://forums.phpfreaks.com/topic/220323-array-help/#findComment-1141709 Share on other sites More sharing options...
n000bie Posted December 1, 2010 Author Share Posted December 1, 2010 Yes I have done that this is my actual code if(isset($_REQUEST['brand'])) { $sSqlQuery = 'where published=?'; $row = $tobj->find_all($sSqlQuery,array('Y')); if($tobj->_row_count>0) { foreach($row as $r) { $brand = explode(',',$r->brand_id); $myTotalBrand=count($brand); if(in_array($cat_id,$brand)) { //echo $myTotalBrand; if ($myTotalBrand==1) { echo "<div style='text-align:center; height:100px; margin-top:50px;'><img src='".SITE_URL."/images/loading10.gif' /><div>loading...</div></div>"; echo "<script>document.location.href='".SITE_URL."/shop/".$r->slug."'</script>"; } else { $count_check++; if(($count_check%6)==0) { echo '<li style="margin-right:0px;">'; } else echo '<li>'; echo "<a href='".SITE_URL."/shop/".$r->slug."'>";echo "<img src=\"$r->logo_img\">"; echo "</a>"; echo "<a href='".SITE_URL."/shop/".$r->slug."' class='fixht'>"; echo ucfirst($r->shop_title); echo "</a>"; echo '</li>'; } } // end in array check } } // end row count } Link to comment https://forums.phpfreaks.com/topic/220323-array-help/#findComment-1141736 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.