I am trying to dynamically populate a select tag in php, by printing values from an array of objects.
this is my code
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[i]->getId().'">'.$z[i]->getDescription().'</option>');
}
the array is populated, in fact if i try to print only the fields i get a result, but in the combo-box nothing appears
Replace i with $i
<select name="name">
<?php
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>');
}?>
</select>
Add select tag and replace i with $i
echo "<select>";
for($i = 0; $i < count($z); $i++) {
echo '<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>';
}
echo "</select>";
ok, i resolved by creating two support arrays
$ids = array();
$descs = array();
for ($i = 0; $i < count($z); $i++) {
$ids[$i] = $z[$i]->getId();
$descs[$i] = $z[$i]->getDescritpion();
}
and using them instead of the objects
for ($i = 0; $i < count($z); $i++) {
print('<option value ="' . $ids[$i] . '">' . $descs[$i] . '</option>');
}
not very efficient, but working.
thanks for the answer :)
Related
I got this code to generate a list of 6 random products.
How can I make it generate each product inside a div?
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
echo $ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart);
}
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
echo '<div>' . $ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart) . '</div>';
}
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
printf('<div> %s </div>',$ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart));
}
Attempting to generate table columns and rows based on input. With smaller numbers it appears to work appropriately, for example inputting 2 rows and 3 cols. But when inputting slightly larger numbers like 5 for rows will output an incorrect amount of rows sometimes looping indefinitely.
if(isset($_POST['submit'])) {
$rows = (int)$_POST['row_num'];
$cols = (int)$_POST['col_num'];
$n = 1;
$e = 0;
echo '<table id="">';
while(($e < $rows) && ($e < $cols)) {
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($i = 0; $i < $cols; $i++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
echo '</tr>';
}
$e++;
}
echo '</table>';
}
Firstly, you don't need the While loop, it's pointless and probably causing issues.
Secondly, you're using $i for both your rows and your columns, you can't use the same variable for both. Use $i for one and $j for the other. This should work:
if(isset($_POST['submit'])) {
$rows = (int)$_POST['row_num'];
$cols = (int)$_POST['col_num'];
$n = 1;
echo '<table id="">';
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n++ . '"></td>';
}
echo '</tr>';
}
echo '</table>';
}
scope variable $i problem here :
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($i = 0; $i < $cols; $i++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}
i guess 2nd for condition change $i to $j :
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}
I am making a website that has x amount of input fields. Each of the input fields is labeled team0, team1, team2 ... teamx and they are wrapped in a post form. Upon posting, I would like to check the POST variables, store them in an array, print out their values, but it seems I cannot use a variable when calling $_POST. I have tried it like so:
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team$i'];
echo $teamNames[$i] . "<br>";
}
What is the correct way to do this?
Try double quotes
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST["team$i"];
echo $teamNames[$i] . "<br>";
}
OR
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team'.$i];
echo $teamNames[$i] . "<br>";
}
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
Why won't $i count up one each time with the following code?
<?php if(get_field('staff_member')) { ?>
<div class="row-fluid">
<?php while(has_sub_field('staff_member'))
{
for($i = 0; $i <= 1; $i++)
echo '<div class="span3 mobile_width' . $i . '">
.....etc...
}
echo '</div>';
}
?>
The output has 4 items and they all return with the the class mobile_width0.
And it also outputs 2 of each item.
Because you are resetting it to 0 each time. You don't need for loop, that's why you had double output. You can do it like this:
<?php $i = 0;
while(has_sub_field('staff_member')) {
echo '<div class="span3 mobile_width' . $i . '">';
$i++;
}
Try
for($i = 0; $i < 1; $i++)
< instead of <=
Each time your while cicle iterates, $i gets reset to 0 and in this line for($i = 0; $i <= 1; $i++) you are saying that $i just can take 0 and 1 values
because your resetting "i" to 0 in your for loop.