this is my first post here. I spent all of yesterday searching the net for an answer, but I think I've got a fairly unusual application of the while loop here so I didn't have much success.
Essentially I have a several categories, for example titled "Kosove" and "Shqiperi". Each of these has a number of sub-sections. Those sub-sections may or may not have content inside them.
I'm using while loops to display the sub-sections, providing they DO have content inside.
This is my code:
<ul>
<?php while(osc_has_list_regions($country = 'kv') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
Notice the " $country = 'kv' " identifies the category as 'Kosove' in this case.
What is generated is:
Prishtine(1)
Vushtrri(1)
Which is fine.
The problem occurs when I want to also display the subsections for another category, e.g. Shqiperi. The code for this:
<ul>
<?php while(osc_has_list_regions($country = 'kv') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
<br><br>
<ul>
<?php while(osc_has_list_regions($country = 'al') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
Again notice the " $country = 'al' " identifies the category as 'Shqiperi' in this case.
What is generated is:
Prishtine(1)
Vushtrri(1)
Prishtine(1)
Vushtrri(1)
Which is not correct. The second part of the code has ignored the 'al' and just repeated the previous while loop content (i.e. the subsections for 'kv').
It SHOULD read:
Prishtine(1)
Vushtrri(1)
Berat(1)
As 'Berat' is the appropriate subsection for the 'Shqiperi' category.
If I have " $country = 'al' " as the first while loop condition, then it shows:
Berat(1)
Berat(1)
So obviously the order is important. It seems that the previous loop hasn't been properly closed.
I'm pretty new to PHP and I'm not even sure if I'm using the while loop appropriately here. Any suggestions how to fix this would be hugely appreciated.
Related
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.
No PHP skills yet, I've just learned HTML/CSS and think I'm already good at it, but still not in PHP. I am developing my own OpenCart Store and also making it as my practice environment for html/css.
I've installed a paid module to make a product option as custom product field. I've already set it up but I am trying now to modify it to fit my needs.
Currently the custom module has this code (exerpt);
<?php foreach ($field['values'] as $value) { ?>
<?php if ($value['selected'] && !$selected) { ?>
<?php $selected = true; ?>
<span><?php echo $field['name']; ?></span>
<?php } ?>
<?php if ($value['selected'] == $value['option_value_id']) { ?>
<?php $checkbox_value .= $value['name'] . ', '; ?>
<?php } ?>
<?php } ?>
The above code displays a custom field above the PRODUCT CODE field of the default theme, ie
Tech Specs: Android Phone, Dual Core, 4.3 Inches Screen
the comma is outputted by the code ', '; in the code line <?php $checkbox_value .= $value['name'] . ', '; ?>
I want to make it as un-ordered list and these code below is what I've got so far, base on trial and error only.
<?php foreach ($field['values'] as $value) { ?>
<?php if ($value['selected'] && !$selected) { ?>
<?php $selected = true; ?>
<span><?php echo $field['name']; ?></span>
<?php } ?>
<?php if ($value['selected'] == $value['option_value_id']) { ?><ul>
<?php $checkbox_value .= $value['name'] . '</li><li>'; ?></li></ul>
<?php } ?>
<?php } ?>
The code above displays the output like so;
Tech Specs:
Android Phone
Dual Core
4.3 Inches Screen
But I like to output it like this
Tech Specs:
Android Phone
Dual Core
4.3 Inches Screen
UPDATE:
Need help to understand if, else and foreach
I am trying to modify the presentation. Each value is separated by comma (Android Phone, Dual Core, 4.3 Inches Screen) and the Option Name is Tech spec. I like to present the value as UL list. I almost done it, as shown above. Only thing is, Android Phone was not included.
My code looks like this when outputted;
UPDATE:
Output for dljve code...
Your <ul> opening tag is not followed by <li>, thus displaying the first option not as a list element. You need to add <li> behind the <ul>.
I would also recommend cleaning up the code by echoing the HTML and reducing the <?php and ?> tags to a minimum.
EDIT:
Ok, so what I think is happening (although I can't see the rest of the software's source) is that $checkbox_value is printed after this whole code, so the first and last <ul> and <li> tags would actually fall outside the list, which would produce the same error as you're seeing.
Could you try this code? This adds the <ul> </ul> tags to the $checkbox_value and adds the products surrounded in <li> </li> tags between them.
<?php
$checkbox_value .= "<ul>";
foreach ($field['values'] as $value)
{
if ($value['selected'] && !$selected)
{
$selected = true;
echo "<span>" . $field['name'] . "</span>";
}
if ($value['selected'] == $value['option_value_id'])
{
$checkbox_value .= "<li>" . $value['name'] . "</li>";
}
}
$checkbox_value .= "</ul>";
?>
Sorry if this seems very simple I'm rather new to php.
I'm drawing data from the database using a for each loop and want to add a list item for each result to a side bar while adding a new container div to the body.
What is the best way to code this?
Any help is much appreciated.
Thanks A
Edit...
My code so far...
<?php
$iidcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_iid_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
$gadcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_gad_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
?>
<ul>
<?php
foreach ( $iidcoversheets as $iidcoversheet )
{
?>
<li><?php echo $iidcoversheet->business name; ?></li>
<?php
}
?>
<?php
foreach ( $gadcoversheets as $gadcoversheet )
{
?>
<li><?php echo $gadcoversheet->business name; ?></li>
<?php
}
?>
</ul>
I understand how to add the <li> items but I'm strugging on how to add a div containing some html to another div not in the <ul> tag as it seems to me this will interrupt the code for the <ul>.
Any help would be really appreciated.
Thanks A
<div>
<ol>
<?php
foreach ($uls as $ul) {
?>
<li>
<?php echo $ul; ?>
</li>
<?php
}
?>
</ol>
</div>
Ok I came to the realisation that I could simply do another exact same loop later on in the page in the div I wanted but with different output. I was trying to do it all up front save the results and output them somewhere else, unnecessary.
Clearly I was having a daft moment earlier.
Thanks for all the input.
Regards
A
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; ?>
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?