Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.
Related
i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?
Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.
$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;
Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:
Let's say we have an URL looking something like this.
http://domain.com/index.php?p=1&u=A1b2C3d4E5f6G7h8I9j
The number span after ?p= goes from 1 to 999 and the rest is unchanged.
Every URL contains one short line of text.
What would a script look like which can run through all 999 URLs and display their contents?
It would be easy. You can use the FOR LOOP.
<?php
for($i=1; $i < 1000; $i++) {
echo file_get_contents('http://domain.com/index.php?p=' . $i . '&u=A1b2C3d4E5f6G7h8I9j');
}
Documentations:
For Loop
file_get_contents() method
This is going to be resource intensive. But taking a shot in the dark, this should work:
<?php
$urlContent = array();
$urlStart = 'http://domain.com/index.php?p=';
$urlEnd = '&u=A1b2C3d4E5f6G7h8I9j';
$count = 1;
while($count <= 999){
$urlContent[$count] = file_get_contents($urlStart.$count.$urlEnd);
$count++;
}
foreach($urlContent as $content){
echo $content.'\n';
}
?>
This code is Untested
foreach(($_POST["msg"] as $mg) AND ($_POST["control"] as $id))
{
echo $mg;
echo $id;
}
i need make something like that, any way to do? i'm trying to get 10 mysql records and edit all of them
No, that won't work. The closest thing I can see to what you're trying to do is:
for($i = 0; $i < count($_POST["msg"]); $i++) {
echo $_POST["msg"][$i];
echo $_POST["control"][$i];
}
Assuming that "msg" and "control" will always contain the same amount of items.
Assuming both $_POST['msg'] and $_POST['control'] are actually arrays, have numeric keys (thanks #iMoses), and have the same length, you could use a for loop -
for ($i = 0; $i < count($_POST["msg"]); $i++){
$mg = $_POST['msg'][$i];
$id = $_POST['control'][$i];
}
I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan
The goal is to count the number of paragraphs in a group of users text...
(I will assume its always bigger than 5 paragraphs for this exercise)
Then I want to 1/2 the number of paragraphs, round it down and enter some content(echo "yehhoo") in between.
I do understand the way I have gotten my $newvalue is not very good, would also like help on that...
<?php
$choppedup = explode("<p>",$node->field_long_body[0]['safe']);
$choppedpos = count($choppedup);
$choppedpos2 = $choppedpos/2;
$newvalue = floor($choppedpos2);
//I know this is working to here... the rest not so sure.
for($j = 0; $j < $choppedup; $j ++):
print $choppedup[$j];
if ($j == $newvalue):
echo "yehhoo" ;
endif;
endif;
?>
Thank you
for
...
endfor; # not endif
your $newvalue calculation is not terrible, for the array iteration I'd rather suggest foreach loop (using curly braces):
foreach($choppedup as $ind => $p) {
echo $p;
if ($ind == $newvalue) {
echo 'yehoo';
}
}
"Yehhoo" for curly brackets!
for($j == 0; $j < $choppedup; $j ++) {
print $choppedup[$j];
if ($j == $newvalue) {
echo "yehhoo";
}
}
Why do such a complex loop to count the number of paragraph tags?
You can do something like this:
$sInput = '<p>Hello World</p><p>What is going on</p><p>Blah</p>';
if(preg_match_all('/<p>/', $sInput, $arMatches)!==false)
print count($arMatches[0]) . ' paragraphs<br/>';
Of course the above needs some work to make sure there are text between the paragraph tags, but this should work for you need.