SimpleHTMLDom iteration through table - php

I am using SimpleHTMLDOM to get information from my school roster. The problem is that the table structure is pretty hard to parse and I am looking for some help.
The table looks like this:
http://pastebin.com/xg3mRAHw
The code looks like this:
http://pastebin.com/gWW7WyDA
The result looks like this (also included how I want the result to look like!):
Current format:
array
3 =>
array
'28-11-2011' =>
array
0 => string '08.45-10.30 ' (length=12)
1 => string 'CMD-1 HC interaction design' (length=27)
2 => string 'CMD-1vt-p2.01 - CMD-1vt-p2.18 ' (length=30)
3 => string 'OVk45' (length=5)
4 => string 'J.P. van Leeuwen' (length=16)
5 => string '10.30-12.15 ' (length=12)
6 => string 'CMD-1 Training samenwerken' (length=26)
7 => string 'CMD-1vt-p2.09 - CMD-1vt-p2.10 ' (length=30)
8 => string 'SL433' (length=5)
9 => string 'B. Hartman' (length=10)
Wanted format:
array
3 =>
array
'28-11-2011' =>
array
0 =>
array
'time' => string '08.45-10.30 ' (length=12)
'name' => string 'CMD-1 HC interaction design' (length=27)
'group' => string 'CMD-1vt-p2.01 - CMD-1vt-p2.18 ' (length=30)
'place' => string 'OVk45' (length=5)
'teacher' => string 'J.P. van Leeuwen' (length=16)
1 =>
array
'time' => string '10.30-12.15 ' (length=12)
'name' => string 'CMD-1 Training samenwerken' (length=26)
'group' => string 'CMD-1vt-p2.09 - CMD-1vt-p2.10 ' (length=30)
'place' => string 'SL433' (length=5)
'teacher' => string 'B. Hartman' (length=10)
The problem is that I do not understand how I can get to this result using (only) SimpleHTMLDOM. I am sure that I'm missing something here because I'm close to the final markup of the array. The last step to have it actually show up like the future example is something I cannot get to work.
Could someone give me a few tips on how to proceed and get the array like the way I want it to? I have been looking at XSL too but that is far too complicated for me at this point.

