I want to show number of row : paginator zend framework - zend-framework

I want to show number of row : paginator zend framework
Example:
number productname price
1. pen 25.00
2. Pencil 10.00
3. Booklet 12.00
i want show number of row the exaple (number row). (get from paginator ok)

At the moment I can think of three ways you can get current item number from/using paginator:
using foreach's $key => $item. In this case $key should be your item number.
using normalizeItemNumber method of Zend_Paginator
using partialView helper
All the three methods are illustrated in the following example:
testAction
public function testAction() {
$input = array(
array(
'productname' => 'somename',
'price' => 23
),
array(
'productname' => 'somename2',
'price' => 657
)
);
$paginator = Zend_Paginator::factory($input);
$paginator->setCurrentPageNumber(1);
$this->view->paginator = $paginator;
}
test.phtml view script
<div>
<?php foreach ($this->paginator as $key => $item): ?>
Item Number (Method 1): <?php echo $key; ?> </br>
Item Number (Method 2): <?php echo $this->paginator->normalizeItemNumber($item); ?> </br>
<?php endforeach; ?>
Third Method through partialLoop view helper:
<?php echo $this->partialLoop('_partials/testPartial.phtml', $this->paginator); ?>
</div>
Where testPartial.phtml is as follows:
<div>
Item Number (Method 3): <?php echo $this->partialCounter; ?> </br>
<!-- Other values can be accessed as $this->productname -->
</div>
Hope this is what you are looking for in your case.

I am not sure what you want exactly, but give this a try
$pages = $paginator->getPages();
var_dump($pages);
echo $pages->pageCount;
getPages() has some useful information in which you can use about the paginator object. It will output something like:
object(stdClass)[201]
public 'pageCount' => int 4
public 'itemCountPerPage' => int 10
public 'first' => int 1
public 'current' => int 1
public 'last' => int 4
public 'next' => int 2
public 'pagesInRange' =>
array
1 => int 1
2 => int 2
3 => int 3
4 => int 4
public 'firstPageInRange' => int 1
public 'lastPageInRange' => int 4
public 'currentItemCount' => int 10
public 'totalItemCount' => int 33
public 'firstItemNumber' => int 1
public 'lastItemNumber' => int 10
Failing that you might need to do something inside the loop while iterating over the object. For example in your view:
$row = 0;
foreach($this->paginator as $rowNumber => $data)
{
echo "I am row " . $rowNumber . "<br />";
}
Hope that helps.

If the number in front of the row is not related to the id of the item, you could simply increment a counter through each iteration of your data display.
$i = 1;
echo '<ul>';
foreach ($this->paginator as $item){
echo '<li>' . $i . ' | ' . $item['name'] . ' | ' . $item['price'] . '</li>';
$i++;
}
echo '</ul>';
The very same counter could be used to make a Zebra table (alternating background colors) with the use of modulo.
$i = 1;
echo '<ul>';
foreach ($this->paginator as $item){
if($i % 2 == 0){
$class = 'even';
}else{
$class = 'odd';
}
echo '<li class = "' . $class . '">' . $i . ' | ' . $item['name'] . ' | ' . $item['price'] . '</li>';
$i++;
}
echo '</ul>';

Related

Drop down does not show the option in correct format selected in codeigniter

<datalist id="stoplist">
Array
(
[0] => Array
(
[stops] => katraj dairy
)
[1] => Array
(
[stops] => bharati vidyapith
)
[2] => Array
(
[stops] => balaji nagar
)
[4] => Array
(
[stops] => k k market
)
)
</datalist>
This is my view
foreach ($data1 as $row) {
echo "<option value=".$row['stops'].">".$row['stops']."</option>";
}
When I select katraj dairy I get only katraj as a value.
I am getting correct value from database as I print above. What is the problem?
From your controller you need to pass data from your controller before you can access it from your view.
&this->load->view("viewname.php",$data)
Where $data is the array you get from your database
format for select option
<select>
<option value="">name</option>
</select>
Modified code:
echo "<select>";
foreach ($data1 as $row) {
echo "<option value=".$row['stops'].">".$row['stops']."</option>";
}
echo "</select>";

How to get List for Contact Form 7

