Implementing a html markup with PHP in OpenCart, How? - php

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>";
?>

Related

How does one echo a php list item within an ordered list?

I am using the repeater field of wordpress' advance custom fields. I have an ordered list. The list items are generated using the repeater field. This is how the static html looks:
<ol class="instructions">
<li>Hallways and transitional spaces are best...</li>
<li>It is best to keep expensive furnishings such...</li>
<li>If you want furnishings and accessories to be a bold source...</li>
<li>Neutral furnishings and accessories? You can afford...</li>
</ol>
The list items have styled numbers beside them.
I am struggling to put the ol together with php as I am very new to learning this language. This is what I have done so far.
<?php
// Check rows exists.
if(have_rows('rules')):
// Loop through rows.
while(have_rows('rules')) : the_row();
// Load sub field value.
$rule = get_sub_field('rule');
// Do something...
echo '<li>' . $rule . '</li>';
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
?>
How do I echo the php li(s) within the ol and give the ol a class of instructions within my php code?
Thank you for any answers. Very grateful <3
Say you have an array of rules;
$rules = [
"Hallways and transitional spaces are best...",
"It is best to keep expensive furnishings such...",
"If you want furnishings and accessories to be a bold source...",
"Neutral furnishings and accessories? You can afford...",
];
<ol class="instructions">
<?php
if(count($rules) > 0):
foreach($ruels as $rule) {
echo '<li>' . $rule . '</li>';
}
else
echo '<li> No Instruction Found </li>'
?>
</ol>
Just add echo '<ol class="instructions">'; before while cycle and echo '</ol>'; after.
<?php
// Check rows exists.
if(have_rows('rules')):
echo '<ol class="instructions">';
// Loop through rows.
while(have_rows('rules')) : the_row();
// Load sub field value.
$rule = get_sub_field('rule');
// Do something...
echo '<li>' . $rule . '</li>';
// End loop.
endwhile;
echo '</ol>';
// No value.
else :
// Do something...
endif;
?>

How to set condition in following code for marking current page in pagination?

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.

PHP separating multiple while loops in sequence

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.

php link code - separate the code

hi i need help with a small bit of code for my wordpress theme.
<a href="<?php echo $link; ?>">
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.', </li>';
}
?>
</a>
the code
<?php echo $link; ?>
this is for a link of a website that is already on the page the link part works great the only issue is i want the links (tags that are being used as keywords/anchor text)to be seo friendly for outbound links
what changes are needed? feel free to spice the link up for seo :)
If you're wanting the same link for each of the tags, then this is a solution:
<ul>
<?php
global $post;
foreach( get_the_tags($post->ID) as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
?>
</ul>
Please note: I've added the unordered list start and end elements as these were missing, these could also be ordered list start and ends.
EDIT:
to reflect your code changes.
The code still looks the same...I would assume it should look something like this: (and I could be completely wrong) :)
<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
echo ' <li>'.$tag->name.'</li>';
}
?>

PHP loop hanging/interspersed/threaded through HTML

I can't figure out how to say what I'm talking about which is a big part of why I'm stuck. In PHP I often see code like this
html
<?php
language construct that uses brackets {
some code;
?>
more html
<?php
some more code;
}
?>
rest of html
Is there any name for this? Having seen this lead me to try it out so here is a concrete example whose behavior doesn't make sense to me
<div id="content">
<ul id="nav">
<?php
$path = 'content';
$dir = dir($path);
while(false !== ($file = $dir->read())) {
if(preg_match('/.+\.txt/i', $file)) {
echo "<li class=\"$file\">$file</li>";
?>
</ul>
<?php
echo "<p class=\"$file\">" . file_get_contents($path . '/' . $file) . '</p>';
}
}
?>
</div>
Which outputs roughly <div><ul><li></li></ul><li></li><p></p>...</div> instead of <div><ul><li></li>...</ul><p></p>...</div>, which is what I thought would happen and what I want to happen. To make it clearer I want the <li> inside the <ul> and the <p> inside the <div>. Yes, I know there is an easy solution but I was wondering if there is a way to do it with the technique I am looking for a name for.
Just something to add here:
If you're using a PHP loop for templating, there is another syntax that helps you avoid confusion with indentation and which-braces-match-which:
<?php
foreach($items as $item):
?>
<b>item: </b> <?php echo $item; ?> <br />
<?php
endforeach;
?>
this may be an oversimplification, but really you shouldn't be using anything more complicated than this in a template. Things like the $items variable and anything else you need should be set up by the code which includes the template, and not in the template itself.

Categories