I’ve got an array where PLACEHOLDER is a placeholder for the variable $value that I get later in the code:
$names = array(
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://hank.com' title='PLACEHOLDER'>Hank</a>",
"<a href='http://marie.com' title='PLACEHOLDER'>Marie</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
);
To check how often an equal value is in my array,
I count them with array_count_values.
$count = array_count_values($names);
foreach ($count as $key => $value) {
echo $value . ' – ' . $key . '<br />';
}
So I get something like this:
3 – <a href='http:/walter.com' title='PLACEHOLDER'>Walter</a>
2 – <a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>
2 – <a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>
1 – <a href='http://hank.com' title='PLACEHOLDER'>Hank</a>
1 – <a href='http://marie.com' title='PLACEHOLDER'>Marie</a>
Now I’d PLACEHOLDER be replaced by $value, so I get the number as title tag of the link.
foreach ($count as $key => $value) {
echo $value . ' – ' . str_replace('PLACEHOLDER', $value, $key) . '<br />';
}
What are you asking, I don't understand, you want to replace PLACEHOLD with $value then do this
$names = array(
"<a href='http://skyler.com' title='".$PLACEHOLDER."'>Skyler</a>",
"<a href='http://jesse.com' title='".$PLACEHOLDER."'>Jesse</a>"
);
Use str_replace to replace PLACEHOLDER with $value
foreach ($count as $key => $value) {
$key = str_replace('PLACEHOLDER', $value, $key); //<--replace PLACEHOLDER here
echo $value . ' – ' . $key . '<br />';
}
Correct code is :
foreach ($count as $key => $value) {
echo str_replace('PLACEHOLDER', $value, $key); //this will replace placeholder with number of tags
echo '<br />';
}
Related
I want to render an array and print the output as Block[content]. There is either my Foreach or the way I use $item array or $row that is wrong. I will appreciate your help.
function produkt_block_view($delta = '')
{
$block = array();
$list = produkt_produkliste(1);
$items = array();
foreach ($list as $row) {
$items[1] = $row->uri;
$items[2] = $row->title;
$items[3] = $row->body_value;
}
switch($delta) {
case 'produkt' :
$block['content'] = '<a href="/19">';
$block['content'] .= '<img class="pimg" src="'.$row->uri.'" alt="Se alle vores filter elementer her">';
$block['content'] .= '<p class="pname">"'.$row->title.'"</p>';
$block['content'] .= '<p>"'.$row->body_value.'"</p>';
$block['content'] .= theme ('item_list',array('items' => $items, 'type' => 'ol', 'attribute' => 'produkt'));
break;
}
return $block;
}
I am trying to get some data from a table but when i use foreach it returns only 1 character or the 1st letter/number and one row is returned.
Here is my code.
home.class.php
public function getData()
{
$aRow = $this->database()->select('*')
->from('tablename')
->execute('getSlaveRow');
return $aRow;
}
ajax.class.php
public function getArr()
{
$data = 'No data found';
$results = Phpfox::getService('files.home')->getData();
if($results) {
$data = '<div id="fileparse" style="height:295px;overflow:auto;display:none;"</div>';
$data .= '<div id="filelist" style="height:295px;overflow:auto;">';
$data .= '<table style="width:100%;"><tr><td><b>File Name</b></td><td> <b>Account Type</b></td><td><b>Account Number</b></td><td><b>Company</b></td><td><b>Results</b></td></tr>';
foreach($results as $result) {
$data .= '<tr>';
$data .= '<td>'.$result['file'].'</td>';
$data .= '<td>'.$result['result'].'</td>';
$data .= '</tr>';
}
$data .= '</table>';
$data .= '</div>';
} else
$data = 'No results found';
$this->html('#eqblock', $data);
}
use getSlaveRows insted of getSlaveRow
getSlaveRow return single array while getSlaveRows return all rows
I'm having a hard time figure out how to paginate in cakePHP when i have activate admin routing.
I have localhost/myapp that is the main page
and localhost/myapp/admin that is tthe admin area. So I want to paginate on the fron end, so in the localhost/myapp index page.
So as the main index page is index of Typology Model.
in my app/Config/routes.php i have this routing for homepage:
Router::connect(
'/',
array(
'admin'=>false,
'controller' => 'typologies',
'action' => 'index', 'index'
)
);
PS: i Also Use slug for finding posts.
In my app/Controller/TypologyController I have this:
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'LEFT',
'conditions' => array('Item.id = Typology.item_id')
)
),
'conditions' => array(
'Item.published' => 1,
'Typology.published' => 1
),
'limit' => 1,
'order' => array('Typology.sort' => 'ASC'),
'fields' => array('Item.*', 'Typology.*'),
'recursive' => 2
);
$typologies = $this->paginate('Typology');
$this->set(compact('typologies'));
I have set limit=>1 just for testing purposes.
In my View/Typologies/index.ctp i have this:
<?php
$result = '';
foreach ($typologies as $typology):
$id = h($typology['Typology']['id']);
$item_id = h($typology['Typology']['item_id']);
$title = h($typology['Typology']['title']);
$description = h($typology['Typology']['description']);
$thumbnail = h($typology['Typology']['thumbnail']);
$price = h($typology['Typology']['price']);
$typology_category_id = h($typology['Typology']['typology_category_id']);
$typology_condition_id = h($typology['Typology']['typology_condition_id']);
if (strlen($description)>330) {
$short_description = substr($description, 0, 327);
$description = $short_description."...";
}
$dir = "img/uploads/typology/thumbnails/";
if (file_exists( $dir . $thumbnail)) {
$typologyThumbnails = $dir . $thumbnail;
}
else {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
if($thumbnail=='NULL' || $thumbnail=='') {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
$result .= "<div class=\"item_shadow\">";
if ($logged_in) {
if ($typology['Typology']['published']==0 || $typology['Typology']['published']==false || $typology['TypologyItem']['published']==0 || $typology['TypologyItem']['published']==false) {
$result .= "<div class=\"badge red\"> non publicato </div>";
}
}
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
$result .= "<div class=\"item-content\">";
$result .= "<div class=\"item-top-content\">";
$result .= "<div class=\"item-top-content-inner\">";
$result .= "<div class=\"item-top-title\">";
$result .= "<h4>{$title}</h4>";
$result .= "</div>";
$result .= "</div>" ;
$result .= "</div>";
$result .= "<div class=\"item-add-content\">";
$result .= "<div class=\"item-add-content-inner\">";
$result .= "<div class=\"description-inner\">";
$result .= "<p>{$description}</p>";
$result .= "</div>";
$result .= "<div class=\"read-more-inner\">";
$result .= $this->Html->link("maggiori informazioni". $this->Html->image('elenco.png'), array('admin'=>false,'controller' => 'items', 'action' => 'view', 'slug' => Inflector::slug($typology['TypologyItem']['seo_url'],'-'),'id'=>$typology['Typology']['item_id'],'?'=> array('active_tab' => $typology['Typology']['id'])), array('escape' => false));
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
endforeach;
$result .= "";
?>
So as you see first i store all the data into a variable then i echo it like this with the paginator helper:
<?php
if (isset($result)) {
echo $result;
}
?>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page{:page} di {:pages}, Showing {:current} records of {:count} in total, starting at {:start}, and finishing at {:end}')
));
?>
</p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('Preview'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('Next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
So the first time i load the page, URL is like this http://localhost/mycakephpApp/ it displays one date like it suppose to:
When i click next, or number 2, URL is like this: http://localhost/mycakephpApp/typologies/index/index/page:2, it should display the next one. It does but now it is blank like this.
I don't know what am i missing or what am i doing wrong. Does anyone has any idea how to fix this?
I would really appriciate your your help.
Relative urls are problematic
The view contains the following:
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
Where $typologyThumbnails is a relative url of the form:
img/uploads/typology/thumbnails/whatever.jpg
That means that for the url http://localhost/mycakephpApp/ the following image url is requested:
http://localhost/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
However, for the url http://localhost/mycakephpApp/typologies/index/index/page:2 the following image url is requested:
http://localhost/mycakephpApp/typologies/index/index/img/uploads/typology/thumbnails/whatever.jpg
Which doesn't exist.
There's a very simple solution:
Always use absolute urls i.e. /img/uploads/typology/thumbnails/whatever.jpg
Use helper functions and the router class, or at least recognise what they do.
You can fix the problem as asked by doing the following before using the variable:
$typologyThumbnails = Router::url('/' . $typologyThumbnails);
This will output:
/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
Use helpers and elements
View files of the form:
echo "large chunk of html"
are rather hard to read/maintain. Instead the view code should be for example:
<?php
foreach ($typologies as $typology) {
echo $this->element('typology', array('row' => $typology));
}
// pagination links
And the body of the foreach loop in an element. But don't write large swathes of html as strings - it's a lot easier to read/maintain if it's written as:
<?php
// /App/View/elements/typology.ctp
// variable setup
?>
<div class="item_shadow">
<div class="item" style="background-image:url(<?= Router::url($typologyThumbnails) ?>);">
...
<h4><?= h($row['Typology']['title']) ?></h4>
It also has the added benefit that your editor will highlight the html.
In wordpress I have made a custom post type. So for custom post type my code looks like this
add_action( 'init', 'broker_post_type' );
function broker_post_type() {
register_post_type( 'new_broker',
array(
'labels' => array(
'name' => __( 'Brands' ),
'singular_name' => __( 'Brand' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
)
);
}
To show the contents of custom posts through shortcode I have done like this
function display_broker_posts() {
$args = array(
'post_type' => 'new_broker',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
);
$dee_bios = new WP_Query( $args );
if( $dee_bios->have_posts() ):
$dee_output = '<div id="brokers-wrap">';
$dee_output .= '<div id="brokers-bg-wrap">';
$dee_output .= '<div class="brokers-content">';
$dee_output .= '<div class="brokers-left-content">';
while ( $dee_bios->have_posts() ) : $dee_bios->the_post();
if( $dee_bios->current_post == 0 || ( $dee_bios->current_post % 2 ) == 0 ) {
$dee_output .= '<a class="branding first" href=""><div class="one-half first">';
}
else {
$dee_output .= '<a href="" class="branding"><div class="one-half">';
}
$dee_output .= get_the_post_thumbnail( $dee_bios->post->ID,'small', 'bios', array( 'class' => 'alignleft' ) );
$dee_output .= '<span></span>';
// $dee_output .= '<p><strong>' . get_the_title() . ',</strong> ' . get_the_content() . '</p>';
$dee_output .= '</div><!--end .one-half-->';
endwhile;
$dee_output .= '<div class="clear"></div><!--clear all floats-->';
$dee_output .= '</div><!-- end #bios-->';
endif;
wp_reset_postdata();
return $dee_output;
}
add_shortcode( 'display_brokers', 'display_broker_posts' );
Here its working fine. I can see the featured image in my page by using shortcode. But lets say I have 20 custom posts and I want that the first 6 will show in a different div and another 14 in another div. So can someone kindly tell me how to do this? Any help will be really appreciable. Thanks
How about something like this:
$dee_bios = new WP_Query( $args );
$i = 0;
if( $dee_bios->have_posts() ):
while ( $dee_bios->have_posts() ) : $dee_bios->the_post();
if ($i ==0) {
$dee_output .= '<div id="first_broker_div">';
} elseif ($i == 6) {
$dee_output .= '</div><div id="second_broker_div">';
}
$dee_output .= //Your post content
$i++;
endwhile;
$dee_output .= '</div>';
endif;
Im using something similar to this code:
<?php foreach ($items as $item): ?>
<div><?php echo $item['Item']['content']; ?></div>
<?php endforeach; ?>
And i'd like to know which element is every item, because i want to add class "last-in-row" for every fourth item in row. How to make something like this code?
<?php for ($i=1; $i <= $items.count; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">$items[$i]</div>";
}; ?>
I haven't tested it but the following should work.
<?php
$i = 1;
foreach ($items as $item) {
$class = ($i % 4 == 0) ? '' : 'class="last-in-row"';
echo "<div $class>{$item['Item']['content']}</div>";
$i++;
}
?>
p.s. I hope you are sanitizing $item['Item']['content'].
What you are trying to do could be done with css3. This will mean you do not need to add a class which is fat better as later on you might want 3 or 5 in a row.
div:nth-child(4n+4) {
....
clear: both;
....
}
The CakePHP option without css3
foreach ($items as $i => $item) {
echo $this->Html->tag('div', $item['Item']['content'], array(
'class' => ($i + 1) % 4 === 0 ? 'last' : null
));
}
You nearly had it in your second example, just change $items.count to the value of count($items)
$itemsCount = count($items);
for ($i=1; $i <= $itemsCount ; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">{$items[$i]}</div>";
}