Jump to content

Recommended Posts

First, is there a GREAT online directory of exact syntax that is searchable

 

Here is what I am trying to do:

I have a menu that if the adminID is 3, 20 or 24 it needs to load an admin only menu option, trying to use this code but it isn' t working:

 

<?php
    if ($row->AdminID == '3, 24, 20') {
?>    
    <tr>
        <td><div class="admin_link"><a href="AddAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Add Admin</a></div></td>
    </tr>
    <tr>
        <td><div class="admin_link"><a href="ViewAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Admin</a></div></td>
    </tr>
<?php
    }
?>

 

and I have also tried this:

<?php
    if ($row->AddEditAdmin == "YES") {
?>    

 

Should I just put a new query in?

Link to comment
https://forums.phpfreaks.com/topic/139535-solved-help-with-syntax/
Share on other sites

Yeah, you can't do that. You must test each value explicitly:

 

if($var == 4 || $var == 20 || $var == 24){
    //do something
}

 

Alternatively, you can place the possible values in an array and use the in_array() function:

 

$values = array(4,20,24);
if(in_array($var,$values)){
    //do something
}

 

At present, your code tests to see whether or not the value of $row->AdminID is the string "3, 24, 20".

 

As for an online directory, there's always the manual.

Yes, it's the PHP Manual. If you're interested in a particular function, just put the name of the function at the end of the url, for example: http://www.php.net/substr

 

As to your question, the best way of doing this (IMHO) is to use the in_array function:

<?php
    if (in_array($row->AdminID,array(3,24,20)) {
?>

 

Ken

(beaten...)

Well interestingly and oddly  enough, those suggestions broke my code and now nothing shows up, and yes I have error_reporting turned on and no errors are being printed to the browser (or anywhere)

I get a blank page.

..any other clues I can use to get this figured out?

Your code was already broken with this

 

if ($row->AdminID == '3, 24, 20') {

 

Well interestingly and oddly  enough, those suggestions broke my code and now nothing shows up, and yes I have error_reporting turned on and no errors are being printed to the browser (or anywhere)

I get a blank page.

..any other clues I can use to get this figured out?

u could do this

 

<?php
$nums = array("3", "24", "20");
foreach($nums as $num) {
    if(ereg($num, $row->AdminID )) $isitin = 1;
    else $isitin = 0;
}

    if ($isitin) {
?>   
    <tr>
        <td><div class="admin_link"><a href="AddAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Add Admin</a></div></td>
    </tr>
    <tr>
        <td><div class="admin_link"><a href="ViewAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Admin</a></div></td>
    </tr>
<?php
    }
?>

Yes, you could do that. But why on earth would you? That's one of the most convoluted pieces of code i've seen in my life

 

Edit: Actually i lied. You couldn't do that. That would only check to see if $row->AdminID was 20, since your variable $isitin is overwritten every time.

Yes, you could do that. But why on earth would you? That's one of the most convoluted pieces of code i've seen in my life

 

Edit: Actually i lied. You couldn't do that. That would only check to see if $row->AdminID was 20, since your variable $isitin is overwritten every time.

 

quote true but its nearly there here u go

 

 

<?php
    $nums = array("3", "24", "20");
    $isitin = 0;
    foreach($nums as $num) if(ereg($num, $row->AdminID )) $isitin = 1;

    if ($isitin) {
?>   
    <tr>
        <td><div class="admin_link"><a href="AddAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">Add Admin</a></div></td>
    </tr>
    <tr>
        <td><div class="admin_link"><a href="ViewAdmin.php?AdminID=<?php echo $AdminID; ?>" target="mainFrame">View Admin</a></div></td>
    </tr>
<?php
    }
?>

convoluted ?, yes maybe but it works watchu sayin ?

 

because it's a MUCH less efficient way of doing in_array()

 

its much more extendibale, u can connect and stuff to db or somthing interesting if not stick with the simple if or || hein lol i thoght u were gona show me a one liner to make my code look crap i was worried there lol

ok...let me shorten your ereg code for you:

 

<?php
if(in_array($row->AdminID,array("3", "24", "20"))) {
?>   
    <tr>
        <td><div class="admin_link"><a href="AddAdmin.php?AdminID=<?php echo $row->AdminID; ?>" target="mainFrame">Add Admin</a></div></td>
    </tr>
    <tr>
        <td><div class="admin_link"><a href="ViewAdmin.php?AdminID=<?php echo $row->AdminID; ?>" target="mainFrame">View Admin</a></div></td>
    </tr>
<?php
    }
?>

 

p.s. - to the OP - you were missing "row->" before AdminID in your HTML block (see above)

Thanks everyone, I figured it out, my original try of using this:

<?php
    if ($row->AddEditAdmin == "YES") {
?>    

 

Did end up working, the problem was that my query had ended on the page (does that make sense) because when I moved that chunk of menu options up on the page, it worked, but at the bottom of the menu (after a chunk of code that uses a different query) this chunk didn't work, so I just went with the rearrange and now I can try and figure out why the one query didn't continue to work, can queries not be nested on a page?

nice

Thanks everyone, I figured it out, my original try of using this:

<?php
    if ($row->AddEditAdmin == "YES") {
?>    

 

Did end up working, the problem was that my query had ended on the page (does that make sense) because when I moved that chunk of menu options up on the page, it worked, but at the bottom of the menu (after a chunk of code that uses a different query) this chunk didn't work, so I just went with the rearrange and now I can try and figure out why the one query didn't continue to work, can queries not be nested on a page?

 

is this a third part software that u are modifying ?

just look at the methods of the $row-> class and break it down ur gona have to get intimate with it, how long have u been working with php professionaly ?

 

If $row is coming from the database it was probably retrieved with mysql_fetch_object not third party at all just a different way of pulling data out of the database.

just look at the methods of the $row-> class and break it down ur gona have to get intimate with it, how long have u been working with php professionaly ?

 

If $row is coming from the database it was probably retrieved with mysql_fetch_object not third party at all just a different way of pulling data out of the database.

 

im just sugesting this may be the freamwork Model component of the MVC in place so it would be good to know it and use it in ur own code like it was made to do rather than revert to basic php which would be a waste of the nice work done by the previouse programmer.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.