I need a code that pulls out the listings count for a product in order to have them in the navigation. I am trying to get the product and listing number in the navigation (if there is any), but I don't want to display something if the listings count is equals to 0.
Here follows my code:
<?php if($listingsCount = getListingsCount(0,0,2,2,1,86) > 0): ?>
Bakery
<?php echo $listingsCount['totalRecords'] ?>.
<?php else: ?>
It should be
if ( ($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0)
In this case you the assignment is done first and is then compared to 0. So $listingCount contains the real count.
The way you did it, the comparison is done first and the return value (true/false) is assigned to $listingCount.
Nevertheless. If you never reach the else-part, maybe something in your method getListingsCount() is broken.
Try this:
<?php if (($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0) { ?>
Bakery
<?php echo $listingsCount['totalRecords']; ?>
<?php } else { ?>
Other text
<?php }?>
Thanks guys for your answers. managed to get it working with this...
<?php
$listingsCount = getListingsCount(0,0,2,2,1,86);
if($listingsCount['totalRecords'] > 0):
?>
<dd>
Bakery
[<?php echo $listingsCount['totalRecords'] ?>]
</dd>
<?php endif; ?>
Related
Enrol table:
From this table I try get value from active_shop
Here I have code:
<?php
$get_config_details = $this->db->get('enrol',$per_page, $this->uri->segment(1));
?>
<?php if ($get_config_details->num_rows() > 0):
foreach($get_config_details->result_array() as $get_config_detail):
$course_details = $this->crud_model->get_course_by_id($get_config_detail['course_id'])->row_array();?>
<?php echo $get_config_detail['active_shop']; ?>
<?php endforeach; ?>
<?php endif; ?>
Now I get the result: 1
But I copied this function from another function. I don't need the foreach function and if and so much code here. I need some simple code to squeeze this table and echo the result. Can anyone help me to correct this code?
I am trying to set a condition for php- mysql pagination so that it can change the current page "li" to "li class="active" " to mark the current selected page. I am new to php and searching for such pagination tutorial but no luck.
I have done so far what is working but not able to mark selected page. Here $id is for detecting the current page id. How can I set if condition ( or other) so that I can mark the current page item in the pagination? Thousands thanks for helping me.
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
<?php if($id!=$page)
//3!=4
{?>
<li>Next</li>
<?php }?>
</ul>
You could change your for loop from
<?php
for($i=1;$i <= $page;$i++){
?>
<?php
if ($id>1)
{ ?>
<li class="active"><?php echo $i;?></li>
<?php }
?>
<!-- <li><?php echo $i;?></li> -->
<?php
}
?>
to:
<?php
for($i=1;$i <= $page;$i++){
$class=($i==$id)? ' class="active"' : '';
echo '<li'.$class.'>'.$i.'</li>';
}
?>
If I've understood your code properly, $page represents total pages and $id represents the current page, this will set the current page number as the active class and leave the other pages without the class
Assuming that $id is the current page number, and that $page is the total number of pages, you need to highlight only one by doing the following in your loop:
if($i==$id) // highlight
else // don’t highlight
Your main errors are that you didn’t test whether $i==$id, and you didn’t have an alternative unhighlighted version.
I really think that you should simplify your code by separating your logic from the HTML. It becomes very unreadable otherwise, and very hard to manage.
I have taken the liberty of doing just that. This way you can see where the logic does the hard work, an you only need to print the results in the HTML.
<?php
$id=3; // test value
$page=20; // test value
$li=array(); // temporary array for convenience
$template='<li%s>%s</li>';
if($id>1) $li[]=sprintf($template,'',$id-1,'Previous');
for($i=1;$i<=$page;$i++) {
if($i==$id) $li[]=sprintf($template,' class="active"',$i,$i); // Current
else $li[]=sprintf($template,'',$i,$i);
}
if($id<$page) $li[]=sprintf($template,'',$id+1,'Next');
$li=implode('',$li); // convert to string for printing
?>
<ul class="pagination">
<?php print $li; ?>
</ul>
You will also see two techniques to make things easier:
I use an array as a temporary container. It is easier to add items this way and then to implode it into a string afterwards
sprintf makes it easier to define a template string, so you can manage the HTML component more easily, and fill in the gaps when the time comes.
I need a pagination for the output that is generated by a WordPress plugin. The plugin retrieves all products of a certain productgroup from the datebase. The basic code is:
<?php
foreach ((array)$this->view['data']['produkte'] as $p)
{ ?>
...some html code here
}
?>
where $p is an multi-dimensional array that contains the data for the single product.
I'd like to limit the output to, say, 10 products and create a simple pagination for that. The plugin is quite complex and uses nested templates, therefore a custom query is probably not an option in this case.
Would be great if somebody could point me in the right direction. Thank you!
Here's the final code. Thank you very much for your valuable help!!
<?php
$nb_elem_per_page = 3;
$page = isset($_GET['seite'])?intval($_GET['seite']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+2;
$page_no = $_REQUEST['seite'];
foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p)
{ ?> some HTML here... <?php } ?>
<?php if (count($data) > $nb_elem_per_page) { ?>
<ul id='paginator'>
<?php
for($i=1;$i<$number_of_pages;$i++){
if ($i == $page_no) {?>
<li><?php echo $i ?></li>
<?php }
else { ?>
<li><?php echo $i ?></li>
<?php }} ?>
</ul>
<?php { ?>
Since the default url contains already a query string, I had to modify the code a litte bit. I don't want the current site to have a link in the pagination. This gets me the number of the pagination query string:
$page_no = $_REQUEST['seite'];
I can then easily change the pagination links with a simple if-statement:
if ($i == $page_no) {...}?>
Thanks again!
You may have to tinker this around a bit but that will be something like that :
$nb_elem_per_page = 10;
$page = isset($_GET['page'])?intval($_GET['page']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+1;
<?php foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p) { ?>
...some html code here
<?php} ?>
<ul id='paginator'>
<?php
for($i=1;$i<$number_of_pages;$i++){?>
<li><a href='./?page=<?=$i?>'>$i</a></li>
<?php}?>
</ul>
You can use array_splice (twice) to get a new array with only the product you need.
Say you want to show 10 items per page and start at page 3:
<?php
$all_products = (array)$this->view['data']['produkte'];
$all_products = array_splice($all_products, 20); //select where to start
$all_products = array_splice($all_products, 0, count($all_products) - 10); //select how many products to show
foreach ($all_products as $p) {
//...some html code here
}
?>
(not tested)
Edit: #Loïc's answer is better as it only uses one array_splice.
How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>
I'm working on my own simple Content Management System to implement on my projects for clients who want to do their own maintenance.
I'm going to contain only page titles and their content in a database table. All I want is to make a list which takes all the page titles and puts them into a <ul>. I believe I should use foreach, but I'm not too good at PHP, so I'd appreciate some help.
So how would I go about this?
<?php
$qry = mysql_query("SELECT * FROM links");
?>
<ul>
<?php while($row = mysql_fetch_array($qry)) { ?>
<li><?php echo $row['link_title']; ?></li>
<?php } ?>
</ul>
...in case your table with links has the colums 'link_url' and 'link_title'. But you get the idea, I guess.
If you have retrieved an array with the table's rows, you can probably do something like this:
The PHP-in-HTML version:
<ul>
<?php foreach( $pages as $page ): ?>
<li><?=$page['title']?></li>
<?php endforeach ?>
</ul>
The HTML-in-PHP version:
echo "<ul>";
foreach( $pages as $page ) {
echo "<li>$page['title']</li>";
}
echo "</ul>";
In both versions $pages is an array containing all the rows from the table.
Maybe you can adapt that to your needs?