justlukeyou Posted August 24, 2013 Share Posted August 24, 2013 Hi, I am trying add a widget to certain blog post so that it display products. Just like an image would appear. The code works fine when I paste onto a standard page but when I try to echo it from a database it displays the second peice of PHP code as it was standard text. Any suggestions please? <?php echo $query_row['name']; ?> <?php $query = mysql_query(" SELECT name, linkname, product_id, price, discount, image_link FROM productdbase p INNER JOIN furniture_groups f ON f.id = p.id WHERE linkname LIKE '%coffee%' LIMIT 15"); while ($query_row = mysql_fetch_assoc($query)) { ?> <div class="productrangeborder"> <div class="productsdetailsborder"> <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" class='productlink' rel="nofollow" ><?php echo $query_row['name']; ?> </a> </div> <div class="productimageborder"> <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" rel="nofollow"><img src="<?php echo $query_row['image_link']; ?>" alt="<?php echo $query_row['name']; ?>" /></a> </div> <div class="priceborder">Price £<?php echo $query_row['price']; ?><br /></div><div class="discountborder">Save <?php echo $query_row['discount']; ?>%<br /> </div></div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/ Share on other sites More sharing options...
trq Posted August 25, 2013 Share Posted August 25, 2013 I am trying add a widget to certain blog post so that it display products. Just like an image would appear. The code works fine when I paste onto a standard page but when I try to echo it from a database it displays the second peice of PHP code as it was standard text. No kidding. Think about what you are actually doing. Code is not meant to be stored in a database. Your doing it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446609 Share on other sites More sharing options...
justlukeyou Posted August 25, 2013 Author Share Posted August 25, 2013 No kidding. Think about what you are actually doing. Code is not meant to be stored in a database. Your doing it wrong. Thanks, How can I echo a 'widget' onto a pirticular blog post? Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446652 Share on other sites More sharing options...
trq Posted August 25, 2013 Share Posted August 25, 2013 How can I echo a 'widget' onto a pirticular blog post? Far too vague a question I'm afraid. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446658 Share on other sites More sharing options...
justlukeyou Posted August 25, 2013 Author Share Posted August 25, 2013 I want to echo products from a database in the middle of certain blog posts. So if Im writing about Mario Console games I can echo "Mario" from my database into a product outlay. It works on a standard page but I need echo it on specific blog posts which are stored in my database. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446659 Share on other sites More sharing options...
jazzman1 Posted August 25, 2013 Share Posted August 25, 2013 What's the extension of the file? It's php or something else? Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446661 Share on other sites More sharing options...
justlukeyou Posted August 25, 2013 Author Share Posted August 25, 2013 It is echoed onto a php file. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446662 Share on other sites More sharing options...
trq Posted August 25, 2013 Share Posted August 25, 2013 I want to echo products from a database in the middle of certain blog posts. So if Im writing about Mario Console games I can echo "Mario" from my database into a product outlay. It works on a standard page but I need echo it on specific blog posts which are stored in my database. All sorts of way of doing that. Generally though, you are just looking to replace some content with some other content as you get it from the database. The basic concept is simple: $original = "This is some test about {{foo}}, and how good it is"; $text = preg_replace_callback( '/\{(.*?)\}/', function() { // some logic to return "foo" from the database } , $original ); echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446664 Share on other sites More sharing options...
mac_gyver Posted August 25, 2013 Share Posted August 25, 2013 (edited) edit: basically says the same as trq's post above (why wasn't that there when i viewed the thread?) You would use a tag in the content, like a custom bbcode tag, that when the output is processed and sent to the browser, the tag would be replaced with the actual content. only the tag would be stored in the database. the actual php code that does the work would be part of your application code. Edited August 25, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446665 Share on other sites More sharing options...
justlukeyou Posted August 25, 2013 Author Share Posted August 25, 2013 All sorts of way of doing that. Generally though, you are just looking to replace some content with some other content as you get it from the database. The basic concept is simple: $original = "This is some test about {{foo}}, and how good it is"; $text = preg_replace_callback( '/\{(.*?)\}/', function() { // some logic to return "foo" from the database } , $original ); echo $text; This echoes title of the blog post regardless of whether I include the $original variable or not. Are you suggesting your code goes after my code and your code echoes the existing code I readly have? Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446667 Share on other sites More sharing options...
trq Posted August 26, 2013 Share Posted August 26, 2013 Your not very good at this asking for help thing are you? Try posting some relvant code. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446764 Share on other sites More sharing options...
justlukeyou Posted August 26, 2013 Author Share Posted August 26, 2013 I'm trying to echo the following code from a database. I'm not sure how I can explain it any easier. <?php $query = mysql_query(" SELECT name, linkname, product_id, price, discount, image_link FROM productdbase p INNER JOIN furniture_groups f ON f.id = p.id WHERE linkname LIKE '%coffee%' LIMIT 15"); while ($query_row = mysql_fetch_assoc($query)) { ?> <div class="productrangeborder"> <div class="productsdetailsborder"> <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" class='productlink' rel="nofollow" ><?php echo $query_row['name']; ?> </a> </div> <div class="productimageborder"> <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" rel="nofollow"><img src="<?php echo $query_row['image_link']; ?>" alt="<?php echo $query_row['name']; ?>" /></a> </div> <div class="priceborder">Price £<?php echo $query_row['price']; ?><br /></div><div class="discountborder">Save <?php echo $query_row['discount']; ?>%<br /> </div></div> <?php } ?> I dont know what this code is supposed to do. All it does is echo the title of the blog. $original = "This is some test about {{foo}}, and how good it is"; $text = preg_replace_callback( '/\{(.*?)\}/', function() { // some logic to return "foo" from the database } , $original ); echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446823 Share on other sites More sharing options...
trq Posted August 26, 2013 Share Posted August 26, 2013 It is an example of the logic involved, its not meant to be working code. If you want someone to write code for you hire a programmer. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446829 Share on other sites More sharing options...
justlukeyou Posted August 26, 2013 Author Share Posted August 26, 2013 Could you explain the logic and what I am supposed to do with it. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446833 Share on other sites More sharing options...
justlukeyou Posted August 26, 2013 Author Share Posted August 26, 2013 (edited) Hi, There is something seriously wrong with echoing the code from the database. I put this simple test on my page which echoes toad. $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. $test = toad; echo $test; ?> Edited August 26, 2013 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446893 Share on other sites More sharing options...
trq Posted August 27, 2013 Share Posted August 27, 2013 No kidding. Think about what you are actually doing. Code is not meant to be stored in a database. Your doing it wrong. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446921 Share on other sites More sharing options...
justlukeyou Posted August 27, 2013 Author Share Posted August 27, 2013 No kidding. Think about what you are actually doing. Code is not meant to be stored in a database. Your doing it wrong. What should I be doing then. There must be a way to do this. Every example I have seen involves storing code in the database. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1446937 Share on other sites More sharing options...
justlukeyou Posted August 27, 2013 Author Share Posted August 27, 2013 Any suggestions please? Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1447042 Share on other sites More sharing options...
trq Posted August 28, 2013 Share Posted August 28, 2013 Every example I have seen involves storing code in the database. Where the fuck are you reading? Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1447099 Share on other sites More sharing options...
KevinM1 Posted August 28, 2013 Share Posted August 28, 2013 "Yo dawg, I heard you like writing PHP to access your database. So I put PHP in your database so you can write PHP code to access your database with the code that's in your database." Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1447105 Share on other sites More sharing options...
justlukeyou Posted August 28, 2013 Author Share Posted August 28, 2013 Okay thanks. I think I have a solution which no one has suggested. I shall try it and let you know. Quote Link to comment https://forums.phpfreaks.com/topic/281522-echoing-php-from-database-widget/#findComment-1447120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.