Create a dynamic XML href in PHP with SimpleXML - php

I'm trying to create a link based on a URL variable using only the last five digits of its respective line of XML data.
For example, if the XML link is http://events.stanford.edu/events/213/21389 how can I create this link a href="e/?i=21389?
Here's my page, XML and code:
<?php
// Build the XML file path, using URL variable $c (above)
$c = $_GET['c'];
$p ="http://events-prod.stanford.edu/xml/byCategory/";
$e = "/mobile.xml";
$file = "$p$c$e";
$xml = simplexml_load_file($file);
?>
<h1><?php echo $xml->title; ?></h1>
Home
</div><!-- /header -->
<div data-role="content">
<?php // Only display if there are events ?>
<?php if (isset($xml->Event->title)) { ?>
<ul data-role="listview">
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->locationText; ?></p>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There is currently nothing scheduled for ', $xml->title, '.</p>';?>
<?php } ?>

I'm using short tags, because I think you'll agree it's easier now to read the code as oppose to before.
<?
$controller ="http://events-prod.stanford.edu/xml/byCategory/";
$category_id = $_GET['c'];
$category_id = 0;
$xml = "/mobile.xml";
$url = $controller . $category_id . $xml;
$xml_object = simplexml_load_file($url);
?>
<div>
<h1><?= $xml_object->title ?></h1>
Home
</div>
<div data-role="content">
<? if (!empty($xml_object->Event->title)): ?>
<ul data-role="listview">
<? foreach($xml_object->Event as $event): ?>
<li>
<?
$pattern = '/[0-9]+$/';
$matches = array();
preg_match($pattern, $event->link, $matches);
$my_link = 'e/?i=' . $matches[0];
?>
<a href="<?= $my_link ?>">
<? if (!empty($event->Media->url)): ?>
<img src="<?= $event->Media->url ?>" alt="<?= $event->title ?> thumbnail" />
<? endif; ?>
<h3><?= $event->title ?></h3>
<p><strong><?= $event->beginDate ?> at <?= $event->beginTime ?></strong></p>
<p><?= $event->locationText ?></p>
</a>
</li>
<? endforeach; ?>
</ul>
<? else: ?>
<? echo '<p>There is currently nothing scheduled for ', $xml_object->title, '.</p>'; ?>
<? endif; ?>
</div>

Related

foreach PHP something wrong

