SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 66
  1. #26
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    When I paste the code onto a .php page the code works as desired. It displays what is in my database.

    But when I paste the code into my database and then echo the code onto the page the code doesn't work.

    The query works. The issue I have is when I echo it from a database. It is the process of echoing it from a database onto a page that is causing the problem.

  2. #27
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    You're trying to take a string from your DB and have it executed as PHP, aren't you? It would be possible to do that using eval, but it's really not a good idea.

    Can I ask why you want to store PHP in the DB?

  3. #28
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    When I paste the code onto a .php page the code works as desired. It displays what is in my database.

    But when I paste the code into my database and then echo the code onto the page the code doesn't work.

    The query works. The issue I have is when I echo it from a database. It is the process of echoing it from a database onto a page that is causing the problem.
    Are you kidding me? Are saying you are putting this query code in your database? What are you trying to accomplish? There must be a better way.

  4. #29
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I am trying to echo a product display onto a blog post.

    Im not sure how else I can achieve this. For example if I write a blog post about Super Mario games I want to echo Super Mario games from my database onto that specific blog post.

  5. #30
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Make a little function at the top of your page to query table and build result for display. Something like this.
    PHP Code:
    function showproduct($linkname){ 
        
    $sql "
            SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
                FROM productdbase as p
                INNER JOIN furniture_groups as f
                    ON
                        f.id = p.id
                WHERE
                    p.linkname LIKE '%
    $linkname%'
            LIMIT 15"
    ;
        
    $result mysql_query($sql) or die(mysql_error());
        
    $output "";
        while (
    $query_row mysql_fetch_assoc($result)) {
            
    $output .= "<div class=\"productrangeborder\">
            <div class=\"productsdetailsborder\">
            <a href=\"http://website.co.uk/products/product/
    {$query_row['product_id']}\" class='productlink' rel=\"nofollow\" >{$query_row['name']}</a>
            </div>
            
            <div class=\"productimageborder\">
            <a href=\"http://website.co.uk/products/product/
    {$query_row['product_id']}\" rel=\"nofollow\"><img src=\"{$query_row['image_link']}\" alt=\"{$query_row['name']}\" /></a>
            </div>
            
            <div class=\"priceborder\">Price 
    {$query_row['price']}<br /></div><div class=\"discountborder\">Save {$query_row['discount']}%<br />
            </div></div>"
    ;
        }
        return 
    $output;

    Then in your page where you need it, you call the function.
    PHP Code:
    $product "coffee";
    echo 
    showproduct($product); 

  6. #31
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried that but no success.

    There is something seriously wrong with echoing the code from the database. I put this simple test on my page which echoes toad.

    Code:
    $test = toad;
    
    echo $test;
    
    ?>
    But when I try it in echoing it from my database it doesn't display anything. The issue is echoing the code from the database.

    Code:
    <?php
    
    $test = toad;
    
    echo $test;
    
    ?>

  7. #32
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Simple answer... don't do it.

  8. #33
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    Simple answer... don't do it.
    What else can I do?

  9. #34
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    What else can I do?
    Keep your data, logic, and presentation separate.. the DB should contain just the data (sometimes a little HTML is unavoidable, such as P tags on text etc.), and your logic should live in functions (or objects, if you're doing OOP), and your HTML should ideally be in separate files with the minimum of PHP necessary to loop and echo your data.

  10. #35
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Keep your data, logic, and presentation separate.. the DB should contain just the data (sometimes a little HTML is unavoidable, such as P tags on text etc.), and your logic should live in functions (or objects, if you're doing OOP), and your HTML should ideally be in separate files with the minimum of PHP necessary to loop and echo your data.
    Thanks,

    But if a simple peice of PHP code doesn't work then what can I do?

    Are you saying its not possible to echo PHP onto a page and for it to work?

  11. #36
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Well it's design issue. Say you have a blog that talks about "Super Mario games". Somewhere you've defined this as the topic, right? So set this to a variable and make your query based on that. I see no reason you have to have a hard-coded query saved to database for every topic. Call a function if this variable is set.
    PHP Code:
    if (isset($product)){
        echo 
    showproduct($product); 

    You can even do it without the function if you wish by just wrapping the query in an IF condition.
    PHP Code:
    if (isset($product)){
        
            
    $sql "
            SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
                FROM productdbase as p
                INNER JOIN furniture_groups as f
                    ON
                        f.id = p.id
                WHERE
                    p.linkname LIKE '%
    $product%'
            LIMIT 15"
    ;
        
    $result mysql_query($sql) or die(mysql_error());
        
    $output "";
        while (
    $query_row mysql_fetch_assoc($result)) {
            
    $output .= "<div class=\"productrangeborder\">
            <div class=\"productsdetailsborder\">
            <a href=\"http://website.co.uk/products/product/
    {$query_row['product_id']}\" class='productlink' rel=\"nofollow\" >{$query_row['name']}</a>
            </div>
            
            <div class=\"productimageborder\">
            <a href=\"http://website.co.uk/products/product/
    {$query_row['product_id']}\" rel=\"nofollow\"><img src=\"{$query_row['image_link']}\" alt=\"{$query_row['name']}\" /></a>
            </div>
            
            <div class=\"priceborder\">Price 
    {$query_row['price']}<br /></div><div class=\"discountborder\">Save {$query_row['discount']}%<br />
            </div></div>"
    ;
        }
        echo 
    $output


    But if you need this display on several pages. Just make a page to hold the function like I posted above and include it at the top of your pages.

  12. #37
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Keep your data, logic, and presentation separate.. the DB should contain just the data (sometimes a little HTML is unavoidable, such as P tags on text etc.), and your logic should live in functions (or objects, if you're doing OOP), and your HTML should ideally be in separate files with the minimum of PHP necessary to loop and echo your data.
    EXACTLY

  13. #38
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry guys but I think you are really missing the point.

    What ever code I use I cannot echo it from my database and get it to work.

    Even this basic test doesn't work. Until I resolve this issue it doesn't matter what code I use.

    Code:
    <?php
    
    $test = toad;
    
    echo $test;
    
    ?>

  14. #39
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Thanks,

    But if a simple peice of PHP code doesn't work then what can I do?

    Are you saying its not possible to echo PHP onto a page and for it to work?
    Well, as I said before, technically it's possible.. but as Drummin said, it's pointing to a problem with your design. Could you post an example of the PHP code you have stored in the DB for one of your products? It'd be easier to give you advice based on what you're trying to output.

  15. #40
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

  16. #41
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    Well, as I said before, technically it's possible.. but as Drummin said, it's pointing to a problem with your design. Could you post an example of the PHP code you have stored in the DB for one of your products? It'd be easier to give you advice based on what you're trying to output.

    Im not sure what you mean by "PHP code you have stored in the DB for one of your products?" I don't have any PHP in the DB for the products.

    Could you give a idiots guide of what you expect the code to do and why the simple test I am trying to do doesn't work?

    I take when the database echoes the PHP code the page isn't reading the PHP code? It is echoing code inside code which it doesn't like?

  17. #42
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    OK, let's backtrack a second because perhaps I'm misunderstanding what's going on here.

    What happens if you try this code (your example earlier was missing quotes)?
    PHP Code:
    <?php

    $test 
    "Can you see me?";

    echo 
    $test;

    ?>

  18. #43
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    OK, let's backtrack a second because perhaps I'm misunderstanding what's going on here.

    What happens if you try this code (your example earlier was missing quotes)?
    PHP Code:
    <?php

    $test 
    "Can you see me?";

    echo 
    $test;

    ?>
    This doesn't display anything.

  19. #44
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    This doesn't display anything.
    What is your set up, are you running PHP on your local machine with WAMP, XAMPP or something like that?

  20. #45
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Yes fretburner, I believe we covered this, which was what was confusing me. When he says he can paste the code into the page and it works but not from the database. So on post 43, he probably attempted to put "that code" in the database and echo it. Correct me if I'm wrong justlikeyou.

  21. #46
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I take when the database echoes the PHP code the page isn't reading the PHP code?
    Correct
    It is echoing code inside code which it doesn't like?
    Correct

    Conclusion: Don't do it. Change your approach.

    Maybe you could explain WHY you feel you need to do it this way. Having a better understanding of WHAT you're trying to do might help.

  22. #47
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Let's say this is a page that can display any type of content. I will assume that somewhere you have defined this content type or category and based on this you are attempting to grab a query from the database to show your items. What we a telling you is to use a query hard coded on that page and the "Product" or "Category" , e.g. "coffee","Super Mario games" etc is what you save to the database. So if for example this is a blog about "Super Mario games", you would have a field in your database called `category`, which would hold the value "Super Mario games" and this value, which you set to a variable is what you use to query for items to display. Make sense?

  23. #48
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I want to have a blog which display specific products based on the subject matter of each blog post.

    How can I achieve this if I cant echo the code to display the products from a database?

    If I write a blog post about Mario I just set the query to Mario. Echo the code that works on a standard and the issue is sorted.

  24. #49
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    I want to have a blog which display specific products based on the subject matter of each blog post.

    How can I achieve this if I cant echo the code to display the products from a database?

    If I write a blog post about Mario I just set the query to Mario. Echo the code that works on a standard and the issue is sorted.
    Is this a blog that you're writing yourself, or you're using something like WordPress?

  25. #50
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Blog on a hand built site.

    I've made the whole site, just need to figure how to echo the products onto the blog part of the site.


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
  •