简体   繁体   中英

PHP Multiple variables from inside an If else statement that is inside an While statement

I'm starting to learn bit by bit how to program in PHP. But I have come to a problem I can't fix myself. So I require a bit of help. The problem is, in my function I get information from my database. The variable $DisplayProducts01Title I want to use into my other function that is posted below

Function to get the data:

//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();
        }
    }
}

Function to show the data:

//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>";
}

How can I use the variable $DisplayProducts01Title from function GetAllPages() into function DisplayProducts01DesignFunction()?

Thanks in advance

You need to pass it into that function when it is called. See below:

//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();
      }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM