Explode and Set if condition php










0














Array ( [0] => 1 [1] => 2 )


I'm trying to set an if condition based on user permission, the idea is to show menu based on given values, if it is 1, one menu will be displayed and 2 second menu will be displayed, if its both then all values will be displayed. So far for single values i have gotten it right but how to do it for array of values
This is my code



<?php
$userid = $this->phpsession->get("user_id");
$userrole = $this->phpsession->get("user_type");

$query = $this->db->select("role_empid,role_permissions")->from('hw_role')->where('role_empid', $userid)->get()->result();
$data = $query[0]->role_permissions;

if($data == 1)
?>
<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php else if($data == 2) ?>
<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php ?>
</ul>









share|improve this question























  • Look at in_array - php.net/manual/en/function.in-array.php
    – fubar
    Nov 12 at 6:07










  • ok @fubar can you share an example
    – Amal Joseph
    Nov 12 at 6:09










  • what is the value in "role_permissions" if it contain multiple value array or json?
    – Poonam Navapara
    Nov 12 at 6:11










  • Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
    – Amal Joseph
    Nov 12 at 6:15










  • What is the issue with your current condition?
    – Sachin
    Nov 12 at 6:27















0














Array ( [0] => 1 [1] => 2 )


I'm trying to set an if condition based on user permission, the idea is to show menu based on given values, if it is 1, one menu will be displayed and 2 second menu will be displayed, if its both then all values will be displayed. So far for single values i have gotten it right but how to do it for array of values
This is my code



<?php
$userid = $this->phpsession->get("user_id");
$userrole = $this->phpsession->get("user_type");

$query = $this->db->select("role_empid,role_permissions")->from('hw_role')->where('role_empid', $userid)->get()->result();
$data = $query[0]->role_permissions;

if($data == 1)
?>
<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php else if($data == 2) ?>
<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php ?>
</ul>









share|improve this question























  • Look at in_array - php.net/manual/en/function.in-array.php
    – fubar
    Nov 12 at 6:07










  • ok @fubar can you share an example
    – Amal Joseph
    Nov 12 at 6:09










  • what is the value in "role_permissions" if it contain multiple value array or json?
    – Poonam Navapara
    Nov 12 at 6:11










  • Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
    – Amal Joseph
    Nov 12 at 6:15










  • What is the issue with your current condition?
    – Sachin
    Nov 12 at 6:27













0












0








0







Array ( [0] => 1 [1] => 2 )


I'm trying to set an if condition based on user permission, the idea is to show menu based on given values, if it is 1, one menu will be displayed and 2 second menu will be displayed, if its both then all values will be displayed. So far for single values i have gotten it right but how to do it for array of values
This is my code



<?php
$userid = $this->phpsession->get("user_id");
$userrole = $this->phpsession->get("user_type");

$query = $this->db->select("role_empid,role_permissions")->from('hw_role')->where('role_empid', $userid)->get()->result();
$data = $query[0]->role_permissions;

if($data == 1)
?>
<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php else if($data == 2) ?>
<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php ?>
</ul>









share|improve this question















Array ( [0] => 1 [1] => 2 )


I'm trying to set an if condition based on user permission, the idea is to show menu based on given values, if it is 1, one menu will be displayed and 2 second menu will be displayed, if its both then all values will be displayed. So far for single values i have gotten it right but how to do it for array of values
This is my code



<?php
$userid = $this->phpsession->get("user_id");
$userrole = $this->phpsession->get("user_type");

$query = $this->db->select("role_empid,role_permissions")->from('hw_role')->where('role_empid', $userid)->get()->result();
$data = $query[0]->role_permissions;

if($data == 1)
?>
<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php else if($data == 2) ?>
<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php ?>
</ul>






php codeigniter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:21









ariefbayu

17k85581




17k85581










asked Nov 12 at 6:06









Amal Joseph

115




