【问题标题】:PHP Multiple variables from inside an If else statement that is inside an While statementPHP 来自 While 语句内的 If else 语句内的多个变量
【发布时间】:2014-10-17 11:13:30
【问题描述】:

我开始一点一点地学习如何用 PHP 编程。但是我遇到了一个我自己无法解决的问题。所以我需要一点帮助。问题是,在我的函数中,我从数据库中获取信息。我想在下面发布的其他函数中使用变量 $DisplayProducts01Title

获取数据的函数:

//GetAllPages
function GetAllPages() {
    $GetAllPagesSql = "SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName, ContentInformation.ContentInformationTitle, ContentPages.ContentPagesOrder FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID = ContentInformation.ContentInformationID ORDER BY ContentPagesOrder";
    $GetAllPagesQuery = mysql_query($GetAllPagesSql);
    while (($GetAllPagesRow = \mysql_fetch_assoc($GetAllPagesQuery)) != false) {
        if($GetAllPagesRow[ContentTypeName] == 'products01') {
            DisplayProducts01DesignFunction();
            $DisplayProducts01Title = $GetAllPagesRow[ContentTypeTitle];
            return $DisplayProducts01Title;
        }
        else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
            DisplayInformation01DesignFunction();
        }
    }
}

显示数据的功能:

//DisplayProductsDesignFunction
function DisplayProducts01DesignFunction($DisplayProducts01Title) {
        echo "<div class='paddingcnt' id='services'>
        <div class='row-fluid'>
                <div class='span12'>
                        <div class='pricing'>
                                <div class='container'>
                                        <div class='row-fluid'>
                                                <h1>" . $DisplayProducts01Title . "</h1>
                                                <div class='row-fluid'>";
                                                        DisplayProducts01Function();
                                                        echo "</div>
                                                </div>
                                        </div>
                                </div>
                        </div>
                </div>
        </div>";
}

如何将函数 GetAllPages() 中的变量 $DisplayProducts01Title 用于函数 DisplayProducts01DesignFunction()?

提前致谢

【问题讨论】:

    标签: php mysql if-statement while-loop


    【解决方案1】:

    您需要在调用时将其传递给该函数。见下文:

    //GetAllPages
    function GetAllPages() {
      $GetAllPagesSql = "SELECT ContentPages.ContentPagesID, ContentType.ContentTypeName, ContentInformation.ContentInformationTitle, ContentPages.ContentPagesOrder FROM `ContentPages` INNER JOIN ContentType ON ContentPages.ContentTypeID = ContentType.ContentTypeID INNER JOIN ContentInformation ON ContentPages.ContentInformationID =   ContentInformation.ContentInformationID ORDER BY ContentPagesOrder";
      $GetAllPagesQuery = mysql_query($GetAllPagesSql);
      while (($GetAllPagesRow = \mysql_fetch_assoc($GetAllPagesQuery)) != false) {
          if($GetAllPagesRow[ContentTypeName] == 'products01') {
            // changes start here
            $DisplayProducts01Title = $GetAllPagesRow[ContentTypeTitle];  
            DisplayProducts01DesignFunction($DisplayProducts01Title);
            // changes end here
          }
          else if ($GetAllPagesRow[ContentTypeName] == 'information01') {
              DisplayInformation01DesignFunction();
          }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-28
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多