display different results based on array value

Napify App Source

I am trying to display a custom HTML page if the user that requests the page has the [is_vip] value equal to "5".

this is the line I'm trying to run:

 <?php if ($membership["is_vip"] == 5) { ?>
do stuff
    <?php } else { 
do other stuff

However it's not displaying the custom results properly, the contents of $membership look like this when printed.

    Array
(
    [0] => napify\Model\Membership Object
        (
            [is_vip] => 5
        )

)

This should be simple but i've been stuck in this for hours.

php

Answers

answered 1 week ago AbraCadaver #1

That's an integer indexed array with one element containing an object. Plus the code you posted won't parse and is a parse error. Once you fix the syntax:

<?php if ($membership[0]->is_vip == 5) { ?>
 do stuff
<?php } else { ?>
 do other stuff
<?php } ?>

Make sure to use error reporting when developing:

error_reporting(E_ALL);
ini_set('display_errors', '1');

answered 1 week ago user2852143 #2

I believe AbraCadaver is correct with their post

<?php if ($membership[0]->is_vip == 5) { ?>

but there is also an issue with your closing tag.

<?php } else {

should be

<?php } else { ?>

and your else statement needs to be closed with a

<?php } ?>

answered 1 week ago Charles Stover #3

Membership is an array, not an object, so you need to use $membership[0] to get the first item in the array. The item in the array is an object, not an associative array, so the syntax for its member variables is ->key not ['key']. You are missing your closing curly brace } after the last line and did not close the last PHP code block with ?> before outputting the last line.

<?php if ($membership[0]->is_vip == 5) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>

You likely should use a text editor that color-codes your code for you, such as Sublime or VS Code.

comments powered by Disqus