I have this code in PHP
<?php foreach($this->group('filter') as $i => $fields): ?>
<?php $newField = $this->field('name#'.$i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter=<?php foreach($finalvar as $fletter) : ?>".filter_<?php echo standardize($fletter); ?>">
<?php endforeach; ?>
<?php if($this->field('icon#'.$i)->value()): ?><i class="<?php echo $this->field('icon#'.$i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if($this->field('label_items#'.$i)->value()): ?><?php echo $this->field('label_items#'.$i)->value(); ?><?php else: ?><?php echo $this->field('name#'.$i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>
PHP code works but I have a problem on the HTML code. The generated HTML code is this:
<a data-filter=".filter_country">
".filter_capital;
<span class="name">Australia</span>
</a>
My goal is to implement this one:
<a data-filter=".filter_country,.filter_capital"
<span class="name">Australia</span>
</a>
I know it's something in front of me but I cannot see it. Does anyone have an idea concerning the syntax?
Check your syntax:
<?php foreach ($this->group('filter') as $i => $fields) : ?>
<?php $newField = $this->field('name#' . $i)->value(); ?>
<?php $finalvar = explode(",", $newField); ?>
<a data-filter="
<?php
$i = 1;
foreach ($finalvar as $fletter) { ?>
.filter_<?php echo standardize($fletter); ?>
<?php if ($i < count($fletter)) { ?>, <?php } ?>
<?php $i++; } ?>
">
<?php if ($this->field('icon#' . $i)->value()) : ?><i class="<?php echo $this->field('icon#' . $i)->value(); ?>"></i><?php endif; ?>
<span class="name"><?php if ($this->field('label_items#' . $i)->value()) : ?><?php echo $this->field('label_items#' . $i)->value(); ?><?php else : ?><?php echo $this->field('name#' . $i)->value(); ?><?php endif; ?>
</span>
</a>
<?php endforeach; ?>

Page goes blank after adding to PHP foreach

Hello after I add some code to this PHP foreach the page goes blank and all I can see is a round black dot at the top left of screen.
My website is in Joomla3 and I am trying to customize a module. my site is http://get2gethersports.com
I have a recent post module that only shows the articles title.
that code is posted below
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
I would like to add an image abocve like the blog feed on http://vape-co.com
So I navigated to the component and saw the call for the image. which is posted below:
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
<?php } ?>
but whenever i add it or a snippet of it to the previous code it breaks....
Any ideas why it is breaking the page and how to fix it?
I tried adding in new li tags. Just adding the PHP part above the a link etc...
CODE UPDATE----
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params->get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { var_dump($item);?>
<li>
<div class="rsblog-entry-content">
<?php if ($this->item->image) { ?>
<div class="rsblog-entry-image">
<img class="rsblog-entry-thumb img-polaroid" src="<?php echo JURI::root().'components/com_rsblog/assets/images/blog/'.$this->item->image; ?>?nocache=<?php echo uniqid(''); ?>" alt="<?php echo $this->escape($this->item->title); ?>">
</div>
</div>
<?php } ?>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Try $item->image instead of $this->item->image
Correct code. Looks like it just needed some reduction.
<?php if ($items) { ?>
<ul class="rsblog-recent-module unstyled<?php echo $params -> get('moduleclass_sfx',''); ?>">
<?php foreach ($items as $item) { ?>
<li>
<?php if($item->image != '') ?>
<img src="components/com_rsblog/assets/images/blog/<?php echo $item->image;?>" alt="<?php echo $item->title. "logo";?>" width="100px"/>
<br/>
<a <?php echo $opener; ?> href="<?php echo JRoute::_('index.php?option=com_rsblog&view=post&id='.RSBlogHelper::sef($item->id,$item->alias).$Itemid,false); ?>">
<?php echo $item->title; ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>

Multilingual slideshow

I have this code for a slideshow in my Drupal 7, Nexus 7.x-1.3 theme.
My website is multilingual and I need to use a different image for every language, eg. I would like to change slide-image-1.jpg (en language) with other image-name for (gr language).
My code:
<?php if ($is_front): ?>
<?php if (theme_get_setting('slideshow_display','nexus')): ?>
<?php
$slide1_head = check_plain(theme_get_setting('slide1_head','nexus')); $slide1_desc = check_markup(theme_get_setting('slide1_desc','nexus'), 'full_html'); $slide1_url = check_plain(theme_get_setting('slide1_url','nexus'));
$slide2_head = check_plain(theme_get_setting('slide2_head','nexus')); $slide2_desc = check_markup(theme_get_setting('slide2_desc','nexus'), 'full_html'); $slide2_url = check_plain(theme_get_setting('slide2_url','nexus'));
$slide3_head = check_plain(theme_get_setting('slide3_head','nexus')); $slide3_desc = check_markup(theme_get_setting('slide3_desc','nexus'), 'full_html'); $slide3_url = check_plain(theme_get_setting('slide3_url','nexus'));
?>
<div id="slidebox" class="flexslider">
<ul class="slides">
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-1.jpg'; ?>"/>
<?php if($slide1_head || $slide1_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide1_head; ?></h2><?php print $slide1_desc; ?>
<a class="frmore" href="<?php print url($slide1_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-2.jpg'; ?>"/>
<?php if($slide2_head || $slide2_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide2_head; ?></h2><?php print $slide2_desc; ?>
<a class="frmore" href="<?php print url($slide2_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
<li>
<img src="<?php print base_path() . drupal_get_path('theme', 'nexus') . '/images/slide-image-3.jpg'; ?>"/>
<?php if($slide3_head || $slide3_desc) : ?>
<div class="flex-caption">
<h2><?php print $slide3_head; ?></h2><?php print $slide3_desc; ?>
<a class="frmore" href="<?php print url($slide3_url); ?>"> <?php print t('CONTACT US'); ?> </a>
</div>
<?php endif; ?>
</li>
</ul><!-- /slides -->
<div class="doverlay"></div>
</div>
<?php endif; ?>
<?php endif; ?>
Is there any solution for that? Thank you.
There is a Drupal connector from Smartling that may be of help. Here is a video showing how the connector works.
Many sites running on Smartling do language/country-specific image replacement, which is explained here.
Hope this helps,
Nataly
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<SCRIPT>
function english(){
$(#image).attr('src','http://link.to.your/english/image.png')
}
function greek(){
$(#image).attr('src','http://link.to.your/greek/image.png')
}
</SCRIPT>
<IMG id="image" src="http://link.to.your/greek/image.png">
Change to English<BR>
Change to Greek
Finally I fixed it in page.tpl with php code. One if/else on your page language to set you picture path.
Regards, HashKey

Trying to get str_replace working in custom Magento code

Here is my code:
<?php
$_rtl = Mage::getStoreConfig('custom_menu/general/rtl') + 0;
$_categories = $this->getStoreCategories();
if (is_object($_categories)) $_categories = $this->getStoreCategories()->getNodes();
?>
<div class="nav-container">
<div style="z-index:999;position:relative;top:-8px;left:979px;cursor:pointer;"><img src="<?php echo $this->getSkinUrl('images/Truck-Prince-text.png'); ?>" height="91" width="123" style="position:absolute;" onclick="location.href='<?php echo Mage::getUrl('checkout/cart'); ?>'"></div>
<div id="custommenu" class="<?php echo $_rtl ? ' rtl' : ''; ?>">
<?php if ($this->showHomeLink()) : ?>
<div class="menu">
<div class="parentMenu menu0">
<a href="<?php echo $this->getUrl('') ?>">
<span><?php echo $this->__('Home'); ?></span>
</a>
</div>
</div>
<?php endif ?>
<?php foreach ($_categories as $_category): ?>
<?php echo $this->drawCustomMenuItem($_category) ?>
<?php endforeach ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('nav-links-after')->toHtml() ?>
<div class="clearBoth"></div>
</div>
</div>
What I want to do is something like this but I don't know how to insert this in without causing errors. I basically want to change the existing url with the string replaced urls..
<?php // We must replace URLs on the fly to use landing pages and not break subcat navigation
$_category = str_replace('wild-bird-feed-supplies.html', 'landing-wild-bird-feed-supplies', $_category);
$_category = str_replace('lawn-garden-supplies.html', 'landing-lawn-garden-supplies', $_category);
$_category = str_replace('pet-food-supplies.html', 'landing-pet-food-supplies', $_category);
$_category = str_replace('animal-feed-supplies.html', 'landing-animal-feed-supplies', $_category); ?>
I finally was able to get it :) Here is what I did...
<?php
$_rtl = Mage::getStoreConfig('custom_menu/general/rtl') + 0;
$_categories = $this->getStoreCategories();
if (is_object($_categories)) $_categories = $this->getStoreCategories()->getNodes();
?>
<div class="nav-container">
<div style="z-index:999;position:relative;top:-8px;left:979px;cursor:pointer;"><img src="<?php echo $this->getSkinUrl('images/Truck-Prince-text.png'); ?>" height="91" width="123" style="position:absolute;" onclick="location.href='<?php echo Mage::getUrl('checkout/cart'); ?>'"></div>
<div id="custommenu" class="<?php echo $_rtl ? ' rtl' : ''; ?>">
<?php if ($this->showHomeLink()) : ?>
<div class="menu">
<div class="parentMenu menu0">
<a href="<?php echo $this->getUrl('') ?>">
<span><?php echo $this->__('Home'); ?></span>
</a>
</div>
</div>
<?php endif ?>
<?php foreach ($_categories as $_category): ?>
<?php $_category = $this->drawCustomMenuItem($_category) ?>
<?php // We must replace URLs on the fly to use landing pages and not break subcat navigation
$_category = str_replace('wild-bird-feed-supplies.html', 'landing-wild-bird-feed-supplies', $_category);
$_category = str_replace('lawn-garden-supplies.html', 'landing-lawn-garden-supplies', $_category);
$_category = str_replace('pet-food-supplies.html', 'landing-pet-food-supplies', $_category);
$_category = str_replace('animal-feed-supplies.html', 'landing-animal-feed-supplies', $_category); ?>
<?php echo $_category; ?>
<?php endforeach ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('nav-links-after')->toHtml() ?>
<div class="clearBoth"></div>
</div>
</div>

Pagination in Zend not working

I have an array of items. Items are files paths. I have set up pagination but when I click on next it does not change. I want to display 1 pdf per page. Below is my code
class IndexController extends Zend_Controller_Action
{
public function init(){}
public function indexAction()
{
if($_SERVER['SERVER_NAME']=="portal-dev"){
$location = "/data/scan/invoice";
$listOfFiles = $this->readFiles($location);
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$this->view->paginator = $paginator;
}
}
public function readFiles($location){
$pattern="(\.pdf$)"; //valid image extensions
$thelist = array();
if ($handle = opendir($location)) {
while (false !== ($file = readdir($handle)))
{
if (eregi($pattern, $file)) //if this file is a valid image
{
$thelist[] = $file; //insert files into array
$max = count($thelist) - 1;
$max1 = count($thelist);
}
}
closedir($handle);
}
return $thelist;
}
}
index.phtml
<div id="main">
<?php if(!$this->paginator){
echo "No Files to process";
}else{
?>
<table>
<tr>
<td rowspan=2></td>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<li><?php echo $item; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php echo $this->paginationControl($this->paginator,'Sliding','partial/my_pagination_control.phtml'); ?>
<?php }?>
</div>
pagination is found in directory partial
<?php if ($this->pageCount): ?>
<div
class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
Previous </a> <span class="bar"> | </span>
<?php else: ?>
<span class="disabled"> Previous</span> <span class="bar"> | </span>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>"> <?php echo $page; ?>
</a> <span class="bar"> | </span>
<?php else: ?>
<?php echo $page; ?>
<span class="bar"> | </span>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>"> Next
</a>
<?php else: ?>
<span class="disabled">Next </span>
<?php endif; ?>
</div>
<?php endif; ?>
Try after setting the current page number in indexAction
$paginator->setCurrentPageNumber($this->_getParam('page'));
Like this
$paginator = Zend_Paginator::factory($listOfFiles);
$paginator->setItemCountPerPage(1);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$this->view->paginator = $paginator;

Categories