How to add current session id with link/download link? - php

I'm not sure but how to simply add current session id with a link/download links ?
<?php if($images) { $i=0; for ($index = 0; $index < count($images); $index++){ $i++ ?>
Download<br/>
<?php }}?>

For example:
<?php if($images) { $i=0; for ($index = 0; $index < count($images); $index++){ $i++ ?>
Download<br/>
<?php }}?>

Related

Create several DIV containers using for loop in PHP

Just wanted to ask how can I (if it is possible at all) create several DIV containers using for loop in PHP. Thank You.
for ($i = 0; $i < 20; $i++) {
echo '<div>$i</div>';
}
<?php for ($i = 0; $i < 20; $i++) { ?>
<div><?= $i; ?></div>
<?php } ?>
<?php for ($i = 0; $i < 20; $i++): ?>
<div><?= $i; ?></div>
<?php endfor; ?>
If we're really going down this silly road...
echo "<ul>";
foreach(array_fill(0, 999, 'why!?') as $key => $really) {
echo "<li>$key: $really</li>";
}
echo "</ul>";

PHP Array, Get every 4 results and output in a loop

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++)

Browsing an array without knowing if content exists

So, what à do is trying to display the 3 first element of an array. Always 3. But, there is not always at least 3 elements.
So, what I've done is using an if :
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if($post["Project"]["Post"][$i]){ ?>
...
<?php } ?>
<?php } ?>
But, I keep having "undefined offset" error when there is not 3 entries at least. Anyone with a solution ?
foreach (array_slice($array, 0, 3) as $item) {
echo $item;
...
}
or:
$i = 1;
foreach ($array as $item) {
...
if ($i++ >= 3) {
break;
}
}
foreach is always preferable to iterate arrays, precisely because you cannot access anything that doesn't exist.
Try the following:
<?php for ($i = 0; $i <= 2; $i++) {
if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php }} ?>
Use isset() to check whether the key exists.
<?php if(isset($post["Project"]["Post"][$i])){ ?>
Or you could use:
$posts = $post["Project"]["Post"];
foreach ($posts as $i => $post) {
//...
if ($i === 2) break;
}
change it to if (isset($post ...
Alternatively:
foreach (array_slice($post["Project"]["Post"], 0, 3)) { ...
<?php for ($i = 0; $i <= 2; $i++) {
if( isset($post["Project"]["Post"][$i] )){
...
}
} ?>
you do not need <?php...?> tags for every line.
try ifset.
<?php for ($i = 0; $i <= 2; $i++) { ?>
<?php if(isset($post["Project"]["Post"][$i])){ ?>
...
<?php } ?>
<?php } ?>
Or, do a count first could be a way too.
Calculate before, which condition is met first (the actual size of the array or the max number - in your case 3) and then just traverse those elements:
$min = min( 3, count( $post["Project"]["Post"] ) );
for ($i = 0; $i < $min; $i++) {
if($post["Project"]["Post"][$i]){
...
}
}

Echo a variable with $i PHP

I am trying to make a dynamic for loop with some database connection.
<?php for ($i = 1; $i <= 8; $i++): ?>
<?php echo $question[0]->option
<?php endfor; ?>
Where the option is in database stored like this: option1, option2, option3 etc..
I have the $i variable which does the counting, but I do not know how I put that into the $question[0]->option variable. Tried $question[0]->option,$i etc, but no luck.
Try this:
<?php
for ($i = 1; $i <= 8; $i++) {
$num = "option".$i;
echo $question[0]->$num;
}
?>
You don't have to open and close the php tags in a block of only php code.
<?php
for ($i = 1; $i <= 8; $i++):
$optionname = "option$i";
echo $question[0]->$optionname
endfor;
?>

How to kickout in a foreach loop?

For example in a for loop you can kick out like this:
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
$i = count($ary);
echo $i;
}
Or in a while loop:
$i = 0;
while($i < count($ary)){
if($ary[$i++] == 'blah')
$i = count($ary);
echo $i;
}
Not sure really what you mean by "kick out", but:
To skip to the next item, use continue;
To stop the entire loop, use break;
If I understand your question correctly what you are looking for is the break keyword.
PHP Break
Use break
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
break;
echo $i;
}
for($i = 0; $i < count($ary); $i++){
if($ary[$i] == 'blah')
break;
echo $i;
}
foreach($ary as $c){
if($c=='blah')break;
}
Manual

Categories