SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Output issue.

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Output issue.

    I am having an issue outputting any of the html. The function outputs fine. I have tried echoing each line of html with no luck as well. What am i doing wrong. Thanks for any help.

    PHP Code:
    <?php if (my_function() !== null) : ?>
        <div class="list">
            <p class="clickable">Click To Find</p>
        <ul class="collapse">
    <?php my_function (); ?>
        </ul>
        </div>
    <?php endif; ?>
    My output should look like this

    HTML Code:
    <div class="list">
        <p class="clickable">Click To Find</p>
    	 <ul class="collapse">
                <li>my function</li>
    	</ul>
    </div>

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    First off, can you provide the output you are currently getting?

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Yes, I am just getting the
    HTML Code:
    <li>my function</li>
    which is generated by
    PHP Code:
    <?php my_function (); ?>

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, my first recommendation is to replace
    PHP Code:
    if (my_function() !== null) : 
    With
    PHP Code:
    if (function_exists('my_function')) : 
    See if that helps resolve your output

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the recommendation, but in this case the function will exist, but it does not have any values stored in it. That is why I had to use the previous.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You can't use the previous if you are performing an echo, print, or similar function in your my_function(). As the output will always occur regardless of what is returned.

    Instead of performing output within your function, return what you want outputted.

    Example:
    PHP Code:
    function my_function()
    {
      return 
    '<li>my_function</li>';

    That would change your code to the following:
    PHP Code:
    <?php $myFunctionOutput my_function();
          if (
    $myFunctionOutput !== null) : ?> 
        <div class="list"> 
            <p class="clickable">Click To Find</p> 
        <ul class="collapse"> 
    <?php echo $myFunctionOutput?> 
        </ul> 
        </div> 
    <?php endif; ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @cpradio thanks for working with me. I had previously assigned a variable to the function and that works well, but I am still not getting any of the html part returned. All I get is this
    HTML Code:
    <li>the function</li>
    This part is not being outputted at all

    HTML Code:
    <div class="list"> 
            <p class="clickable">Click To Find</p> 
        <ul class="collapse">
    I of course could add all of the above into the function so it outputs with the rest, but that would require me to use those styles, which will not work in this case. I do apologize if I am confusing, maybe returning it in the function is my only option.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Can you paste your function, and the code that calls your function (the updated versions) so I can see them. I must be assuming something that isn't quite right.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its a pretty big function to paste and I do not want for you to have to read through all the code. I do appreciate you offering though. I will try again to explain what it is I want to do.

    Basically, I have a function that stores some values. If those values are true, it will output this

    HTML Code:
    <div class="list">
        <p class="clickable">Click To Find</p>
    	 <ul class="collapse">
                <li>my function output</li>
    	</ul>
    </div>
    However, I have ran into one case that the values will be empty so nothing will output from the function. However, the html is still getting displayed .I dont want the html to display the value if the function is null.

    So with the above code I can return the function if it is true or not. The issue is, that I need to output The <div class="list">, <p class="clickable">Click To Find</p> and <ul class="collapse">with it, without putting that html directly in the function. Hopefully that is clear as mud, if not, I will accept the title as bumbling idiot of the day ..

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I would have thought this code would have worked then:
    PHP Code:
    <?php $myFunctionOutput my_function();
          if (
    $myFunctionOutput !== null) : // did you see my change here? 
    ?>  
        <div class="list"> 
            <p class="clickable">Click To Find</p> 
        <ul class="collapse"> 
    <?php echo $myFunctionOutput?> 
        </ul> 
        </div> 
    <?php endif; ?>

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I did see that and I changed it so its clean as you wrote it, but the <div>, <p> and <ul> are not outputting.

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,151
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    So the output being generated is null then? You would need to look into why that is. If you are still seeing the <li>my_function</li> output, that is because the data is not being returned, it is being outputted. To rectify that, you may have to use output buffering

    Something like:
    PHP Code:
    <?php 
      ob_start
    ();
      
    my_function();
      
    $myFunctionOutput ob_get_contents();
      
    ob_end_clean();

      if (
    $myFunctionOutput !== null) :
    ?>  
        <div class="list"> 
            <p class="clickable">Click To Find</p> 
        <ul class="collapse"> 
    <?php echo $myFunctionOutput?> 
        </ul> 
        </div> 
    <?php endif; ?>

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you cpradio. I appreciate all your help. I will give that a try and read up on it as well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •