skip to Main Content

I would like to show the content of an xml file in a way that it shows the title, image, price and button with external link to the website.

Construction of the xml:

 <title>
  <![CDATA[ here comes the title ]]>
  </title>
  <description>
  <![CDATA[ here you can find the description of the product. ]]>
  </description>
  <link>https://www.linktoproduct.com</link>
  <g:product_type>
  <![CDATA[ brand1 &gt; brand2 &gt; brand3 &gt; brand ]]>
  </g:product_type>
  <g:image_link>https://www.linktoproduct.com/imageproduct.jpg</g:image_link>
  <g:price>
  <![CDATA[ 35.10 EUR ]]>
  </g:price>

This is the code I have so far:

  <style>
  .title{
   font-weight: bold;
   font-size: 24px;
   color: seagreen;
   }
  .description{
   font-weight: bold;
   font-size: 20px;
   color: #bbb;
   }
   .link{
     outline: none;
     border: none;
     color: #fff;
     font-weight: 600;
     font-size: 14px;
     background-color: red;
     border: 1px solid red;
     border-radius: 50px;
     padding: 10px 25px;
     line-height: 24px;
     margin-bottom: 9px;
     transition: 0.3s;
     cursor: pointer;
      }
      .col-md-4 {
      position: relative;
      min-height: 1px;
      padding-left: 15px;
      padding-right: 15px;
      }  
      @media (min-width: 992px) {
      .col-md-4 {
      width: 33.33333333%;
      float: left;
      }
  </style>

  <?php
  $xml_data =    simplexml_load_file("https://www.website.com/productfeed.xml") or 
  die("Error: Object Creation failure");

  foreach ($xml_data->children() as $data)
  {
 echo "<div class='col-md-4'>";
 echo "<div class='title'>";
 echo $data->title ."<br>";
 echo "</div>";
 echo "<div class='description'>";
 echo $data->description ."<br> ";
 echo "</div>";
 echo "<div class='link'>";
 echo $data->link ."<br> ";
 echo "</div>";
 echo "</div>";

  }

  ?>

but it gives me this issue on how to see a button with link to the website opening in a new tab. So not to have the link visible, but have it behind a button.

2

Answers


  1. Chosen as BEST ANSWER

    Here's the answer: replace

     echo $data->link ."<br> ";
    

    by

     echo "<a href='$data->link' target='_blank'>buy now</a>";
    

  2. If I understood correctly – you wish to have a button to take the user to the relevant URL rather than display the actual URL on the page?! If so then you could include an inline hyperlink that contains a simple button that displays some text or other.

    foreach( $xml_data->children() as $data ){
        printf('
            <div class="col-md-4">
                <div class="title">%s</div>
                <div class="description">%s</div>
                <div class="link">
                    <a href="%s" target="_blank">
                        Click me baby one more time
                    </a>
                </div>
            </div>',
            $data->title,
            $data->description,
            $data->link
        );
    }
    

    Example of how the HTML might look

    <div class="col-md-4">
      <div class="title">Banana</div>
      <div class="description">Curvy Yellow fruit</div>
      <div class="link">
        <a href="https://www.bananalink.org.uk/all-about-bananas/" target="_blank">
          Click me baby one more time
        </a>
      </div>
    </div>
    <div class="col-md-4">
      <div class="title">Apple</div>
      <div class="description">Spherical green fruits loved by teachers</div>
      <div class="link">
        <a href="https://cideruk.com/apple-growers/" target="_blank">
          Click me baby one more time
        </a>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search