0

I have recently learned to create dynamic links via php, but my problem today is I do not know how to do this in a php block, for example I know how to do this:

     <div><a href="blah.php?id=<?php echo $id; ?>">content</a></div>

But I am doing this above the head tags in a php block, so basically I am creating a dynamic link for dynamic content, essentially I am going to dynamically render images on the home page, which are links to products. This is what I have:

    <?php 
     include_once "scripts/connect_to_mysql.php";
     //Select query for latest items
     $dynamic_newest = "";
     $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 24");
     $productCount = mysql_num_rows($sql); // count the output amount
     if ($productCount > 0) {
          while($row = mysql_fetch_array($sql)){
              $id = $row["id"];
              $pid = $id;
              $category = $row["category"];
              $subcategory = $row["subcategory"];
              $dynamic_newest .= "<a href="THIS IS WHAT I DONT KNOW HOW TO DO"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
        }
          } else {
          $dynamic_newest = "<h1>There are no products to display yet.</h1>";
          }
   ?>

This is probably really easy, but I can't find it, maybe I'm not asking magic google the right question. Thanks in advance!

3 Answers 3

2

Not sure if this is what you are looking for but anyway..

$dynamic_newest .= "<a href='blah.php?id=$pid'><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";
Sign up to request clarification or add additional context in comments.

10 Comments

I've tried single quotes already, it didn't work, but i was also trying to echo $id, which I don't think is right now that I see yours... let me try this and I'll let you know in a second. Thanks :)
What have you spotted? How is it different from my answer ? Koi faraq nahi hey :)
@Blaster the original version of your answer was different ;)
Nice to find some Urdu for the first time here :)
@Nathan just to clarify. PHP helps you render HTML/CSS to the browser amongst other things. So to it simple, if you can do it in HTML/CSS, you can do it in PHP, as you are just using PHP to send the HTML to the browser. So yes you can do inline styles and what not with PHP. Hope that helps.
|
2

Escaping issues, change:

 $dynamic_newest .= "<a href="THIS IS WHAT I DON'T KNOW HOW TO DO"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";

To:

 $dynamic_newest .= "<a href=\"blah.php?id=$id\"><img src='inventory_images/$pid.jpg' width='100px' height='100px' /></a>";

3 Comments

if i do that can I just use the: <a href="blah.php?id=<?php echo $id; ?>"> in that spot?
@Nathan: Since your code is already in php, dont use php tags in there again, should be <a href="blah.php?id=$id">. If you are asking out of any php tags, then this is also fine: <div><a href="blah.php?id=<?php echo $id; ?>">content</a></div>
@Nathan: I got your point, updated the link in the answer, please check.
2

While you aren't using this now, I am sure you will find use for it in the future. Later on, when you need to write large blocks of preformatted non-php text and want to add php variables into it, you can do something like this:

echo<<<YOUR_IDENTIFIER

<h1>Hi!</h1>
<form action='post'>
Welcome to my webpage =).
You can place code here as if it were not inside a PHP block,
but you can also use PHP variables. That means you can even
insert quotes like this --> "", though since this is still HTML,
it would be more accurate to use &gt; and &quot;. Your cleanest
bet and best-practice coding method is to {$encapsulate} php
variables in squiggley brackets. You can even
{$encapsulateArrays['likethis']}.
</form>

YOUR_IDENTIFIER;

echo "Back to regular PHP code.";

Make sure there are no spaces or tabs before the YOUR_IDENTIFIER;.

To answer your original question though (not related by my blabber above), be sure to properly escape with a backslash \ any quotes you have so as to not accidentally terminate your string literal. Don't forget to {} encapsulate your variables, even if you are doing regular echo "My {$variable} here"; It won't affect parsing, but will make it a lot easier to debug 2 months down the line, when you're working on your next project.

2 Comments

Thanks for this, I'm sure I'll find plenty of use for it :)
Heredoc syntax is especially handy for large chunk of html or other markup code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.