I have 2 contact forms created by Contact Form 7.
How to list all contact forms created through shortcode?
Please check images, tks.
Updated:
this is my code, this working!
$args = array('post_type' => 'wpcf7_contact_form', 'posts_per_page' => -1);
$rs = array();
if( $data = get_posts($args)){
foreach($data as $key){
$rs[$key->ID] = $key->post_title;
}
}else{
$rs['0'] = esc_html__('No Contact Form found', 'text-domanin');
}
Below is a dropdown list:
<select name="field-name" id="field-id">
<option value="">--Select--</option><?php
$dbValue = get_option('field-name'); //example!
$posts = get_posts(array(
'post_type' => 'wpcf7_contact_form',
'numberposts' => -1
));
foreach ( $posts as $p ) {
echo '<option value="'.$p->ID.'"'.selected($p->ID,$dbValue,false).'>'.$p->post_title.' ('.$p->ID.')</option>';
} ?>
</select>

My rows are wonky!

I am building a theme template page that calls up all the child categories of a particular parent and displays them so that they look like this:
The only trouble is that when any row has less than 3 posts, it looks like this:
I need to display 1-3 posts and move on to the next category if there are either more OR less than 3 posts, and not have it look like crap.
$taxonomy = 'category'; // e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 13
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'candidate',
'post_status' => 'publish',
'showposts' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<div class="val-postcontent val-post"><h4 >' . $term->name. ' Candidates</h4></div> ';
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part('content', 'overview');
endwhile;
echo '<div class="test"><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>Click here to view all ' . $term->name. ' Candidates</a></div>';
}
}
}
I too belive this is a CSS issue, so it probably belongs on StackExchange instead, but here it goes:
The easiest, quickest (and probably ugliest) fix is to add <div style="display:block; clear: both;"> before each <div class="val-postcontent val-post">.
A quick way to make this a little bit cleaner: move the display:block; clear: both; into a separate css class, for example .clearfix {...}.
edit: I was to quick on the trigger. The div should be added before the div

PHP- PDO get metadata from database

I want to get the metadata from a database with a table 'friends'
id name
1 Herbert
2 LG
3 Levins
Here is the code I was trying to get the data.
<?php
$dsn = 'mysql:host=localhost;dbname=postgre';
$username = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$db = new PDO($dsn, $username, $password, $options);
$stmt = $db->query("SELECT * FROM friends");
$cnt_columns = $stmt->columnCount();
for($i = 0; $i < $cnt_columns; $i++) {
$metadata = $stmt->getColumnMeta($i);
var_dump($metadata);
}
?>
When I execute the code:, it displays
array
'native_type' => string 'LONG' (length=4)
'pdo_type' => int 2
'flags' =>
array
empty
'table' => string 'friends' (length=7)
'name' => string 'id' (length=2)
'len' => int 11
'precision' => int 0
array
'native_type' => string 'VAR_STRING' (length=10)
'pdo_type' => int 2
'flags' =>
array
empty
'table' => string 'friends' (length=7)
'name' => string 'name' (length=4)
'len' => int 60
'precision' => int 0
Till here, it is giving the correct count of rows, but I need the result to display as it looks in the my database like
Output:
id name
1 Hebert
2 LG
3 Levins
How could I get all my fields as it is like a table in my database using metadata.
Use columnCount.
$stmt = $db->query("SELECT * FROM friends");
$cnt_columns = $stmt->columnCount();
for($i = 0; $i < $cnt_columns; $i++) {
$metadata = $stmt->getColumnMeta($i);
var_dump($metadata);
}
By the way, getColumnMeta is experimental. Not recommended to use. Why do you want to use it?
For the desired output, you don't need metadata. Just loop through the results:
$sql = "SELECT * FROM friends";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
// field names
if(count($result) > 0) {
foreach($result[0] as $k => $v) {
if(!is_int($k)) {
echo $k . "\t";
}
}
}
echo PHP_EOL;
// data
foreach ($result as $row) {
foreach($row as $k => $v) {
if(!is_int($k)) {
echo $row[$k] . "\t";
}
}
echo PHP_EOL;
}

zend-form select optgroup, how to specify id

