Unfortunately, after research I still couldn't find the answer for my issue.
The issue is that I cannot print data from multiple pages. Data is printed only once. Perhaps I am missing silly mistake here, which you could help me find it.
$cycles=10;
$listValue=0;
for ($cy = 0; $cy < $cycles; $cy++){
$html = file_get_contents("http://www.website.com/rate/today.aspx?d=02.03.2015&r=". $listValue ."01&c=#");
$dom = new DOMDocument;
#$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('td');
$data = array();
while($table = $tables->item($i++))
{
//stuff
}
foreach($data as $item)
{
echo "Rank - " . $item['rank'] . "</br>";
}
$listValue++;
echo $listValue."<br>";
}
So basically, I am able to print data only of first page.
Declare the collection variable before the first loop $whatWasCollected = "";
Assign the collected data to a variable at the end of the first loop & Append to the variable each time.
$whatWasCollected .= "This is what I want to print..."
Get rid of the last loop and just echo the complete string.
echo $whatWasCollected;
Just a suggestion. Try it out and let me know if it works seem like and interesting question to me.
Related
I have used the following code to store a small timetable in an array. I am trying to return specific elements of the array but get the error Cannot use object of type DOMElement as array.
I would be grateful if somebody could tell me how to retrieve the nodeValue of certain elements and display them.
$doc = new DOMDocument();
$doc->loadHTML($timetable);
$headers = $doc->getElementsByTagName('th');//' missed which is typo i think
$cells = $doc->getElementsByTagName('td');//' missed which is typo i think
$array = iterator_to_array($cells);
I can loop through this and display the entire table with a for loop, and that's fine, but I can't return 1 value from the array:
for ($i=0; $i<=5; $i++){
$val = $array[$i];
echo $val[1]->nodeValue;
}
And this returns the error I mentioned above.
So, guys, my problem is that i'm creating an array from a mysql column, but, when i echo the array items, it returns me nothing, i'm about this for a while and i'm not seeing a possible error, hope can get some help. Here' s my code: (I know about the mysql to mysqli, but i'm just beginning and trying the logical stuff. :))
$duracao = mysql_query(
"SELECT SUM(fim_periodo-inicio_periodo)/0.01666667 AS mysum
FROM afunda_eleva_$a"
); //THIS $duracao IS INSIDE A FOR LOOP THAT EXECUTES THE QUERY IF A CONDITION IS SATISFIED (DEPENDING ON $a and $prevNum), it is working fine!
while ($row2 = mysql_fetch_assoc($duracao))
{
$duracao_afunda_eleva[] = $row2['mysum'];
}
so, to test some possible problems, i've put:
$i2 = sizeof($duracao_afunda_eleva);
echo $i2;
echo <br>;
which returns me the right size of the array, the problem comes here, i think:
for($i1 = 0 ; $i1 < sizeof($duracao_afunda_eleva); $i1++)
{
echo $duracao_afunda_eleva[$i1] or die("couldn't echo"
. mysql_error());
echo "<br>";
}
and this returns me no value. I really would appreciate any suggestion. Thanks in advance!
Try print_r instead of echo - echo outputs (string)$variable, which in case of an array is just "array". Also, echo is a language construct and not a function, so your or die(...) is problematic, as language constructs don't have return values.
Generally it's better to not use or die(...) constructs, and handle problems correctly (using a defined return value, so cleanup code can run, for example). Also, not being able to output anything with echo would mean that your php/whatever installation is seriously gone bad, at which point you probably won't even reach that line in your code, and even then it won't have anything to do with mysql.
Since the problem is not in the variable, try to use an alias in your MySQL select:
SELECT SUM(fim_periodo-inicio_periodo)/0.01666667 as mysum
Then in your PHP, you can get $row2['mysum'];
It may not solve the problem, but it is a good start.
Personally I use:
$arr = array ( /* ... */ );
echo "<pre>";
var_dump($arr);
echo "</pre>";
In your last for-loop, you use $i++, it should be $i1++
A quick tip is not to use the sizeof function in your for loop. This gets run with each iteration of your loop and you will notice an improvement in performance if you move it outside the loop (or use a foreach loop instead). e.g.
$count = sizeof($duracao_afunda_eleva);
for($i = 0 ; $i < $count; $i++) {
echo $duracao_afunda_eleva[$i] . '<br />';
}
or
// Foreach loop example
foreach ($duracao_afunda_eleva as $key => $value) {
echo $value . '<br />';
}
or what you could also do is this one line
echo implode('<br />', $duracao_afunda_eleva);
Use this code:
for($i1 = 0 ; $i1 < sizeof($duracao_afunda_eleva); $i1++)
{
echo $duracao_afunda_eleva[$i1] or die("couldn't echo". mysql_error());
echo "<br>";
}
You increased $i instead of $i1.
Although, with arrays, it's usually easier to use a foreach loop.
This runs from the first to the last element in the array.
Like so:
foreach ($duracao_afunda_eleva as $value) {
echo $value;
}
I would also suggest removing the space in $row2 ['mysum']:
$duracao_afunda_eleva[] = $row2['mysum'];
Edit
Just noticed it now, but your fetch-statement is wrong... It has a typo in it:
while ($row2 = mysql_fetch_assoc($duracao))
Make sure it reads mysql instead of myqsl.
I'm trying to get the result of my foreach loop into an url to do a simplexml_load_file with.
So it goes like this:
(...) //SimpleXML_load_file to get $feed1
$x=1;
$count=$feed1->Count; //get a count for total number of loop from XML
foreach ($feed1->IdList->Id as $item1){
echo $item1;
if($count > $x) {
echo ',';} //Because I need coma after every Id, except the last one.
$x++;
}
The two echo are just to see the result. It gives me something like:
22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843
I would like to put that in a url to make a simplexml_load_file just like that
$feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';
So it would look like:
$feed_url = 'http://www.whatevergoeshere22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843someothertext';
I've try to store it into an array or a function and then call it into the feed_url but it did not work the way I tried it.
I hope it's clear, I'll answer fast to questions if not.
Thanks.
It's really difficult to make out what you want, so I'm going to guess you want to store the list as a comma delimited string in a variable. the easiest way is to implode the array of ids
$ids = array();
foreach ($feed1->IdList->Id as $item1){
$ids[] = (string) $item1;
}
$RESULT_OF_FOREACH = implode(',', $ids);
$feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';
I am parsing data from a website via xml that always changes so I do not know what data to search for, just the location. I get the data by calling upon the specific class for the data needed. Within this class that is retrieved, there are only certain things I need retrieved. So for example, I retrieve the class .candy. Now .candy corresponds to different parts on the website so let us say that candy retrieves "Cookies Chocolate gummy bear". I only want "chocolate".
My idea was to put everything into an array an array and then access it. Assume the array is called test, then I would want to access "chocolate" by $test[1];
It is not working for me (says array is out of bounds whenever I put a number bigger than 0) and I was wondering if anyone knows where my mistake is within my code? Please note that the above was purely an example for better understanding and not the actually data I need.
Thank you
function getData($html)
{
#$doc=new DOMDocument();
#$doc->loadHTML($html);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
//$images=$xml->xpath('//[]');
$info=$xml->xpath('//*[#class="date"]');
$arr= array();
foreach ($info as $img) {
$arr= array($img);
}
return $arr[3];
}
You are replacing $arr in every loop. $arr[] = $img; should work.
Example:
function getData($html)
{
#$doc = new DOMDocument();
#$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
$info = $xml->xpath('//*[#class="date"]');
$arr = array();
foreach ($info as $img)
{
$arr[] = $img;
}
return $arr[3];
}
foreach ($info as $img) {
$arr[] = $img;
}
for($j = 0; $j < $length; $j++){
while($answersRow = mysql_fetch_row($fetch)){
if($answersRow[0] == $curr_answer_id[$j]){
echo "<input type='checkbox' value='$answersRow[0]' checked>$answersRow[1]</input> ";
} else {
echo "<input type='checkbox' value='$answersRow[0]'>$answersRow[1]</input> ";
}
}
}
Assuming $fetch is my result, here's my dilemma. The for loop represents the array of checkboxes. The first loop around, everything works fine. However, any more than that, we no longer cycle through the while loop because of the internal mysql pointer. I know I can move the pointer around using mysql_data_seek(), but don't have an idea on how to do so usefully. If I move it back to 0, then it just outputs everything for as many things that were checked.
I basically want to traverse through for each question through the database but without any overlap. It is a bit hard to explain so I apologize if I am not explaining this simple problem properly; I'll try and clarify if need be.
You can save the results of your first while in an array:
//> FIRST LOOP
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row; //> Saving in the $rows array
//> All your code from your first loop
}
//> SECOND LOOP
foreach($rows as $v) {
//> All your code you need for your second loop without any additional query
}
Also sorry If I misunderstood your question but It is not so clear what you are asking for
Addendum
After reading better your question if I understood correctly you want to achive everything within only one loop. In this case you can save the html you need in the first while like this:
$firstLoop = '';
$secondLoop = '';
while() {
$firstLoop .= '<option> [...]';
$secondLoop .= '<div> [...]';
}
At this point with one loop you have built the html you need and they are in $firstLoop and $secondLoop. This is of course faster then any other solutions