Putting HTML Inside Curly Brackets - Simplyfying

Hi,

I am trying to add a code to display products and pagination together but I having real problems in fitting the HTML inside the curly tags. Is there anyway to seperate these out.

So I just echo each column inside its own set of PHP brackets.

$pages_query = mysql_query("SELECT COUNT(`name`) FROM `productdbase`");
$pages = mysql_result($pages_query, 0);
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ?  (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query_string = "SELECT `name`, `rrp`, `image_link`, `image_link`, `discount` FROM `productdbase` LIMIT $start, $per_page";
$query = mysql_query($query_string);
while ($query_row = mysql_fetch_assoc($query)) {
  echo '<p>', $query_row['name'] ,'</p>';
  echo '<p>', $query_row['rrp'] ,'</p>';
  echo '<p>', $query_row['image_link'] ,'</p>';
  echo '<p>', $query_row['name'] ,'</p>'; 
} 
if ($pages >= 1 && $page <= $pages) { 
  for ($x=1; $x<=$pages; $x++) { 
    echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> '; 
  } 
}

Is there a way I can simplify this?

'<div class="productrangeborder">'
echo '<div class="productdetailsborder"><a href="product.php?product_id=' . $query_row['product_id'] . '">' . $query_row['name'] . '</a></div>';
echo '<div class="productimageborder"><a href="product.php?product_id=' . $query_row['product_id'] . '"><img src="' . $query_row['image_link'] . '"/></a></div>';
echo '<div class="priceborder">' . $query_row['rrp'] . '</div>';
echo '<div class="discountborder">' . $query_row['discount'] . '</div>';'</div>'

You can simplify it by using just one echo:

echo '
<div class="productrangeborder">
<div class="productdetailsborder"><a href="product.php?product_id=' . $query_row['product_id'] . '">' . $query_row['name'] . '</a></div>
<div class="productimageborder"><a href="product.php?product_id=' . $query_row['product_id'] . '"><img src="' . $query_row['image_link'] . '"/></a></div>
<div class="priceborder">' . $query_row['rrp'] . '</div>
<div class="discountborder">' . $query_row['discount'] . '</div></div>';

or insert php tags for the variables:

<div class="productrangeborder">
<div class="productdetailsborder"><a href="product.php?product_id=<? $query_row['product_id'] ?>"><? $query_row['name'] ?></a></div>
<div class="productimageborder"><a href="product.php?product_id=<? $query_row['product_id'] ?>"><img src="<? $query_row['image_link'] ?>"/></a></div>
<div class="priceborder"><? $query_row['rrp'] ?></div>
<div class="discountborder"><? $query_row['discount'] ?></div></div>

Fixed the second one for you.

Thanks :blush:

Thats brilliant, thank you ever so much.

You can also use curly brackets to embed arrays in strings. IE:


echo "<div class='discountborder'>{$query_row['discount']}</div></div>";

One of the other methods would probably be better for your situation. I was just illustrating a way to embed variables/arrays (in double quoted strings) without having to escape out of the string and concatenate. PHP can be weird about arrays embedded in stringsā€¦ so encapsulating them in curly brackets can help. I do this for most variables that are embedded.