Hello i am using Zend Framework Form and have tried to get this example to work http://framework.zend.com/issues/browse/ZF-8252, but it fails xD
this is my code
$options = Array
(
[] => Qualsiasi Agente
[agenti_attivi] => Array
(
[4] => Giovanni Abc
[10] => Luigi Abc
[13] => Michela Abc
)
);
$agenti->addMultiOptions($options);
and the generated code is :
<select name="agente_id" id="agente_id" tabindex="6">
<option value="" label="Qualsiasi Agente" selected="selected">Qualsiasi Agente</option>
<optgroup id="agente_id-optgroup-Agenti attivi: " label="Agenti attivi: ">
<option value="4" label="Giovanni Abc">Giovanni Abc</option>
<option value="10" label="Luigi Capoarea">Luigi Abc</option>
<option value="13" label="Michela Abc">Michela Abc</option>
</optgroup>
</select>
where id="agente_id-optgroup-Agenti attivi: " is not xhtml valid Line 724, Column 44: value of attribute "id" must be a single token
i am using zend 1.11.10
thanks
Create a custom view helper FormSelect that extends the core FormSelect and then modify the code.
Include the path to your view helpers in the bootstrap file
protected function _initHelpers()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
The custom view helper. It's a copy of Zend_View_Helper_FormSelect but with small modification.
class My_View_Helper_FormSelect extends Zend_View_Helper_FormSelect
{
public function formSelect($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />\n")
{
$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
extract($info); // name, id, value, attribs, options, listsep, disable
// force $value to array so we can compare multiple values to multiple
// options; also ensure it's a string for comparison purposes.
$value = array_map('strval', (array) $value);
// check if element may have multiple values
$multiple = '';
if (substr($name, -2) == '[]') {
// multiple implied by the name
$multiple = ' multiple="multiple"';
}
if (isset($attribs['multiple'])) {
// Attribute set
if ($attribs['multiple']) {
// True attribute; set multiple attribute
$multiple = ' multiple="multiple"';
// Make sure name indicates multiple values are allowed
if (!empty($multiple) && (substr($name, -2) != '[]')) {
$name .= '[]';
}
} else {
// False attribute; ensure attribute not set
$multiple = '';
}
unset($attribs['multiple']);
}
// now start building the XHTML.
$disabled = '';
if (true === $disable) {
$disabled = ' disabled="disabled"';
}
// Build the surrounding select element first.
$xhtml = '<select'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $multiple
. $disabled
. $this->_htmlAttribs($attribs)
. ">\n ";
// build the list of options
$list = array();
$translator = $this->getTranslator();
foreach ((array) $options as $opt_value => $opt_label) {
if (is_array($opt_label)) {
$opt_disable = '';
if (is_array($disable) && in_array($opt_value, $disable)) {
$opt_disable = ' disabled="disabled"';
}
if (null !== $translator) {
$opt_value = $translator->translate($opt_value);
}
$opt_id = ' id="' . $this->formatElementId($id . '-optgroup-' . $opt_value) . '"';
$list[] = '<optgroup'
. $opt_disable
. $opt_id
. ' label="' . $this->view->escape($opt_value) .'">';
foreach ($opt_label as $val => $lab) {
$list[] = $this->_build($val, $lab, $value, $disable);
}
$list[] = '</optgroup>';
} else {
$list[] = $this->_build($opt_value, $opt_label, $value, $disable);
}
}
// add the options to the xhtml and close the select
$xhtml .= implode("\n ", $list) . "\n</select>";
return $xhtml;
}
private function formatElementId($id)
{
// in here put whatever filter you want for the id value
$id = trim(strtr($id, array('[' => '-', ']' => '', ' ' => '', ':' => '')), '-');
$id = strtolower($id);
return $id;
}
}
Done. Create multi select element with a valid id.
<?php
$this->addElement('multiSelect', 'agente_id', array(
'label' => 'Label Name:',
'multiOptions' => array(
'' => 'Qualsiasi Agente',
'Agenti attivi: ' => array(
4 => 'Giovanni Verdi',
10 => 'Luigi Capoarea',
13 => 'Michela Passarin',
)
)
));
try this, it's works for me:
$select = new Zend_Form_Element_Select('select');
$options = Array(
'' => 'Qualsiasi Agente',
'agenti_attivi' => Array(
4 => 'Giovanni Verdi',
10 => 'Luigi Capoarea',
13 => 'Michela Passarin'
)
);
$this->addElements(array($xxxx,$select,$yyyy)); // $this : the form instance
and the result is:
<select id="select" name="select">
<option label="Qualsiasi Agente" value="">Qualsiasi Agente</option>
<optgroup label="agenti_attivi">
<option label="Giovanni Verdi" value="4">Giovanni Verdi</option>
<option label="Luigi Capoarea" value="10">Luigi Capoarea</option>
<option label="Michela Passarin" value="13">Michela Passarin</option>
</optgroup>
</select>
the problem is that the id attribute does not accept spaces and special special characters:
id="agente_id-optgroup-Agenti attivi: "
Zend is usually pretty good about rendering the proper html, given a doctype.
Try setting your doctype like this if you aren't already.
<?php
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->initView();
$viewRenderer->view->doctype('XHTML1_STRICT');
AND
<?php echo $this->doctype(); ?>
at the top of your layout
I don't have a install of ZF i can mess with easy, if this doesn't work ill setup a test environment.