I am trying to loop through a certain number of pages to index them.
My code is as following:
$indexedpages = '5';
for ($i = 0; $i <= $indexedpages; $i++) {
$url = 'https://thisisadomain.com/'.$i.'/';
....
rest of code here
....
}
But this does not seam to work, it only takes the first page over and over again for the number given in $indexedpages.
What do I do wrong?
Thank you
If you have used $url
outside the loop then there will be only a single value, ie: you have to use $url
inside the loop so as to get all 5 url's. Try on:
$url_arr = array();
$indexedpages = '5';
for ($i = 0; $i <= $indexedpages; $i++) {
echo $url = 'https://thisisadomain.com/'.$i.'/';
echo "<br>"; // Displays all values assigned for '$url'
// OR you can create an array
$url_arr[] = 'https://thisisadomain.com/'.$i.'/';
}
echo $url; // Only displays last value for '$url'
var_dump($url_arr); // List of all urls.
As I can see, you're just overriding the value over and over again. If you want to get all pages, use an array();
Example:
$pages = 5;
$url = array();
for ($i = 0; $i <= $pages; $i++) {
$url[] = 'https://thisisadomain.com/'.$i.'/';
//.... rest of code here ....
}
print_r($url);
Output:
Array ( [0] => https://thisisadomain.com/0/ [1] => https://thisisadomain.com/1/ [2] => https://thisisadomain.com/2/ [3] => https://thisisadomain.com/3/ [4] => https://thisisadomain.com/4/ [5] => https://thisisadomain.com/5/ )
Is this what you want? Hope this helps.
Regards.