SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    $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?

    Code:
    '<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>'
    Last edited by guido2004; Jun 1, 2012 at 02:21. Reason: added line brakes to code

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can simplify it by using just one echo:
    PHP Code:
    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:
    PHP Code:
    <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>

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    57 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tom8 View Post
    You can simplify it by using just one echo:
    PHP Code:
    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:
    PHP Code:
    <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.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thats brilliant, thank you ever so much.

  6. #6
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You can also use curly brackets to embed arrays in strings. IE:
    PHP Code:
    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.


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
  •