115











  • Look at in_array - php.net/manual/en/function.in-array.php
    – fubar
    Nov 12 at 6:07










  • ok @fubar can you share an example
    – Amal Joseph
    Nov 12 at 6:09










  • what is the value in "role_permissions" if it contain multiple value array or json?
    – Poonam Navapara
    Nov 12 at 6:11










  • Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
    – Amal Joseph
    Nov 12 at 6:15










  • What is the issue with your current condition?
    – Sachin
    Nov 12 at 6:27
















  • Look at in_array - php.net/manual/en/function.in-array.php
    – fubar
    Nov 12 at 6:07










  • ok @fubar can you share an example
    – Amal Joseph
    Nov 12 at 6:09










  • what is the value in "role_permissions" if it contain multiple value array or json?
    – Poonam Navapara
    Nov 12 at 6:11










  • Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
    – Amal Joseph
    Nov 12 at 6:15










  • What is the issue with your current condition?
    – Sachin
    Nov 12 at 6:27















Look at in_array - php.net/manual/en/function.in-array.php
– fubar
Nov 12 at 6:07




Look at in_array - php.net/manual/en/function.in-array.php
– fubar
Nov 12 at 6:07












ok @fubar can you share an example
– Amal Joseph
Nov 12 at 6:09




ok @fubar can you share an example
– Amal Joseph
Nov 12 at 6:09












what is the value in "role_permissions" if it contain multiple value array or json?
– Poonam Navapara
Nov 12 at 6:11




what is the value in "role_permissions" if it contain multiple value array or json?
– Poonam Navapara
Nov 12 at 6:11












Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
– Amal Joseph
Nov 12 at 6:15




Array ( [0] => 1 [1] => 2 ) value of role permission @PoonamNavapara
– Amal Joseph
Nov 12 at 6:15












What is the issue with your current condition?
– Sachin
Nov 12 at 6:27




What is the issue with your current condition?
– Sachin
Nov 12 at 6:27












3 Answers
3






active

oldest

votes


















0














if role_permission contain json object that first convert it into the array like



$data = $query[0]->role_permissions;
$data=json_decode($data);


otherwise direct check the condition for



$data = $query[0]->role_permissions;

if(in_array(1,$data) && in_array(2,$data))


else if(in_array(1,$data))


else if(in_array(2,$data))







share|improve this answer




















  • ok let me check if it works ,thanks for your time
    – Amal Joseph
    Nov 12 at 6:18










  • A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
    – Amal Joseph
    Nov 12 at 6:21










  • Yes, $data must be contain array value
    – Poonam Navapara
    Nov 12 at 6:23










  • what should i do then?
    – Amal Joseph
    Nov 12 at 6:26










  • i exploded the value now it works
    – Amal Joseph
    Nov 12 at 6:27


















0














If $query[0]->role_permissions is an array that has distinct value, then you can try :



 <ul>
<?php for($i = 0; $i<count($data);$i++)
if($data[$i] == 1) ?>

<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

<?php
if($data[$i] == 2) ?>

<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

<?php ?>
</ul>





share|improve this answer






















  • when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
    – Amal Joseph
    Nov 12 at 6:25










  • updated my answer @AmalJoseph, just remove else
    – arisalsaila
    Nov 12 at 6:28










  • ok let me check @arisalsaila thanks
    – Amal Joseph
    Nov 12 at 6:36


















0














Try This



