i have this bit of code:
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>
and currently it echos the links in order they appear on the website. I would like for it to echo the results in a reverse order (the last link on example.com to echo as the first with this code)
any help is appreciated! thanks.
for ($row = $count - 1; $row > -1 ; $row--) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
PHP has a function for it: array_reverse.
$a[1] = array_reverse($a[1]);
Also why would you use ["$row"] instead of [$row]? There is no functional difference as numeric strings get converted back to numbers when using them as indexes, so don't worry about that, but just because something can be done doesn't mean you should do it.
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$a = array_reverse($a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>
Related
I am pulling data from a database and I want to create array names on the fly.....group1, group2...etc using a for loop. I wonder if this is possible at all? The code below obviously doesn't work and I'm only including it to demonstrate what I'm trying to do. Any help would be much appreciated!
<?php
for ($i=1; $i <=40; $i++){
$group.$i = [];
}
?>
Its not so much "array names" that you are looking for as a much as a nested array. $group[$i] would give you a nested array. eg
for ($i=1; $i <=40; $i++){
$group[$i] = [];
}
$group[1][] = 'foo';
echo $group[1][0];
// prints: foo
Something like this ? This works 100%
// $result = $conn->query($sql);
$i = 0;
$all = array();
while($row = $result->fetch_assoc()) {
$i++;
$arr[$i]['id'] = $row['id'];
$arr[$i]['firstname'] = $row['first_name'];
$arr[$i]['lastname'] = $row['last_name'];
$all[] = $arr[$i];
}
echo '<pre>';
echo print_r($all);
$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
That's my code but it only shows the following when I try it:
[TR][TD="align: left"]15[/TD][TD="align: left"][/TD][/TR]
It should show that but instead of only 15 it should be 1-15 inclusive.
Any ideas?
You keep setting $res to a new value rather than appending to it. Using $res .= 'something'; is like saying $res = $res . 'something';. Doing this will allow you to keep the previous value of $res and appending more to the end of it.
$x = 1;
$num = 15;
$res = '';
while($x <= $num) {
$res .= '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
}
echo $res;
$x = 1;
$num = 15;
while($x <= $num) {
$res = '[TR][TD="align: left"]'.$x.'[/TD][TD="align: left"][/TD][/TR]';
$x++;
echo $res;
}
Put echo $res; inside the loop, otherwise it will echo just the $res from the last loop cycle instead of all 15 times.
Sorry for the beginner question.
I'm searching about an hour, but I can't understand why my $row outside from the second while doesn't function... The name variable run just the $row var doesn't function...
$i = 0;
while($i < 8)
{
$str = "SELECT * FROM `$name[$i]`";
$result = mysql_query($str, $connessione);
$l = mysql_num_rows($result);
while($l > 1)
{
$strs = "SELECT * FROM `$name[$i]` WHERE `Livello` = '$l'";
$results = mysql_query($strs, $connessione);
$row[$i][$l] = mysql_fetch_array($results);
if I put here the echo I can view the mysql variable
echo $row[$i][$l]['var'];
$l--;
}
if I put here echo $row[$i][$l]['var']; he send me the error " Undefined offset"
$i++;
}
Hope you can help me...
In the place where you put:
echo $row[$i][$l]['var'];
$l value is 0 and you set $row values for $l from 1 to mysql_num_rows($result)
if you put there:
echo $row[$i][1]['var'];
it should work fine assuming mysql_num_rows($result) was more than 1 element.
Probably your code should look like this:
$i = 0;
while($i < 8)
{
$str = "SELECT * FROM `$name[$i]`";
$result = mysql_query($str, $connessione);
$l = mysql_num_rows($result);
while($l > 0) // changed 1 to 0
{
$strs = "SELECT * FROM `$name[$i]` WHERE `Livello` = '$l'";
$results = mysql_query($strs, $connessione);
$row[$i][$l] = mysql_fetch_array($results);
if i put here the echo i can view the mysql variable
echo $row[$i][$l]['var'];
$l--;
}
// added extra loop to display array values
$whileIndex = 0;
while (true) {
if (!isset($row[$i][$whileIndex]['var']) {
break;
}
echo $row[$i][$whileIndex]['var']; // should work
++$whileIndex;
}
$i++;
}
When you try to use echo, $i = 8 and $l = 1. These keys doesn´t exists in your array.
I have been thinking for hours but i still cant get a solution for this
Basically what i want to do is to echo a separator inside a while, it should be something like this
$num = 1;
while($num < 3){
echo 'dog';
//function to stop while
echo 'separator';
//function to continue while
echo 'cat';
$num++;
}
I want to get this output
dog
dog
dog
separator
cat
cat
cat
I dont know if i explained myself well but hope you understand. Thank you very much in advance.
Update: I know i can make this with 2 while functions but is it possible to make it using only one while function?
definitely yes you can with one while ~function. :)
function OnlyOneWhileFunction($echoThis, $howManyTimes){
$i = 1;
while($i <= $howManyTimes){
echo $echoThis."\r\n";
$i++;
}
}
OnlyOneWhileFunction('dog', 3);
echo 'separator';
OnlyOneWhileFunction('cat', 3);
$num = 0;
$dogs = '';
$cats = '';
$seperator = 'seperator';
while($num < 3){
$dogs .= 'dog';
$cats .= 'cat';
$num++;
}
echo $dogs . $seperator . $cats;
Save the output of each of the dogs and cats then combine at end.
Alternate solution:
$items = array_reverse(array("cat","dog"));
$output = array();
while(count($items) > 0)
{
$item = array_pop($items);
$output[] = implode("\n", array_fill(0, 3, $item));
}
echo implode("\nseparator\n", $output);
You can replace \n with <br> for HTML output (or use nl2br).
Try this:
<?php
$num = 0;
while($num < 7){
if($num < 3)
echo 'dog';
elseif($num == 3)
echo 'separator';
elseif($num>3)
echo 'cat';
echo "<br>";
$num++;
}
?>
You would be better off using a C style for loop and a function call for this:
$recho = function($out, $limit) {
for ($x=0; $x<$limit; $x++) {
echo $out . "\n";
}
}
$recho('dog',3);
echo "seperator\n";
$recho('cat',3);
Put this inside a <pre> on a webpage to get your line breaks, or replace the "\n" with <br> tags.
The following code should work:
$num = 0;
while($num <= 6){
if($num < 3) echo 'dog<br/>';
else if($num == 3) echo 'separator<br/>';
else echo 'cat<br/>';
$num++;
}
Here's another approach if you wanted n number of options and assuming each option is iterated over the same number of times. I'd do some cleanup but this is a quick and dirty approach:
<?php
/**
* Loops items with separator every nth time
* #param array list of items
* #param integer number of iterations
* #param string separator text
*/
function loopItemsWithSeparator(array $items, $count, $separator) {
$items = array_reverse($items);
while(!empty($items)) {
$item = array_pop($items);
for($i = 0; $i < $count; $i++) {
echo $item . "\n";
}
if (count($items) > 0) {
echo($separator . "\n");
}
}
}
loopItemsWithSeparator(array('dog', 'cat', 'bird'), 3, 'separator');
?>
I'm really new at php just doing some work, I want to save images in a php array and then show them in the screen, but I cannot save them or display them.
<?php
$min = 1;
$max = 9;
$number1 = rand($min,$max);
for ($i=1 ; $i<=$number1 ; $i++){
$firstN [$i] = echo "<img src='index.jpg' border='0'>";
}
echo $firstN [1];
?>
This is what I got , and the last line is to test it but nothing works, I google the topic but it doesn't help.
Thanks in advance.
As long as index.jpg is in the same directory as your file, this should work:
<?php
$firstN = array();
$min = 1;
$max = 9;
$number1 = rand($min, $max);
for ($i = 0; $i < $number1; $i++){
$firstN[] = '<img src="index.jpg" border="0">';
}
echo $firstN[0];
?>
Cleaned up the code a bit. When storing information in the array, you don't use echo and, like Mister pointed out, you had a space in the echo at the bottom of the code between the array-variable and the brackets.