i have a veriable named (num) which is used for increments changes names of ids by 1 num+1 but in my foreachloop i cant access it .
i tried declaring it before the loop still doesnot work
<?php $num = 0; ?>
<?php foreach($listings as $list):?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num+1;?>" name="treatment<?php echo $num+1;?>">
<label for="visit<?php echo $num+1;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
</ul>
<hr>
<input type="hidden" value="<?php echo $num;?>" name="total"/>
i want the input ids to be incremented by 1 like treatment1,treatment2
You should increment the $num variable by doing $num++; once inside the loop, then print it where you need it with <?php echo $num; ?> without using <?php echo $num+1; ?> - as doing so will only increment it as you echo it - not add one to each iteration.
<?php
$num = 0;
foreach($listings as $list):
$num++; // Increment $num for each iteration
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
If your $listings is numeric indexed, you can use the key of each element in the array instead by doing
foreach($listings as $num=>$list):
?>
<li>
<div class="checkbox">
<input type="checkbox" class="css-checkbox" value="<?php echo $list['title'];?>" id="visit<?php echo $num;?>" name="treatment<?php echo $num;?>">
<label for="visit<?php echo $num;?>" class="css-label"><?php echo $list['title']?> <strong><?php echo $list['price'];?><span>£</span></strong></label>
</div>
</li>
<?php endforeach; ?>
The problem is you did not set the value of $num variable you just only print or echo it inside the html tags. You need to add or increment the $num variable inside the loop like this.
<?php $num++; ?>
or
<?php $num = $num+1; ?>
Related
I know that this question has been asked before, but I am struggling with selecting a default checkbox. I want the checkbox to be "Kenyan Used" by default as in the picture below:Selected "Kenyan Used. What i have tried is $taxonomy= $wpdb->get_results("SELECT term_id FROM par_taxonomy WHERE (taxonomy = 'condition' AND term_taxonomy_id= '181')");. Though I don't how the below code implements this, I know it is a for loop, but how do i modify it to work for my case?. Below is the specific code from where the taxonomies are fetched from:
<?php
if (!empty($modern_filter)){ $counter = 0;
foreach ($modern_filter as $modern_filter_unit) {
$counter++;
$terms = get_terms(array($modern_filter_unit['slug']), $args);
if (empty($modern_filter_unit['slider']) && $modern_filter_unit['slug'] != 'price') { /*<!--If its not price-->*/
//if ($modern_filter_unit['slug'] != 'price') { /* != 'price'<!--If its not price-->*/
/*First one if ts not image goes on another view*/
if ($counter == 1 and empty($modern_filter_unit['use_on_car_modern_filter_view_images'])
and !$modern_filter_unit['use_on_car_modern_filter_view_images']) {
if (!empty($terms)) { ?>
<div class="stm-accordion-single-unit <?php echo esc_attr($modern_filter_unit['slug']); ?>">
<a class="title" data-toggle="collapse"
href="#<?php echo esc_attr($modern_filter_unit['slug']) ?>" aria-expanded="true">
<h5><?php esc_html_e($modern_filter_unit['single_name'], 'motors'); ?></h5>
<span class="minus"></span>
</a>
<div class="stm-accordion-content">
<div class="collapse in content" id="<?php echo esc_attr($modern_filter_unit['slug']); ?>">
<div class="stm-accordion-content-wrapper">
<?php foreach ($terms as $term): ?>
<?php if (!empty($_GET[$modern_filter_unit['slug']]) and $_GET[$modern_filter_unit['slug']] == $term->slug) { ?>
<script type="text/javascript">
jQuery(window).load(function () {
var $ = jQuery;
$('input[name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"]').click();
$.uniform.update();
});
</script>
<?php } ?>
<div class="stm-single-unit">
<label>
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
/>
<?php echo esc_attr($term->name); ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<?php } ?>
Thanks for the help in advance.
Assuming that this is the checkbox you want to do this for: Add an if statement to determine if the checkbox should be checked by default
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
<?php if($condition){?> checked <?php } ?>
/>
Replace $condition with something to determine if this is the checkbox you want checked by default. If you want all checkbox checked by default just add 'checked' inside the input tag without any if statements.
You need to add the checked attribute to your checkbox. You can check for the required term with an if statement. You need to add the following:
<?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?>
So then your input code looks like this:
<input type="checkbox"
name="<?php echo esc_attr($term->slug . '-' . $term->term_id); ?>"
data-name="<?php echo esc_attr($term->name); ?>"
<?php if ($term->name == 'Kenyan Used') { echo ' checked'; } ?> />
<?php echo esc_attr($term->name); ?>
Hope this helps!
I have no experience in php but i am facing a problem in php.
i have a php code like this:
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
<?php $count++;?>
</li>
<?php
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>
But i want to print "Total Test Case count" at beginning list. How i can do this?
Put a count($caseList) at the top.
<div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<li> <?php echo "Total Test Case COunt: " . count($caseList);?> </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>
You can try to get the length of the array without a counter using the count function.
$count = count($caseList);
So your final code could be like
div>
<ul>
<li><input type="checkbox" name="checkAll" id="selectAll" class="checkbox"> Select All </li>
<?php
$total = count($caseList);
echo "Total Test Case Count: $total"
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases" class="checkbox" value="<?php echo $testcase->id; ?>"><?php echo $testcase->name; ?><?php $count++;?></li>
<?php
}
?>
</ul>
you need to move increment from "li" tag
<div>
<ul>
<li><input type="checkbox" name="checkAll"
id="selectAll" class="checkbox"> Select All </li>
<?php
$count = 0;
foreach ($caseList as $testcase)
{
?>
<li><input type="checkbox" name="testcases"
class="checkbox"
value="<?php echo $testcase->id; ?>">
<?php echo $testcase->name; ?>
</li>
<?php
$count++;
}
?>
<li> <?php echo "Total Test Case COunt: $count";?> </li>
</ul>
I am using onestepcheckout in Magento. I have added in some extra flat rates and I am using two of them. I want to be able to only show one of the flat rates depending on the subtotal of the cart.
I have got the subtotal into a variable but the code has a foreach through each shipping method available so I need a way to say if $total is over 500 only show the second shipping method, if the total is under 500 only show the first shipping method.
<?php $total = Mage::getSingleton('checkout/session')->getQuote()->getSubtotal(); ?>
<?php foreach ($_shippingRateGroups as $code => $_rates): ?>
<dd><?php echo $this->getCarrierName($code) ?></dd>
<?php foreach ($_rates as $_rate): ?>
<dt style="margin-bottom: 5px;">
<?php if ($_rate->getErrorMessage()): ?>
<ul class="messages"><li class="error-msg"><ul><li><?php echo $_rate->getErrorMessage() ?></li></ul></li></ul>
<?php else: ?>
<input name="shipping_method" type="radio" class="validate-one-required-by-name" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> />
<label for="s_method_<?php echo $_rate->getCode() ?>"><!--<b><?php echo $this->getCarrierName($code) ?>:</b>--> <?php echo $_rate->getMethodTitle() ?>
<strong>
<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
<?php echo $_excl; ?>
<?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
(<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
<?php endif; ?>
</strong>
</label>
<?php endif ?>
</dt>
<?php endforeach; ?>
<?php endforeach; ?>
Just do ...
//first method here by default
if($total>500){
//second method here
}
Something tells me you already know how to do this though
I can't decipher, what the first and second method are in your code, otherwise I would've posted a more complete code
I have an array here which has (30 values e.g.).And I displayed it on a div. I want to split them into 3parts and display them in a three separated div. How can I do this one?Here's my code:
<div class="span4">
<?php if($col):?>
<?php foreach($col as $names):?>
<label class="checkbox">
<input type="checkbox" id="chk_distinct" class="check"
value="<?php echo $names->COLUMN_NAME;?>">
<?php echo $names->COLUMN_NAME;?></label>
<?php endforeach;?>
<?php endif;?>
</div>
It will display this one:
<div>
Checkbox1
Checkbox2
Checkbox3
Checkbox4
...
Checkbox30
</div>
I wanted it to look like this one:
<div1> <div2> <div3>
Checkbox1 Checkbox11 Checkbox21
Checkbox2 Checkbox12 Checkbox22
Checkbox3 Checkbox13 Checkbox23
Checkbox4 Checkbox14 Checkbox24
Checkbox5 Checkbox15 Checkbox25
... ... ...
Checkbox10 Checkbox20 Checkbox30
</div> </div> </div>
Any Idea about this one?Beginner here..
Use array_chunk For example
$cols = array_chunk($col, 10, true);
foreach ($cols as $col)
{
echo '<div>';
foreach ($col as $names)
{
echo '<label>'.$names->COLUMN_NAME.'</label>';
}
echo '</div>';
}
I have two tables listed (on screen) in PHP and the left should be hyperlinked so when click on it the right table will show a query. So at the beginning it should be empty then when clicked refresh the page with the selected listname's result.
unfortunately I have no experience with these things so i don't know the concept of it yet, but I am happy to learn:)
<form method="post" action="test.php">
<div id="left"><table>
<?php
$left="SELECT * FROM groups";
$resultleft=mysql_query($left);
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
?>
</table></div>
<div id="right"><table>
<?php
$right="SELECT * FROM grouplink WHERE grouplink.group_id= ";
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
}
?>
</table></div>
<?php
if (isset($_POST('???'))){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
?>
</form>
any help would be appreciated
Can for example link to table.php?gid=n, where n would be the group id. You can then check if $_GET['gid'] isset, and if it is, take that id and put it in your query.
if(isset($_GET['gid']))
$right = sprintf("SELECT * FROM grouplink WHERE grouplink.group_id=%u", $_GET['gid']);
You need mysql_fetch_assoc
instead of mysql_query
in:
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and also in:
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
so this will be:
while ($left=mysql_fetch_assoc($resultleft)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and similar issue with right
Thanks to Svish here is my new working code:
<?php include("db_con1.php");?>
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<div id="left">
<?php
$queryl = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$queryl->execute();
?>
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowl['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
<div id="right">
<?php
if(isset($_GET['gid'])) {
$gid=$_GET['gid'];
$queryr = $pdo->prepare('SELECT test3.name FROM test1, test2, test3 WHERE test1.id=test2.groupid AND test3.id=test2.peopleid AND test1.id='.$gid.' ORDER BY test3.name ASC');
$queryr->execute();
}
?>
<ul>
<?php foreach ($queryr as $i => $rowr) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_del[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowr['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowr['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
</form>
</body>
</html>