Thread: Echo PHP code

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post Echo PHP code


    Hi There,

    I'm having a situation that I would appreciate to get some ideas on.

    My goal is to generate my php documents dynamically. I have an HTML template, then based on the variable that I get from the GET method, I want to generate the appropriate body.

    This is the template:

    Code:
    <?php
    require_once 'functions.php';
    $document_type_name = $_GET["document_type"];
    $document_title = get_document_title($document_type_name);
    
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    
    <!-- BEGIN HEAD -->
    
    <head>
    <title>Submit a new <?php echo $document_title ?></title>
    <!-- BEGIN METACONTENTS -->
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="" name="description" />
    <meta content="" name="keywords" />
    <meta content="index,follow" name="robots" />
    <!-- END METACONTENTS -->
    <!-- BEGIN CSS -->
    css links ...
    <!-- END CSS -->
    <!-- BEGIN JAVASCRIPTS -->
    <script src="functions.js" type="text/javascript"></script>
    <!-- END JAVASCRIPTS -->
    <!-- BEGIN jQuery -->
    ...
    <!-- END jQuery -->
    <script type="text/javascript">
    	... some java scripts
    </script>
    <!-- END Needed for JS DatePciker,AutoComplete -->
    </head>
    <!-- END HEAD -->
    <!-- BEGIN BODY -->
    <body>
    <!-- START Header -->
    <header class="header_bg clearfix">
    	<div class="container clearfix">
    		<!-- START Logo -->
    		<div class="logo">
    			<a href="css/index.html"><img alt="" src="css/images/logo.png" /></a>
    		</div>
    		<!-- END Logo -->
    		<!-- START Master Nav -->
    		<nav class="main-menu">
    		...
    		</nav>
    		<!-- END Master Nav --></div>
    </header>
    <!-- END Header -->
    <!-- START DOCUMENTCONTENTS -->
    <section class="container clearfix">
    	<!-- START DOCUMENT TITLE -->
    	<header class="container page_info clearfix">
    		<h1 class="regular brown">Submit a new <?php echo $document_title ?></h1>
    	</header>
    	<!-- END DOCUMENT TITLE -->
    	<!-- START CONFIRM POPUP DIV -->
    	<div id="confirm_dialog" title="Confirm">
      	<p>This will submit the new document and distribute it to the selected users. Do you wish to continue?</p>
    	</div>
    	<!-- END CONFIRM POPUP DIV -->
    	<!-- START ERROR POPUP DIV -->
    	<div id="adduser_errordialog" title="Error">
      	<p>Selected user already exists in the list! Please choose another one. <br>
    	If you wish to change the distribution type, <strong>remove</strong> the user and add it again.
    	</p>
    	</div>
    	<!-- END ERROR POPUP DIV -->
    	<div class="container">
    		<!-- START DOCUMENT WEBFORM -->
    		<form action="submit.php" method="post" name="submitdocument" id="submitdocument">
    			<?php
    			$text = file_get_contents("html_strings/rfi.txt");
    			echo($text);
    			?>
    		</form>
    		<!-- END DOCUMENT WEBFORM --></div>
    	<div class="clear padding20">
    	</div>
    	<div class="clear">
    	</div>
    </section>
    <!-- END DOCUMENTCONTENTS -->
    <!-- START footer -->
    <footer class="footer_bg_bottom clearfix">
    	<div class="footer_bottom container">
    		...
    	</div>
    </footer>
    <!-- END footer -->
    
    </body>
    
    </html>
    in <form> I need to read the content from an external source (either MySQL table or a text file) base on the $document_type_name.
    The content of this specific document is just a table with a couple of controls. The text file does also include php code, something like this:

    Code:
    <?php GenerateSelect('tbl_projects', 'name', '', 'project_id', 'id');	?>
    the problem is when I read the content from the text file and echo it to the HTML page, the generate php line gets commented out. When I view the page source it's like this:

    Code:
    <!--?php	GenerateSelect('tbl_projects', 'name', '', 'project_id', 'id'); 	?-->
    Why does this happen? How can I avoid this? and if you have any better suggestions for me to generate my content dynamically from an external source, please do let me know. Appericiate your help in advance.

    Cheers,
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Basically, take the php function call out of the html, replace it with a variable.

    Call the function in your main php code when appropriate and get your results and produce the content you want to see and assign it all to that variable you place in the html.

    I build my scripts using a single function to output all my html, with the necessary php vars stuck in where needed. My php code (outside of this function) does all the work to populate those vars in the html and when done, I call my 'displaypage' function and exit.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Originally Posted by jimmyg999
    Basically, take the php function call out of the html, replace it with a variable.

    Call the function in your main php code when appropriate and get your results and produce the content you want to see and assign it all to that variable you place in the html.

    I build my scripts using a single function to output all my html, with the necessary php vars stuck in where needed. My php code (outside of this function) does all the work to populate those vars in the html and when done, I call my 'displaypage' function and exit.
    so, if I have a variable as the output of the function, then it wouldn't be commented out in the HTML?

    something like this ?:

    PHP Code:
    <?php $output GenerateSelect('tbl_projects''name''''project_id''id');    ?>
    then in my HTML:

    PHP Code:
    echo $output
    Did I understand you correctly?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Dont know your syntax on the first line
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Originally Posted by jimmyg999
    Dont know your syntax on the first line
    It's a function which creates SELECT elements in my html page:

    PHP Code:
    function GenerateSelect($table$column$style$controlname$valuecolumn) {
        
    connect_db();
        try {

            
    $sql "SELECT $column,$valuecolumn FROM $table";
            
    $result mysql_query($sql);

            
            if (empty(
    $style))
            {
            echo 
    "<select id='$controlname' name='$controlname'>";
            }
            else
            {
            
    $style "'" $style "'";
            echo 
    "<select id='$controlname' name='$controlname' style=$style>";
            }
            echo 
    "<option value=''> </options>";
            while (
    $row mysql_fetch_array($result)) {
                echo 
    "<option value='" $row[$valuecolumn]  . "'>" $row[$column] . "</option>";
            }
        } catch (
    Exception $e) {
            
    //echo 'Caught exception: ', $e -> getMessage(), "\n";
            
    log_error('GenerateSelect',$e->getMessage());
        }
        echo 
    "</select>";

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Dont get

    $output : xxxxxx
    What is that?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Originally Posted by jimmyg999
    Dont get

    $output : xxxxxx
    What is that?
    I get the reason behind your confusion. My bad it was a typo. It was supposed to be = instead of :.

    $output = xxxxxx

    I'm assigning the return value of the function to the variable $output.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Let's make it simple:

    I have a text file, the content is the following:
    Code:
    <?php GenerateSelect('tbl_phases', 'title', '', 'phase_id', 'id'); ?>;
    in my php file, first I read the content of the file and assign a variable for it:
    Code:
    $text = file_get_contents("html_strings/file.txt");
    so at this point the content of $text is a string holding that 1st line of code.
    Now, what I want to do is to echo that in a specific location. Like this:
    Code:
    <form action="submit.php" method="post" name="submitdocument" id="submitdocument">
    <?php echo($text);?>
    </form>
    When I do like this, the echo'ed line is commented out!
    Code:
    <form action="submit.php" method="post" name="submitdocument" id="submitdocument">
    <!--?php	GenerateSelect('tbl_phases', 'title', '', 'phase_id', 'id'); ?-->
    </form>
    so the generated SELECT element is not displayed... why?! What should I do to avoid this?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    724
    Rep Power
    7
    Are you aiming to literally show PHP on your page? Or do you wish the output of this function to display? If that is not commented out, there will be no form and people will be visually reading the line:

    <?php GenerateSelect('tbl_phases', 'title', '', 'phase_id', 'id'); ?>

    And the GenerateSelect() function won't even process/run. Is that the intent/goal to not run, but just to display?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Very confusing post you have here.

    Why do you want to display actual php code on your web page??????

    As for the commenting - you MUST be doing something else with this output before it hits the screen.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Originally Posted by jimmyg999
    Very confusing post you have here.

    Why do you want to display actual php code on your web page??????

    As for the commenting - you MUST be doing something else with this output before it hits the screen.
    I'm sorry if it sounds that confusing.

    Listen, I have a template document, with head, body and form tags. OK? the name of the file is document.php.

    In the form I need to show different kinds of elements depending on the document type. How do I get the document type? by GET method.

    Code:
    document.php?document_type=xxx
    or

    Code:
    document.php?document_type=yyy
    so in my document.php I process the GET variable and need to show the proper form elements within the <form>. Since each document has it's own set of form elements. Some of the form elements are generated using php code. For example, GenerateSelect('tbl_projects', 'name', '', 'project_id', 'id');
    reads the contents from tbl_projects and creates a SELECT element and populates it with proper OPTIONS.

    I keep each document's type form contents in text files.

    document_xxx.txt holds the form elements (as in proper HTML form tags) for document xxx and I just read the the content and echo them in template.

    at the same time document_yyy.txt holds the form elements for document type yyy.

    in my document.php based on the GET variable, I read the content of the appropriate txt file and echo them in the right place which is in div.

    example of the document text file is like this:

    Code:
    <table>
    			<thead>
    				<tr>
    					<th colspan="4">General Information:</th>
    				</tr>
    			</thead>	
    				<tr>
    					<td>Subject:</td>
    					<td colspan="3">
    					<input id="subject" name="subject" placeholder="Enter subject here" type="text"/>
    					</td>
    				</tr>
    				<tr>
    					<td>Project:</td>
    					<td id="projects" colspan="3"><?php
    					GenerateSelect('tbl_projects', 'name', '', 'project_id', 'id');
    					?></td>
    				</tr>
    notice that in the first td I have a simple text input, in the second td I have the php code which generates the SELECT input.

    the first one is displayed correctly, however, the SELECT element is never displayed because it gets commented out automatically when I echo it. Get it?

    Again, I'm reading this content from a text file and echoing it to the document.php based on the given GET variable.

    and by the way, in document.php (the template) I'm echoing like this:
    Code:
    <?php
    $text = file_get_contents("html_strings/document_xxx.txt");
    ?>
    ...
    <div class="container">
    		<!-- START DOCUMENT WEBFORM -->
    		<form action="submit.php" method="post" name="submitdocument" id="submitdocument">
    			<?php
    			echo($text);
    			?>
    		</form>
    		<!-- END DOCUMENT WEBFORM --></div>
    	<div class="clear padding20">
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    ok - so you are not trying to output PHP code. You need to alter your generateselect function to concatenate the results to a variable instead of echoing them and then to return that variable.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Location
    Earth, Solar system
    Posts
    30
    Rep Power
    4

    Post


    Originally Posted by jimmyg999
    ok - so you are not trying to output PHP code. You need to alter your generateselect function to concatenate the results to a variable instead of echoing them and then to return that variable.
    Thank you for your help and reply.

    so you mean that instead of all of those echos in GenerateSelect, I store them in a string variable?

    something like this:

    PHP Code:
    function select_gen($table$column$style$controlname$valuecolumn) {
        
    connect_db();
        try {

            
    $sql "SELECT $column,$valuecolumn FROM $table";
            
    $result mysql_query($sql);
            
            
    $output_val "";
            
            if (empty(
    $style))
            {
            
    $output_val "<select id='$controlname' name='$controlname'>";
            }
            else
            {
            
    $style "'" $style "'";
            
    $output_val "<select id='$controlname' name='$controlname' style=$style>";
            }
            
    $output_val=  $output_val."<option value=''> </options>";
            while (
    $row mysql_fetch_array($result)) {
                
    $output_val $output_val."<option value='" $row[$valuecolumn]  . "'>" $row[$column] . "</option>";
            }
        } catch (
    Exception $e) {
            
    //echo 'Caught exception: ', $e -> getMessage(), "\n";
            
    log_error('GenerateSelect',$e->getMessage());
        }
        
    //echo "</select>";
        
    return $output_val;

    I really appreciate your effort to understand my situation and help me out
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    Concatenate. Concatenate.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    724
    Rep Power
    7
    Aight. From what it is now sounding, your goal is VERY simple. I would use the switch() function with this. Build each file to be included as you wish for your form. Then, run something similar to this between your form tags to include the desired form:
    PHP Code:
    switch($_GET['type']) {
      case 
    "add":
        include(
    'file_add.php');
        break;
      case 
    "edit":
        include(
    'file_edit.php');
        break;
      case 
    "remove":
        include(
    'file_remove');
        break;

    And you mention you have a text file...and then mention a PHP file. You're not writing this in HTML files, are you? This is all to be done in a PHP file, otherwise PHP functions and variables do nothing since the server has zero clue it's PHP.
    Last edited by Triple_Nothing; July 29th, 2013 at 12:06 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo