How to merge two PHP for-loop - php

How do I merge two (for) in one code?
e.g first code
for ($i = 0, $n = count($options); $i < $n; $i ++) {}
and second code
for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++)
here is oll code
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_('index.php');?>" method="post" name="poll_vote_<?php echo $poll->id;?>" id="poll_vote_<?php echo $poll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="mod_voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="mod_voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?> <?php for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++) { ?>
<a href="<?php echo $params->get($u); ?>" onClick="return popup(this, 'notes')">
<img src="/images/stories/add.png" alt="play"></a>
<?php
} ?
I want in one poll show onle play button
now shows this
here is my xml code:

You don't really need that second for. You already have an index with $i from your first loop. (Except that it starts from 0 instead of 1, but you can easily get it working)
Remove the second for, and try this :
// echo $params->get($u);
echo $params->get('rel_article' . ($i + 1));

Related

show group of sets using foreach

I am having trouble figuring out how to write this code.
I have 1000 name db entries and I need to echo the results in sets of 4.
So the set # counts per each 4 entries until end of data.
How can I do this with php?
My result should look like this:
<div id="set-1">
<div>Name1</div>
<div>Name2</div>
<div>Name3</div>
<div>Name4</div>
</div>
<div id="set-2">
<div>Name5</div>
<div>Name6</div>
<div>Name7</div>
<div>Name8</div>
</div>
//and so on......
I would assume that your returned vlaues from your DB are in an array. I made a mock array just to mimick an array with 1000 entries.
<?php
$mockNameArray = array();
for ($i = 0; $i < 1000; $i++)
{
$mockNameArray[$i] = "Name" . $i;
}
?>
Since you have a set number returned from your DB it is easy to run a for loop instead of a foreach loop. I would do something like this.
<?php for ($i = 0, $x = 1; $i < 1000, $x < 251; $i++, $x++): ?>
<div id="set-<?php echo $x; ?>">
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; $i++; ?></div>
<div><?php echo $mockNameArray[$i]; ?></div>
</div>
<p>
<?php endfor; ?>

PHP limit the loop based on available array element

I created the available seat with a for loop below:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And I want to add a booked class if the $i variable matches with the $k variables which taken from the booked seat array elements below:
$booked_seat = array('1','4','5','6','7');
the result:
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
So I did this:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo ($i == $booked_seat[$i-1]) ? 'booked' : '' ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And i get the offset Error because the $booked_seat is not much as the $seats loop, how to limit the loop so it will not offset?
Thank you before
Try this:
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo (in_array($i, $booked_seat)) ? "booked" : "" ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
warning: code is untested.
You could always flip the $booked_seat array, then test for the existence of the array element using isset(). Something like this:
<?php
$seats = 7;
$booked_seat = array('1','4','5','6','7');
$booked_a = array_flip($booked_seat);
for ($i=1; $i <= $seats; $i++) {
printf("seat: %d%s\n", $i, isset($booked_a[$i]) ? " booked" : "");
}
(This was my test, you can add your HTML to suit.)
Example: http://3v4l.org/5npsm
You should use the method sizeof().
So:
$num_seats = sizeof($seats);
Then a for loop with this.
for($i = 0; $i < $num_seats; $i++){
//code
}

While loop with two conditions

This while loop is supposed to continue until the end of the array or until counter reaches 6, what am I doing wrong here?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC) && ($counter < 6))
{
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}
Works without the && ($counter < 6)but shows the wrong number of images, adding this will show the correct number of boxes (where the images should be) but not retrieve the images or names from the array.
Thanks for any help.
Couldn't you just use break; inside, when the second condition isn't true anymore?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC))
{
if($counter > 5){
break;
}
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}

Change currency icon block to drop-down list

My client would prefer a currency drop-down list to the currency icon block installed on a 1.5.1 OpenCart theme. I've tried coding it but get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ';' in /...file path......
I've pasted the code and used before and after the offending line.
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name=”curselect” onchange=”$(‘input[name=\'currency_code\']‘).attr(‘value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();”>
***<?php for ($z = 0; $z <= $a – 1; $z++) { ?>***
<?php if ($thisCurCode[$z] == $currency_code) { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>” selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } else { ?>
<option value=”<?php echo $thisCurCode[$z]; ?>”><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
Any help would be most appreciated.
$a – 1 contains something like an m-dash, or whatever it's called. Not a - minus sign. This most likely happened because you copy-pasted code that had been through an auto-formatter like some mail programs or word processors.
after 2 hours of work here is my solution
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" name="formcur" id="formcur">
<div id="currency">
<?php echo $text_currency; ?>
<?php
$a = 0;
foreach ($currencies as $currency) {
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left']) {
$thisCurSymb[$a] = $currency['symbol_left'];
} else {
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
?>
<select name="curselect" id="curselect" onchange="$('input[name=\'currency_code\']').attr('value', document.getElementById('curselect').value); document.forms['formcur'].submit();">
<?php
for ($z = 0; $z <= $a - 1; $z++) {
if ($thisCurCode[$z] == $currency_code) { ?>
<option value="<?php echo $thisCurCode[$z]; ?>" selected><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php
} else {
?>
<option value="<?php echo $thisCurCode[$z]; ?>"><?php echo $thisCurTitle[$z]; ?> <?php echo $thisCurSymb[$z]; ?></option>
<?php } ?>
<?php } ?>
</select>
<input type="hidden" name="currency_code" value="" />
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
</div>
</form>
I think your problem is that the value returned in your if block is a String?
if ($currency['symbol_left']) {
Had to clean up your code to be able to make any sense of it. You had a load of odd ` chars stick to " and ' and try to keep inside PHP as much as possible.... this is not an answer yet.... I suggest you upload this and identify to us exaclty which line is at fault, I suspect its in the first echo
<?php
$a = 0;
foreach ($currencies as $currency)
{
$thisCurTitle[$a] = $currency['title'];
$thisCurCode[$a] = $currency['code'];
if ($currency['symbol_left'])
{
$thisCurSymb[$a] = $currency['symbol_left'];
}
else
{
$thisCurSymb[$a] = $currency['symbol_right'];
}
$a++;
}
echo "<select name='curselect' onchange='$('input[name=\'currency_code\']').attr('value’, this.options[this.selectedIndex].value).submit(); $(this).parent().parent().submit();'>";
for ($z = 0; $z <= $a – 1; $z++)
{
if ($thisCurCode[$z] == $currency_code)
{
echo "<option value='".$thisCurCode[$z]."' selected>".$thisCurTitle[$z]." ".$thisCurSymb[$z]."</option>";
}
else
{
echo "<option value='".$thisCurCode[$z]."'>".$thisCurTitle[$z]." & nbsp;".$thisCurSymb[$z]."</option>";
}
}
?>
found the problem.....
You have placed a calculation inside the for() statement...
$a =10; // this is to mimic your counter
$a=$a-1; // do you subtract here
//for ($z = 0; $z <= $a – 1; $z++) // this is the issue.
//for ($z = 0; $z <= ($a – 1); $z++) // doesn't work either.
for($z=0; $z <= $a; $z++)
{
echo $z." hello</br />";
}
http://php.net/manual/en/control-structures.for.php
cant see any reason why you wouldnt be able to do it the way you tried, my PHP errors in the same way, even encapsulating the subtract in ()... fixes the issue but still bothers me. Rarely use for()...
I hope that fixes your trouble!

Divide php for cycle in 2 columns

I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?
<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically
I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}

Categories