Jump to content

Echoing PHP from Database (Widget)


justlukeyou

Recommended Posts

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 
 }
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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 by justlukeyou
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.