You need to segment the tr array as well.
$count = 0;
foreach ($table as $tr) {
...
$output[$info['week']][$info['date']][$count] = array();
$count++;
...
$output[$info['week']][$info['date']][$count][] = $td->innertext;
Now as for the 'time', 'name', 'group' etc. values, I don't see those anywhere in the xml, so I guess you will just have to maintain an inner count when appending td->innertext.

Related

Preg_replace not working as wanted

Basically i have the following text stored in $text var :
$text = 'An airplane accelerates down a runway at 3.20 m/s2 for 32.8 s until is finally lifts off the ground. Determine the distance traveled before takeoff'.
I have a function that replaces some keywords on the text from an array named $replacements which is (I did a var_dump on it) :
'm' => string 'meter' (length=5)
'meters' => string 'meter' (length=5)
's' => string 'second' (length=6)
'seconds' => string 'second' (length=6)
'n' => string 'newton' (length=6)
'newtons' => string 'newton' (length=6)
'v' => string 'volt' (length=4)
'speed' => string 'velocity' (length=8)
'\/' => string 'per' (length=3)
's2' => string 'secondsquare' (length=12)
The text goes through the following function :
$toreplace = array_keys($replacements);
foreach ($toreplace as $r){
$text = preg_replace("/\b$r\b/u", $replacements[$r], $text);
}
However, there is a difference between what I expect and the output :
Expected Output : an airplane accelerates down runway at 3.20 meterpersecondsquare for 32.8 second until finally lifts off ground determine distance traveled before takeoff
Function Output : an airplane accelerates down runway at 3.20 meterpers2 for 32.8 second until finally lifts off ground determine distance traveled before takeoff
Notice that I expect 'meterpersecondsquare' and I get 'meterpers2' (the 's2' isn't replaced) while the 'm' and '/' were replaced with their values.
I noticed that when I put m/s instead of m/s2 it works fine and gives :
an airplane accelerates down runway at 3.20 meterpersecond for 32.8 second until finally lifts off ground determine distance traveled before takeoff
So the problem is basically it doesn't match that s2. Any thoughts why is it the case?
Move the s2 replacement before the s replacement.
Since you are doing the replacement one at a time, you are destroying the s2 before it gets a chance to replace it.
3.20 m/s2 will be transformed like this
[m] 3.20 meter/s2
[s] 3.20 meter/second2
[/] 3.20 meterpersecond2
Which results in meterpersecond2
Here is the proper order
'm' => string 'meter' (length=5)
'meters' => string 'meter' (length=5)
's2' => string 'secondsquare' (length=12)
's' => string 'second' (length=6)
'seconds' => string 'second' (length=6)
'n' => string 'newton' (length=6)
'newtons' => string 'newton' (length=6)
'v' => string 'volt' (length=4)
'speed' => string 'velocity' (length=8)
'\/' => string 'per' (length=3)

JpGraph : no error but broken picture

I make a graph with JPGraph but I've a little problem.
No error was displayed but the picture is a "broken picture".
Here is my code :
$graph = new Graph(1000,300);
$graph->img->SetMargin(40,30,50,40);
$graph->SetScale("textlin");
$graph->title->Set("Graph");
$graph->ygrid->Show();
$graph->ygrid->SetColor('blue#0.7');
$graph->ygrid->SetLineStyle('dashed');
$graph->xgrid->Show();
$graph->xgrid->SetColor('red#0.7');
$graph->xgrid->SetLineStyle('dashed');
$graph->title->SetFont(FF_ARIAL,FS_BOLD,11);
$bigCourbe = array();
$i = 0;
foreach($bigData as $data)
{
var_dump($data);
$courbe = new LinePlot($data);
$courbe->value->Show();
$courbe->value->SetFont(FF_ARIAL,FS_NORMAL,9);
$courbe->value->SetFormat('%d');
$courbe->mark->SetType(MARK_FILLEDCIRCLE);
$courbe->mark->SetFillColor("green");
$courbe->mark->SetWidth(2);
$courbe->SetWeight(10);
echo $function[$i];
$courbe->SetLegend($function[$i++]);
$bigCourbe[] = $courbe;
}
$graph->xaxis->title->Set("Heures");
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
foreach ($bigCourbe as $elem) {
$graph->Add($elem);
}
//$graph->Stroke();
So, the result seems good :
array (size=24)
0 => string '0.000' (length=5)
1 => string '0.000' (length=5)
2 => string '0.000' (length=5)
3 => string '0.000' (length=5)
4 => string '0.000' (length=5)
5 => string '0.000' (length=5)
....
Index HC <- it's the $function[$i] var
array (size=24)
0 => string '0.200' (length=5)
1 => string '0.200' (length=5)
2 => string '0.100' (length=5)
3 => string '0.200' (length=5)
4 => string '0.200' (length=5)
5 => string '0.200' (length=5)
....
Index HP
array (size=24)
0 => string '0.000' (length=5)
1 => string '0.000' (length=5)
2 => string '0.000' (length=5)
3 => string '0.000' (length=5)
4 => string '0.000' (length=5)
5 => string '0.000' (length=5)
Index HPTE
array (size=24)
0 => string '0.200' (length=5)
1 => string '0.200' (length=5)
2 => string '0.100' (length=5)
3 => string '0.200' (length=5)
4 => string '0.200' (length=5)
5 => string '0.200' (length=5)
....
Total Heure
I do not see the problem...
If I remove the debug, and uncomment the $graph->Stroke(); line. I have that :
Can you help me ?
Thank you in advance.
in my case (php 7.1.0) it was Deprecated message
just put
ini_set('display_errors', 0);
at the top of your script and try again.
or
comment out $graph->Stroke(); and see what error(or warning) you have
I know this is an old question but I just had the same problem and fixed it by removing everything before the <?php.
THis was suggested in the link below - check 1.b.
Solution Source
How do you display the graph? Do you have an HTML page? Please show the HTML code.
I have a similar problem where I have no error message, but get the broken image icon.
jpGraph can't include PHP file
One thing I would recommend is to add the lines
//save to file
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
if the image from the file is correct, then you can rule out the graph generating code.

What array function should I use for creating an index?

Hello guys I am trying to create an index of all words on html page that my crawler parses.
At this moment I have managed to breakdown the html page into an array of words and I have filtered out all the stop words.
At this stage I have a few problems.
The array of words from the parsed html page have words that are repeated, I like that because I still have to record how many times a word appeared in the page.
The array looks like this.
$wordsFromHTML =
array (size=119)
0 => string 'web' (length=3)
1 => string 'giants' (length=6)
2 => string 'vryheid' (length=7)
3 => string 'news' (length=4)
4 => string 'access' (length=6)
5 => string 'mails' (length=5)
6 => string 'mobile' (length=6)
7 => string 'february' (length=8)
8 => string 'access' (length=6)
9 => string 'mails' (length=5)
10 => string 'web' (length=3)
11 => string 'february' (length=8)
12 => string 'access' (length=6)
13 => string 'mails' (length=5)
14 => string 'desktop' (length=7)
15 => string 'february' (length=8)
16 => string 'hosting' (length=7)
17 => string 'web' (length=3)
18 => string 'giants' (length=6)
19 => string 'vryheid' (length=7)
20 => string 'february' (length=8)
22 => string 'us' (length=2)
Now I want to save all the words from the $wordsFromHTML to the $indesArray which is my final index.
It should look like this.
$indexArray = array('web'=>array('url'=>array(0,10,17)))
The problem is how to keep incrementing the position ($wordsFromHTML keys) for each word that was repeated from the $wordsFromHTML array in the final index array.
The index array should only have unique words and if another word that already exists try to come in, we use the already existing word which has the same URL and increment its position.
Hope you understand my question.

Smarty value of array extraction using date_format result on other array value as a key

I have two arrays in a smarty template: $months and $contract.
{$months|var_dump} gets this:
array (size=12)
1 => string 'января' (length=12)
2 => string 'февраля' (length=14)
3 => string 'марта' (length=10)
4 => string 'апреля' (length=12)
5 => string 'мая' (length=6)
6 => string 'июня' (length=8)
7 => string 'июля' (length=8)
8 => string 'августа' (length=14)
9 => string 'сентября' (length=16)
10 => string 'октября' (length=14)
11 => string 'ноября' (length=12)
12 => string 'декабря' (length=14)
array values are russian names of months in genitive.
{$contract|var_dump} gets this
'date_till' => '1355518365' (length=10)
so I need to create a month number first from $contract.date_till. it is usually done like
{$contract.date_till|date_format:"%m"}
And now the question is: how do I extract a month name from $months array by the month number made of $contract.date_till with date_format?
I've tried many variants described in smarty manuals, but noone works. For example, this one doesn't:
{$months[{$contract.date_till|date_format:"%m"}]}
{assign var=monthNo value=$contract.date_till|date_format:"%m"}
{$months.$monthNo}
This will give u the month of the necessary date.

Count() returns different number of entries than var_dump()

Hey guys I'm tired and can't figure this one out so any help would be appreciated.
I have an array called $Row that I get from database table.
When I run var_dump($Row); I get the following:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'erik' (length=4)
'username' => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
'email' => string '' (length=0)
4 => string '0' (length=1)
'date_join' => string '0' (length=1)
5 => string '0' (length=1)
'date_mod' => string '0' (length=1)
6 => string '1' (length=1)
'active' => string '1' (length=1)
7 => string '1' (length=1)
'admin' => string '1' (length=1)
8 => string '0' (length=1)
'deleted' => string '0' (length=1)
When I run echo count($Row); I get value 18.
Count and var_dump are right next to each other, there is no modification of $Row.
Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...
Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:
array
0 => string '1' (length=1)
1 => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
4 => string '0' (length=1)
5 => string '0' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '0' (length=1)
* I'm using mysql_fetch_array() to retrieve the data and put it into a table.
You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.
Because your strings are also values in $Row they just seem to be indented because of the string char.
That means
'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...
are also in your array
becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).
There is very well 18 elements in your array.
I think that applying count to this:
0 => string '1' (length=1)
'id' => string '1' (length=1)
returns 2 instead of 1!
0 and id are two different items of the array!
This taken for the others 8 elements make the count return 18
Actually, there are indeed 18 elements in your array:
indices 0 through 8 (9 elements in total)
id, username, ... another 9 elements
9 + 9 = 18
If you only want the numeric indices, and supposing you're using MySQL:
$Row = mysql_fetch_array($result, MYSQL_NUM)
Source: http://php.net/manual/en/function.mysql-fetch-array.php

Categories