<?php
if(in_array(1,$data))
?>
<li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
<li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
<?php
if(in_array(2,$data))
?>
<li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
<?php ?>





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256653%2fexplode-and-set-if-condition-php%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    if role_permission contain json object that first convert it into the array like



    $data = $query[0]->role_permissions;
    $data=json_decode($data);


    otherwise direct check the condition for



    $data = $query[0]->role_permissions;

    if(in_array(1,$data) && in_array(2,$data))


    else if(in_array(1,$data))


    else if(in_array(2,$data))







    share|improve this answer




















    • ok let me check if it works ,thanks for your time
      – Amal Joseph
      Nov 12 at 6:18










    • A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
      – Amal Joseph
      Nov 12 at 6:21










    • Yes, $data must be contain array value
      – Poonam Navapara
      Nov 12 at 6:23










    • what should i do then?
      – Amal Joseph
      Nov 12 at 6:26










    • i exploded the value now it works
      – Amal Joseph
      Nov 12 at 6:27















    0














    if role_permission contain json object that first convert it into the array like



    $data = $query[0]->role_permissions;
    $data=json_decode($data);


    otherwise direct check the condition for



    $data = $query[0]->role_permissions;

    if(in_array(1,$data) && in_array(2,$data))


    else if(in_array(1,$data))


    else if(in_array(2,$data))







    share|improve this answer




















    • ok let me check if it works ,thanks for your time
      – Amal Joseph
      Nov 12 at 6:18










    • A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
      – Amal Joseph
      Nov 12 at 6:21










    • Yes, $data must be contain array value
      – Poonam Navapara
      Nov 12 at 6:23










    • what should i do then?
      – Amal Joseph
      Nov 12 at 6:26










    • i exploded the value now it works
      – Amal Joseph
      Nov 12 at 6:27













    0












    0








    0






    if role_permission contain json object that first convert it into the array like



    $data = $query[0]->role_permissions;
    $data=json_decode($data);


    otherwise direct check the condition for



    $data = $query[0]->role_permissions;

    if(in_array(1,$data) && in_array(2,$data))


    else if(in_array(1,$data))


    else if(in_array(2,$data))







    share|improve this answer












    if role_permission contain json object that first convert it into the array like



    $data = $query[0]->role_permissions;
    $data=json_decode($data);


    otherwise direct check the condition for



    $data = $query[0]->role_permissions;

    if(in_array(1,$data) && in_array(2,$data))


    else if(in_array(1,$data))


    else if(in_array(2,$data))








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 6:17









    Poonam Navapara

    18618




    18618











    • ok let me check if it works ,thanks for your time
      – Amal Joseph
      Nov 12 at 6:18










    • A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
      – Amal Joseph
      Nov 12 at 6:21










    • Yes, $data must be contain array value
      – Poonam Navapara
      Nov 12 at 6:23










    • what should i do then?
      – Amal Joseph
      Nov 12 at 6:26










    • i exploded the value now it works
      – Amal Joseph
      Nov 12 at 6:27
















    • ok let me check if it works ,thanks for your time
      – Amal Joseph
      Nov 12 at 6:18










    • A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
      – Amal Joseph
      Nov 12 at 6:21










    • Yes, $data must be contain array value
      – Poonam Navapara
      Nov 12 at 6:23










    • what should i do then?
      – Amal Joseph
      Nov 12 at 6:26










    • i exploded the value now it works
      – Amal Joseph
      Nov 12 at 6:27















    ok let me check if it works ,thanks for your time
    – Amal Joseph
    Nov 12 at 6:18




    ok let me check if it works ,thanks for your time
    – Amal Joseph
    Nov 12 at 6:18












    A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
    – Amal Joseph
    Nov 12 at 6:21




    A PHP Error was encountered Severity: Warning Message: in_array() expects parameter 2 to be array, string given
    – Amal Joseph
    Nov 12 at 6:21












    Yes, $data must be contain array value
    – Poonam Navapara
    Nov 12 at 6:23




    Yes, $data must be contain array value
    – Poonam Navapara
    Nov 12 at 6:23












    what should i do then?
    – Amal Joseph
    Nov 12 at 6:26




    what should i do then?
    – Amal Joseph
    Nov 12 at 6:26












    i exploded the value now it works
    – Amal Joseph
    Nov 12 at 6:27




    i exploded the value now it works
    – Amal Joseph
    Nov 12 at 6:27













    0














    If $query[0]->role_permissions is an array that has distinct value, then you can try :



     <ul>
    <?php for($i = 0; $i<count($data);$i++)
    if($data[$i] == 1) ?>

    <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
    <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

    <?php
    if($data[$i] == 2) ?>

    <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

    <?php ?>
    </ul>





    share|improve this answer






















    • when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
      – Amal Joseph
      Nov 12 at 6:25










    • updated my answer @AmalJoseph, just remove else
      – arisalsaila
      Nov 12 at 6:28










    • ok let me check @arisalsaila thanks
      – Amal Joseph
      Nov 12 at 6:36















    0














    If $query[0]->role_permissions is an array that has distinct value, then you can try :



     <ul>
    <?php for($i = 0; $i<count($data);$i++)
    if($data[$i] == 1) ?>

    <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
    <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

    <?php
    if($data[$i] == 2) ?>

    <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

    <?php ?>
    </ul>





    share|improve this answer






















    • when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
      – Amal Joseph
      Nov 12 at 6:25










    • updated my answer @AmalJoseph, just remove else
      – arisalsaila
      Nov 12 at 6:28










    • ok let me check @arisalsaila thanks
      – Amal Joseph
      Nov 12 at 6:36













    0












    0








    0






    If $query[0]->role_permissions is an array that has distinct value, then you can try :



     <ul>
    <?php for($i = 0; $i<count($data);$i++)
    if($data[$i] == 1) ?>

    <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
    <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

    <?php
    if($data[$i] == 2) ?>

    <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

    <?php ?>
    </ul>





    share|improve this answer














    If $query[0]->role_permissions is an array that has distinct value, then you can try :



     <ul>
    <?php for($i = 0; $i<count($data);$i++)
    if($data[$i] == 1) ?>

    <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
    <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>

    <?php
    if($data[$i] == 2) ?>

    <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>

    <?php ?>
    </ul>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 6:27

























    answered Nov 12 at 6:21









    arisalsaila

    37416




    37416











    • when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
      – Amal Joseph
      Nov 12 at 6:25










    • updated my answer @AmalJoseph, just remove else
      – arisalsaila
      Nov 12 at 6:28










    • ok let me check @arisalsaila thanks
      – Amal Joseph
      Nov 12 at 6:36
















    • when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
      – Amal Joseph
      Nov 12 at 6:25










    • updated my answer @AmalJoseph, just remove else
      – arisalsaila
      Nov 12 at 6:28










    • ok let me check @arisalsaila thanks
      – Amal Joseph
      Nov 12 at 6:36















    when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
    – Amal Joseph
    Nov 12 at 6:25




    when it have array value of Array ( [0] => 1 [1] => 2 ) only enquiry list is shown, i need both menu to be shown when it contains 1 and 2
    – Amal Joseph
    Nov 12 at 6:25












    updated my answer @AmalJoseph, just remove else
    – arisalsaila
    Nov 12 at 6:28




    updated my answer @AmalJoseph, just remove else
    – arisalsaila
    Nov 12 at 6:28












    ok let me check @arisalsaila thanks
    – Amal Joseph
    Nov 12 at 6:36




    ok let me check @arisalsaila thanks
    – Amal Joseph
    Nov 12 at 6:36











    0














    Try This



    <?php
    if(in_array(1,$data))
    ?>
    <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
    <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
    <?php
    if(in_array(2,$data))
    ?>
    <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
    <?php ?>





    share|improve this answer

























      0














      Try This



      <?php
      if(in_array(1,$data))
      ?>
      <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
      <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
      <?php
      if(in_array(2,$data))
      ?>
      <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
      <?php ?>





      share|improve this answer























        0












        0








        0






        Try This



        <?php
        if(in_array(1,$data))
        ?>
        <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
        <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
        <?php
        if(in_array(2,$data))
        ?>
        <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
        <?php ?>





        share|improve this answer












        Try This



        <?php
        if(in_array(1,$data))
        ?>
        <li><a href="<?php echo base_url("enquiry/addEnquiry"); ?>">Add Enquiry</a></li>
        <li><a href="<?php echo base_url("enquiry"); ?>">Enquiry List</a></li>
        <?php
        if(in_array(2,$data))
        ?>
        <li><a href="<?php echo base_url("enquiry/proposalList"); ?>">Request For Proposal</a></li>
        <?php ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 6:44









        Sachin

        620411




        620411



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256653%2fexplode-and-set-if-condition-php%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto