duan2477
duan2477
2015-09-21 22:55

如果else语句显示为两个字符串:[关闭]

已采纳

Edit three - In which we find out I am really not very clever

False alarm people, I can't use php in my current setup. I will go beat my head against a wall for a while. Thanks for your help and suggestions anyway.


I am attempting to follow this tutorial in order to allow a "Mobile/Full" switch on my responsive website. However, the link being used to display the option is showing both at once, not an 'either or' situation like it's supposed to.

The tutorial is using a little bit of PHP, which I don't know yet, so I don't know exactly where the error is. It all looks fine to me, but this code:

<div id="toggleView">
<?php if ( $fixedwidth == '1' ): ?>
        <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=0">Switch to Mobile site</a>
<?php else: ?>
        <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=1">Switch to Full site</a>
<?php endif; ?>
    </div>

Should display only one of the strings, but instead it displays both links like this:

?fixedwidth=0">Switch to Mobile site ?fixedwidth=1">Switch to Full site

I'm thinking it's a syntax problem with the ["Script_Name"] segment but I don't know what the correct syntax should be.

EDIT

This is the PHP I've inserted on my page to detect the change in fixedwidth:

<?php
    $fixedwidth = '';
    // Check if the query string contains our key and assign its value to $fixedwidth
    if ( isset($_GET['fixedwidth']) && ($_GET['fixedwidth'] != "") ) {
        $fixedwidth = $_GET['fixedwidth'];
    }
    // Check if a cookie has already been set
    if ( isset($_COOKIE['fixedwidth']) ) {
        if ( $fixedwidth == '0' ) {
            // If the value is '0', delete the cookie
            setcookie('fixedwidth', '', time() - 60, '/');
        } else {
            // Value is not '0', so no need to get the cookie value
            $fixedwidth = '1';
        }
    } else if ( $fixedwidth == '1') {
        // The user wants fixed width, so set a cookie
        $expires = 60 * 60 * 24 * 60 + time(); 
        setcookie('fixedwidth', '1', $expires, '/');
    }
?>

So, I've basically copy-pasted this from the tutorial to try and get this functionality on my page but I don't know if it's actually correct.

Edit 2 - Electric Boogaloo

Here's the code from the page in question:

<%@ Master Language="c#" CodeFile="Standard.master.cs" Inherits="Standard_" %>
<%@ Register Src="/include/menu_left.ascx" TagName="menu_left" TagPrefix="uc4" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<?php
    $fixedwidth = '1';
    // Check if the query string contains our key and assign its value to $fixedwidth
    if ( isset($_GET['fixedwidth']) && ($_GET['fixedwidth'] != "") ) {
        $fixedwidth = $_GET['fixedwidth'];
    }
    // Check if a cookie has already been set
    if ( isset($_COOKIE['fixedwidth']) ) {
        if ( $fixedwidth == '0' ) {
            // If the value is '0', delete the cookie
            setcookie('fixedwidth', '', time() - 60, '/');
        } else {
            // Value is not '0', so no need to get the cookie value
            $fixedwidth = '1';
        }
    } else if ( $fixedwidth == '1') {
        // The user wants fixed width, so set a cookie
        $expires = 60 * 60 * 24 * 60 + time(); 
        setcookie('fixedwidth', '1', $expires, '/');
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Website Title</title>
    <asp:ContentPlaceHolder ID="MetaDescriptionHolder" runat="server" Visible="true">
    </asp:ContentPlaceHolder>
    <asp:ContentPlaceHolder ID="MetaKeywordsHolder" runat="server" Visible="true">
    </asp:ContentPlaceHolder>
    <meta name="author" content="website" />

<?php
// Only insert the meta[name="viewport"] element if responsive is not disabled
if ( $fixedwidth = '1' ): ?>
    <meta name="viewport" content="width=1400, initial-scale=1" />
<?php endif; ?>

    <link rel="shortcut icon" href="favicon.ico" />
    <link rel="stylesheet" type="text/css" href="fontawesome/css/font-awesome.min.css">
    <link href="favicon.ico" rel="shortcut icon" />
    <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
    <style type="text/css"><!-- @import url("../../../css/responsive.css?t=<%= DateTime.Now.Ticks %>"); --></style>
    <%--<style type="text/css"><!-- @import url("../../../css/main-cs.css?t=<%= DateTime.Now.Ticks %>"); --></style>--%>
    <script language="javascript" type="text/javascript" src="../../../js/jquery1.4.2.js"></script>
    <script language="javascript" type="text/javascript" src="../../../js/validation.js"></script>
    <script language="javascript" type="text/javascript" src="../../../js/functions.js"></script>
    <script language="javascript" type="text/javascript" src="/js/browser-selector.js"></script>

    <script src="/js/jquery.autocomplete.js" type="text/javascript"></script>
    <asp:ContentPlaceHolder ID="slideshowHolder" runat="server" Visible="true">
    </asp:ContentPlaceHolder>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txtSearch.ClientID %>").autocomplete('/SearchTextBox.ashx');
        });       
    </script>
    <script type="text/javascript">
        function displayLogin(obj) {
            if (obj.checked) {
                if (!confirm('If you tick this box then you will no longer need to log in when visiting website from this computer.

You can disable this feature at any time by logging out.

Don\'t use this option unless you are the only person who has access to this computer.

WARNING:
If you do decide to use this option you will be liable for the actions of anyone who uses your login to access website.'))
                { obj.checked = false; }
            }
        }

        var mouse_is_inside = false;

        $(document).ready(function () {


            $('#login_box').hover(function () {
                mouse_is_inside = true;
            }, function () {
                mouse_is_inside = true;
            });



        });

</script>
</head>
<body>


    <form id="Form1" runat="server">
    <div class="top-menu">
        <div class="top-menu-holder">
            <div class="top-menu-tabs">
                <div class="top-menu-tabs-shop-on">
                    <a href="/"><strong>Online Store</strong></a>
                </div>
                <div class="top-menu-tabs-residential">
                        <a href="http://www. <strong>IT Services</strong></a>
                    </div>
                <div class="top-menu-tabs-services">
                    <a href="link">Business <strong>IT Services</strong></a>
                </div>
            </div>
                <div class="top-menu-menu">
                    <ul>
                        <li class="line"><a href="/contact-us.aspx" alt="Contact Us" title="Contact Us">Contact</a></li>
                        <li class="line"><a href="/about-us.aspx" alt="About Us" title="About Us">About</a></li>
                        <li class="line"><a href="/feedback.aspx" alt="Feedback" title="Feedback">Feedback</a></li>
                        <li class="line"><a href="/testimonials.aspx" alt="Testimonials" title="Testimonials">
                        Testimonials</a></li>
                        <li class="line"><a href="/employment.aspx" alt="Employment" title="Employment">Employment</a></li>
                        <li class="line"><a href="/awards.aspx" alt="Awards" title="Awards">Awards</a></li>
                        <li class="line"><a href="/Gift-Vouchers/Gift-Vouchers/Index.aspx" alt="Gift Vouchers" title="Gift Vouchers">Vouchers</a></li>
                        <li class="line"><a href="/promotions.aspx" alt="Promotions" title="Promotions">Promotions</a></li>
                    </ul>
                </div>
        </div>
    </div>
    <div class="page-content">
        <div class="shop-header">
            <div class="shop-header-logo">
                <a href="/">
                    <img alt="Online Store" src="/images/shop-logo.png" /></a>
            </div>
            <div class="shop-header-phone">
                Freephone<br />

            </div>
            <div class="shop-header-ad" style='<%# "display:" + (c.CustomerID >0 || Convert.ToInt32(c.GetTotalsForCart()[0]) > 0  ? "none": "") %>'>
                <a href="https://shop.eset.co.nz/shop/?mode=tr&resellerid=IyUqXygK">
                    <img alt="eset free 30 day trial" src="/images/top-strip-ad/eset-trial.jpg" title="eset free 30 day trial" /></a>
            </div>
            <asp:Panel ID="pnlCart" runat="server">
                <div class="shop-header-cart">
                    <div class="shop-header-cart-label-cart">
                        Shopping Cart:</div>
                    <div class="shop-header-cart-label-link">
                        <a id="A12" class="white" href="~/cart.aspx" runat="server">Shopping Cart:</a></div>
                    <div class="shop-header-cart-label">
                        Items:</div>
                    <div class="shop-header-cart-details">
                        <asp:Label ID="lblProducts" runat="server"></asp:Label></div>
                    <div class="shop-header-cart-label">
                        Total:</div>
                    <div class="shop-header-cart-details">
                        $<asp:Label ID="lblCost" runat="server"></asp:Label></div>
                    <div class="shop-header-cart-button">
                        <div class="green-button">
                            <a id="A6" href="~/cart.aspx" runat="server">View Cart</a>
                        </div>
                    </div>
                </div>
            </asp:Panel>
            <asp:Panel ID="pnlAccount" runat="server" Visible="false">
                <div class="account-menu">
                    <ul>
                        <li><a id="A2" href="~/saved-carts.aspx" runat="server">Saved Carts</a></li>
                        <li id="lstAccountModule1" runat="server" visible="false" class="line"><a id="A3"
                            href="~/transactions.aspx" runat="server">Transactions</a></li>
                        <li id="lstAccountModule2" runat="server" visible="false" class="line"><a id="A4"
                            href="~/orders.aspx" runat="server">Orders</a></li>
                        <li id="lstServiceModule1" runat="server" visible="false" class="line"><a id="A5"
                            href="~/service-tickets.aspx" runat="server">Service Tickets</a></li>
                        <li id="lstManageModule" runat="server" visible="false" class="line"><a id="A7"
                            href="~/manage.aspx" runat="server">Manage</a></li>
                    </ul>
                </div>
            </asp:Panel>
        </div>

        <div class="shop-menu-login-bar">
                <asp:Panel ID="pnlLoggedOut" runat="server" autocomplete="off">
                    <ul>
                        <li>
                            <a href="/log-in.aspx" alt="Log In" title="Log In">Log In</a>                   
                        </li>
                        <li class="line">
                            <a href="/create-account.aspx" alt="Register" title="Register">Register</a>
                        </li>
                    </ul>

                </asp:Panel>
                <asp:Panel ID="pnlLoggedIn" runat="server" autocomplete="off">
                    <ul>
                        <li>
                            <a href="~/log-in.aspx?logout=1" id="A8" runat="server">Logout</a>                  
                        </li>
                        <li class="line">
                            <a id="A9" href="~/update-details.aspx" runat="server">My Details</a>
                        </li>
                    </ul>
                </asp:Panel>
                <asp:Panel ID="pnlsearch3" runat="server" DefaultButton="btnsearch3">
                    <div class="search-3">
                        <asp:TextBox ID="txtSearch3" runat="server" Class="search-textbox-3" onblur="if(this.value == '') { this.value='Enter keyword or product code'; isSet=true; }"
                            onmouseover="if(this.value == 'Enter keyword or product code') { this.value='';isSet = true; }"
                            onmouseout="if(this.value == '' && !isSet) { this.value='Enter keyword or product code'; isSet=>false; }"
                            MaxLength="255" Text="Enter keyword or product code" ontextchanged="btnsearch3_Click" />
                        <asp:ImageButton ID="btnsearch3"  Class="search-icon-3" ImageAlign="Right" BackColor="White"  runat="server" Width="20" Height="20" OnClick="btnsearch3_Click" ImageUrl="~/images/search-icon.gif" />
                    </div>
                </asp:Panel>
            </div>

        <div class="shop-menu">
            <div class="shop-menu-float-left">
                <ul>
                <li><asp:HyperLink runat="server" ID="hlComputers" NavigateUrl="/Computers/Index.aspx" Text="Computers" ToolTip="View Products in Computers Category"/></li>
                    <li class="line"><asp:HyperLink ID="hlElectronics" NavigateUrl="/Electronics/Index.aspx" runat="server" tooltip="View Products in Electronics Category" Text="Electronics"/></li>
                    <li class="line"><asp:HyperLink ID="hlExLease" NavigateUrl="/Computers/Ex-Lease/index.aspx" runat="server"  tooltip="View our Ex-Lease Items" Text="Ex-Lease"/></li>
                    <li class="line"><asp:HyperLink ID="hlClearance" NavigateUrl="/search.aspx?S=Clearance+Item&picture=0" runat="server" tooltip="View our Clearence Items" Text="Clearance"/></li>
                    <li class="line"><asp:HyperLink ID="hlRepairs" NavigateUrl="/Christchurch-Computer-Repairs-and-Service.aspx" runat="server" tooltip="View our Store Service Department" Text="Repairs" /></li>
                </ul>
            </div>
            <div class="shop-menu-float-right">
                <asp:Panel ID="pnlLogin" runat="server" autocomplete="off">
                    <div class="green-button">
                        <a href="#" alt="Log In" title="Log In" onclick="$('#login_box').fadeIn(&#34;fast&#34;); mouse_is_inside = true; if(frmvalidator != null) { DisableAllValidators();}; return false;">Log In</a>
                    </div>
                    <div class="my-details">
                        <div class="green-button">
                            <a href="/create-account.aspx" alt="Register" title="Register">Register</a>
                        </div>
                    </div>
                    <div id="login_box">
                        <div id="login_box_content">
                            <div class="login-row">
                                <h2 class="login">
                                    Login<a onclick="javascript:$('#login_box').fadeOut(&#34;fast&#34;);"><img alt=''
                                        src="/images/close.png" class="login-close" /></a></h2>
                            </div>
                            <div class="login-row">
                                <div class="login-label">
                                    Email</div>
                                <div class="login-field">
                                    <input type="text" id="txtLoginEmail" autocomplete="off" name="txtLoginEmail" tabindex="1" maxlength="255"
                                        class="login-input" /></div>
                            </div>
                            <div class="login-row">
                                <div class="login-label">
                                    Password</div>
                                <div class="login-field">
                                    <input type="password" name="txtLoginPassword" autocomplete="off" id="txtLoginPassword" tabindex="2"
                                        maxlength="255" class="login-input" />
                                </div>
                            </div>
                            <div class="login-remember">
                                <div class="login-remember-field">
                                    <input name="auto_login" id="auto_login" onclick="displayLogin(this);" tabindex="3" type="checkbox" />
                                </div>
                                <div class="login-remember-label">
                                    Remember Me
                                </div>
                            </div>
                            <div class="login-button">
                                <div class="green-button">
                                    <input type="image" causesvalidation="false" src="/images/log-in.png" alt="login" title="login" tabindex="4" runat="server" id="login" />
                                </div>
                            </div>
                            <div class="login-remember">
                                <a href="/forgotten-password.aspx">Forgot your password?</a>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="previousPage" value="<%=strPreviousPage %>" />
                </asp:Panel>
                <asp:Panel ID="pnlLogoutMenu" runat="server">
                    <div class="green-button">
                        <a href="~/log-in.aspx?logout=1" id="logout" runat="server">Logout</a>
                    </div>
                    <div class="my-details">
                        <div class="green-button">
                            <a id="A1" href="~/update-details.aspx" runat="server">My Details</a>
                        </div>
                    </div>
                </asp:Panel>
            <asp:Panel ID="pnlSearch" runat="server" DefaultButton="ImageButton1">
                    <div class="search-textbox">
                                                <asp:TextBox ID="txtSearch" runat="server" CssClass="search" onblur="if(this.value == '') { this.value='Enter keyword or product code'; isSet=true; }"
                            onmouseover="if(this.value == 'Enter keyword or product code') { this.value='';isSet = true; }"
                            onmouseout="if(this.value == '' && !isSet) { this.value='Enter keyword or product code'; isSet=>false; }"
                            MaxLength="255" Text="Enter keyword or product code" ontextchanged="btnSearch_Click" /><asp:ImageButton ID="ImageButton1"  Class="search-icon" ImageAlign="Right" Width="15" Height="15" BackColor="White"  runat="server" OnClick="btnSearch_Click" ImageUrl="~/images/search-icon.gif" />
                    </div>
                </asp:Panel>

            </div>



                    <input type="hidden" name="previousPage" value="<%=strPreviousPage %>" 
            </div>

            <asp:Panel ID="pnlmsearch" runat="server" DefaultButton="btnmsearch" Visible="true">
                    <div class="search-m">
                        <asp:TextBox ID="txtSearchM" runat="server" Class="search-textbox-m" onblur="if(this.value == '') { this.value='Enter keyword or product code'; isSet=true; }"
                            onmouseover="if(this.value == 'Enter keyword or product code') { this.value='';isSet = true; }"
                            onmouseout="if(this.value == '' && !isSet) { this.value='Enter keyword or product code'; isSet=>false; }"
                            MaxLength="255" Text="Enter keyword or product code" ontextchanged="btnmsearch_Click" />
                        <asp:ImageButton ID="btnmsearch"  Class="search-icon-m" ImageAlign="Right" BackColor="White"  runat="server" Width="30" Height="30" OnClick="btnmsearch_Click" ImageUrl="~/images/search-icon.gif" />
                    </div>
                </asp:Panel>
        <div class="content-holder">
            <div class="page-content-menu">
                <uc4:menu_left ID="Menu_left" runat="server" />
            </div>
            <div class="page-content-info">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
                            </div>
        </div>

        <div class="footer">
        <div id="toggleView">
<?php if ( $fixedwidth == '1' ) { ?>
    <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=0">Switch to Mobile site</a>
<?php } else { ?>
    <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=1">Switch to Full site</a>
<?php } ?>
</div>
            <div class="footer-content">
                <div class="footer-content-border">
                    <div class="footer-payment-method">
                            <div class="footer-payment-method-border">
                                <div class="footer-payment-text">
                                Secure online payments with <strong>3D Secure</strong></div>
                                <div class="footer-payment-visa-verified">
                                    <img runat="server" src="/images/verified-by-visa.png" alt="Verified by Visa" title="Verified by Visa" /></div>
                                    <div class="footer-payment-mastercard-securecode">
                                    <img runat="server" src="/images/mastercard-securecode.png" alt="MasterCard SecureCode" title="MasterCard SecureCode" /></div>
                                <div class="footer-payment-visa">
                                    <img runat="server" src="/images/visa-logo.png" alt="site accepts Visa" title=site accepts Visa" /></div>
                                <div class="footer-payment-mastercard">
                                    <img runat="server" src="/images/mastercard-logo.png" alt="Global PC accepts Mastercard"
                                        title="site accepts Mastercard" /></div>
                                        <div class="footer-payment-payment-express">
                                    <img runat="server" src="/images/payment-express.png" alt=" accepts Payment Express"
                                        title=" PC accepts Payment Express" /></div>
                            </div>
                        </div>
                    <div class="footer-social-media">
                        <div class="footer-title">
                            Social Media</div>
                        <div class="footer-facebook">
                            <a runat="server" href="http://www.facebook.com/" target="_blank">
                                <img runat="server" src="/images/facebook.png" border="0" alt="Become a fan of on Facebook"
                                    title="Become a fan of  on Facebook" /></a>
                        </div>
                    </div>
                    <div class="footer-links-left">
                        <div class="footer-title">
                            Shopping</div>
                        <a href="/">Home</a><br />
                        <a href="/cart.aspx">View cart</a><br />
                        <a href="/promotions.aspx">Promotions</a><br />
                        <a href="/create-account.aspx">Create Account</a>
                    </div>
                    <div class="footer-links-right">
                        <div class="footer-title">
                            About </div>
                        <a href="/contact-us.aspx">Contact us</a><br />
                        <a href="/about-us.aspx">About us</a><br />
                        <a href="/contact-us.aspx">Stores</a><br />
                        <a href="/suppliers.aspx">Suppliers</a><br />
                        <a href="/feedback.aspx">Feedback</a>
                    </div>
                    <div class="footer-legal">
                        <div class="footer-title">
                            Legal Information</div>
                        <a href="/terms.aspx">Terms and Conditions</a><br />
                        <a href="/privacy.aspx">Privacy policy</a><br />
                        <asp:Label ID="lblCopyright" runat="server"></asp:Label>
                    </div>
                </div>
            </div>
        </div>

    </div>

    </form>
    <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        try {
            var pageTracker = _gat._getTracker("UA-8649565-1");
            pageTracker._trackPageview();
        } catch (err) { }</script>
</body>
</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • du229908 du229908 6年前

    To make this a bonafide answer, from the comments and your edits it can be seen that you are attempting to write PHP code into an ASPX file who's language is set to C#, and that is why it does not work.

    You need to rewrite this using ASP.net's template syntax, not PHP.

    点赞 评论 复制链接分享
  • doulu5717 doulu5717 6年前

    You're not using proper PHP syntax for conditionals.

    Here's a fixed version:

    <div id="toggleView">
    <?php if ( $fixedwidth == '1' ) { ?>
        <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=0">Switch to Mobile site</a>
    <?php } else { ?>
        <a href="<?php echo $_SERVER["SCRIPT_NAME"] ?>?fixedwidth=1">Switch to Full site</a>
    <?php } ?>
    </div>
    
    点赞 评论 复制链接分享