I have to parse 2 kind of table
one is this
http://leghe.fantagazzetta.com/f12-13/ ("Classifica Generale")
and the other two table are here
http://leghe.fantagazzetta.com/f12-13/formazioni?id=30339&g=4
how can i extract this data to an array?
for the first table i wrote this code but I really don't know what I'm doing
<?php
require('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica');
$tabClassifica = $html->find('table#classifica tr');
foreach($tabClassifica as $n) {
$team=$n->find('td',0)->outertext;
$arrayTeams[] = array('teamname' => $team);
}
?>
<pre>
<? print_r($arrayTeams); ?>
</pre>
I've to get this Array structure
[1] => Array
(
[TeamName] => A.C. Tua
[Pt.] => 9
[G] => 4
[V] => 3
[N] => 0
[P] => 1
[G+] => 8
[G-] => 5
[Somma Punti] => 293,50
)
[2] => Array
(
[TeamName] => Ehi Team
[Pt.] => 7
[G] => 4
[V] => 2
[N] => 1
[P] => 1
[G+] => 5
[G-] => 5
[Somma Punti] => 279,50
)
[3] => Array
(
[TeamName] => Brontolo
Could someone guide me?
You can use PHPQuery to achieve what you want.
Here is an example code:
include('phpQuery-onefile.php');
$content = file_get_contents('http://leghe.fantagazzetta.com/f12-13/');
$html = phpQuery::newDocumentHTML($content);
$table = $html->find('#classifica tbody');
$general_ranking = array();
$i=0;
foreach($table->children('tr') as $tr){
/**
* #var DOMElement $tr
*/
$getTd = $tr->getElementsByTagName('td');
foreach($getTd as $td){
/**
* #var DOMElement $td
*/
$general_ranking['tr_'.$i][] = trim($td->textContent);
}
++$i;
}
This should display something similar to:
array (size=6)
'tr_0' =>
array (size=7)
0 => string 'A.C. Tua' (length=8)
1 => string '9' (length=1)
2 => string '4' (length=1)
3 => string '3' (length=1)
4 => string '0' (length=1)
5 => string '1' (length=1)
6 => string '293,50' (length=6)
'tr_1' =>
array (size=7)
0 => string 'Ehi Team' (length=8)
1 => string '7' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '1' (length=1)
5 => string '1' (length=1)
6 => string '279,50' (length=6)
'tr_2' =>
array (size=7)
0 => string 'Brontolo' (length=8)
1 => string '7' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '1' (length=1)
5 => string '1' (length=1)
6 => string '274,50' (length=6)
'tr_3' =>
array (size=7)
0 => string 'milanelcuore' (length=12)
1 => string '6' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '0' (length=1)
5 => string '2' (length=1)
6 => string '281,00' (length=6)
'tr_4' =>
array (size=7)
0 => string 'LONGOBARDA' (length=10)
1 => string '5' (length=1)
2 => string '4' (length=1)
3 => string '1' (length=1)
4 => string '2' (length=1)
5 => string '1' (length=1)
6 => string '254,50' (length=6)
'tr_5' =>
array (size=7)
0 => string 'i puffi' (length=7)
1 => string '0' (length=1)
2 => string '4' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
5 => string '4' (length=1)
6 => string '258,00' (length=6)
Edit:
After answering I got interested in simple_html_dom so I decided to try it out.
The coding style is a little bit easier than PHPQuery but it's not that stable I think.
Anyway, here is the code you need to get it working:
include('simple_html_dom/simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica');
$tabClassifica = $html->find('table#classifica tr');
foreach($tabClassifica as $n) {
/**
* #var simple_html_dom_node $n;
*/
$tds = $n->find('td');
// Don't allow empty records.
$team = trim(strip_tags($tds[0]->innertext));
if($team == "" || $team == " ") continue;
$arrayTeams[] = array(
'TeamName' => $team,
'Pt.' => trim(strip_tags($tds[1]->innertext)),
'G' => trim(strip_tags($tds[2]->innertext)),
'V' => trim(strip_tags($tds[3]->innertext)),
'N' => trim(strip_tags($tds[4]->innertext)),
'P' => trim(strip_tags($tds[5]->innertext)),
'G+' => trim(strip_tags($tds[6]->innertext)),
'G-' => trim(strip_tags($tds[7]->innertext)),
'Somma Punti' => trim(strip_tags($tds[8]->innertext)),
);
}
Related
i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
How to run select * from table where col1+col2=b; using codeigniter's active record. Using mysql
I tried this but fail
$this->db->where("col1 + col2",$b)->get("table")->result_array();
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '915087' at line 4
SELECT * FROM table WHERE a + b 25
You can do that how you were thought. Just fixed out your mistake
follow the below code. Thanks. If it's useful please acknowledge it.
$b = 25;
$data = $this->db->where(array("(number1 + number2) = " => $b))->get("dummy_table")->result_array();
echo "<pre>";
print_r($data);
// output
Array
(
[0] => Array
(
[id] => 1
[number1] => 10
[number2] => 15
[number3] => 25
)
[1] => Array
(
[id] => 2
[number1] => 23
[number2] => 2
[number3] => 25
)
[2] => Array
(
[id] => 3
[number1] => 9
[number2] => 16
[number3] => 25
)
[3] => Array
(
[id] => 4
[number1] => 23
[number2] => 2
[number3] => 25
)
)
$this->db->select('(col1+col2) as col3');
$this->db->from('table');
$this->db->HAVING('col3 = ', $b);
$query = $this->db->get();
Try this.
Hope it will solved your problem. If you have any further query you can knock me feel free.
$this->load->model('dummy_table_model');
$b = 25;
$data = $this->db->get_where("dummy_table", array("(number1 + number2) = " => $b))->result_array();
// $this->db->last_query();
var_dump($data);
// Out put
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'number1' => string '10' (length=2)
'number2' => string '15' (length=2)
'number3' => string '25' (length=2)
1 =>
array (size=4)
'id' => string '2' (length=1)
'number1' => string '23' (length=2)
'number2' => string '2' (length=1)
'number3' => string '25' (length=2)
2 =>
array (size=4)
'id' => string '3' (length=1)
'number1' => string '9' (length=1)
'number2' => string '16' (length=2)
'number3' => string '25' (length=2)
3 =>
array (size=4)
'id' => string '4' (length=1)
'number1' => string '23' (length=2)
'number2' => string '2' (length=1)
'number3' => string '25' (length=2)
I have an array that contains products and their categoryID, some of products have the same category. the output of the array is below
array (size=5)
'categoryID' =>
array (size=3)
0 => string '5' (length=1)
1 => string '2' (length=1)
2 => string '2' (length=1)
'name' =>
array (size=3)
0 => string 'HP Player' (length=9)
1 => string 'Android App' (length=11)
2 => string 'TV' (length=2)
'price' =>
array (size=3)
0 => string '600' (length=3)
1 => string '111' (length=3)
2 => string '1' (length=1)
'qty' =>
array (size=3)
0 => string '22' (length=2)
1 => string '22' (length=2)
2 => string '222' (length=3)
'exprice' =>
array (size=3)
0 => string '13200' (length=5)
1 => string '2442' (length=4)
2 => string '222' (length=3)
I want to combine the products that have same categoryID, the output should be like below:
array (size=2)
=> 0 array
'categoryID' =>
array
0 => string '5' (length=1)
'name' =>
array
0 => string 'HP Player' (length=9)
'price' =>
array
0 => string '600' (length=3)
'qty' =>
array
0 => string '22' (length=2)
'exprice' =>
array
0 => string '13200' (length=5)
=> 1 array
'categoryID' =>
array
0 => string '2' (length=1)
'name' =>
array
0 => string '2' (length=1)
1 => string '2' (length=1)
'price' =>
array
0 => string '111' (length=3)
1 => string '1' (length=1)
'qty' =>
array
0 => string '22' (length=2)
1 => string '222' (length=3)
'exprice' =>
array
0 => string '2442' (length=4)
1 => string '222' (length=3)
how could I do that? Thanks
$arr = array (
'categoryID' =>
array (
0 => '5',
1 => '2',
2 => '2'
),
'name' =>
array (
0 => 'HP Player',
1 => 'Android App',
2 => 'TV'
),
'price' =>
array (
0 => '600',
1 => '111',
2 => '1'
),
'qty' =>
array (
0 => '22',
1 => '22',
2 => '222'
),
'exprice' =>
array (
0 => '13200',
1 => '2442',
2 => '222'
)
);
$cat_count = count(array_keys($arr['categoryID']));
$new_arr = array();
for($i=0;$i<$cat_count;$i++){
for($j=0;$j<$i;$j++){
if($arr['categoryID'][$i] == $arr['categoryID'][$j]){
$arr_key = array_search($arr['categoryID'][$i], $arr['categoryID']);
$cat_exists = 1;
}
}
if($cat_exists){
$new_arr[$arr_key]['name'][] = $arr['name'][$i];
$new_arr[$arr_key]['price'][] = $arr['price'][$i];
$new_arr[$arr_key]['qty'][] = $arr['qty'][$i];
$new_arr[$arr_key]['exprice'][] = $arr['exprice'][$i];
}else{
$new_arr[$i]['categoryID'][] = $arr['categoryID'][$i];
$new_arr[$i]['name'][] = $arr['name'][$i];
$new_arr[$i]['price'][] = $arr['price'][$i];
$new_arr[$i]['qty'][] = $arr['qty'][$i];
$new_arr[$i]['exprice'][] = $arr['exprice'][$i];
}
}
P.S :: $arr is input array & $new_arr is output(result) array!
I ‘ve got an array like this =>
this is a result from a google analytics request. I asked for the amout of visits for the last three months.
$statPerMonth
array (size=2)
'08' => // The month (August)
array (size=34)
0 =>
array (size=3)
0 => string '08' (length=2) // Month again
1 => string 'admin.testweb.fr' (length=19) // host
2 => string '1' (length=1) // amount of visits
1 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '6' (length=1)
2 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'www.audigie-espace-auto.reseau-fivestar.fr' (length=31)
2 => string '9' (length=1)
3 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'carrosserie-abberis.reseau-fivestar.fr' (length=38)
2 => string '7' (length=1)
'07' =>
array (size=47)
0 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.anothersite.testweb.fr' (length=13)
2 => string '1' (length=1)
1 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=16)
2 => string '2' (length=1)
2 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=19)
2 => string '1' (length=1)
3 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '20' (length=2)
4 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.admin.testweb.fr' (length=19)
2 => string '1' (length=1)
This array respresent the amount of visits for my websites but
you can see that the values [‘08’][‘1’] and [‘08’][‘2’] are identicals (only ‘www.’ differs)
I want to merge those cells and add their value (because it’s the same site !) in order to get the total amount of visits for a site with it’s two hostnames.
Consider $sites as an array of Site Object (websites).
the getHost() method will return the site host for exemple ‘my-host.fr’ without the ‘www’
consider $statsPerMonth array explained above
finally consider this algorithm
foreach ($statsPerMonth as $actualMonth => $stats) {
foreach($sites as $site) {
$siteHost = $site->getHost();
foreach ($stats as $row) {
if (strstr($row['1'], $siteHost)) {
if(isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
} else {
$globalStats[$actualMonth][$siteHost] = 0;
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
}
}
if(!isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = 0;
}
}
}
}
This algorithm return the $globalStats array in this form
array (size=3)
'08' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 1
'audigie-espace-auto.reseau-fivestar.fr' => int 15
'carrosserie-abberis-fivestar.fr' => int 16
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 0
'07' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 2
'audigie-espace-auto.reseau-fivestar.fr' => int 20
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
'06' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 0
'audigie-espace-auto.reseau-fivestar.fr' => int 29
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
This is exaclty what I want but I think we can improve this algorithm to make it more efficient (because the arrays are big). Have you any idea in order to make this algorithm better ?
Thank you.
Try to replace all "foreach" by "for", such as :
foreach ($array as $key => $value) {
}
by
$keys = array_keys($array);
$keyCount = count($keys);
for($i = 0; $i < $keyCount; $i++) {
$key = $keys[$i];
$value = $array[$key];
}
I want to run what I thought was going to be a simple query.
I want to return a database table just the values) in the same 2D style it is written in into $data[i][k]
I am using PHP's PDO, and the closest I have been able to get is:
$result=$database->query("SELECT * FROM `garage_statistics`",$bind = null,$fetch = 'FETCH_COLUMN');
$this->statement = $this->pdo->prepare($query);
$result = $this->statement->fetchAll(PDO::FETCH_COLUMN,1);
Which returns
0 => string 'Garage2' (length=1)
1 => string 'Garage3' (length=7)
2 => string 'Garage4' (length=7)
3 => string 'Garage6' (length=7)
4 => string 'Garage7' (length=7)
However I can't get it to loop for all the columns. I tried fetchall and got back:
array (size=10)
0 =>
array (size=14)
'name' => string 't' (length=1)
0 => string 't' (length=1)
'tablename' => string 't' (length=1)
1 => string 't' (length=1)
'numfloors' => string '3' (length=1)
2 => string '3' (length=1)
'status' => string '4' (length=1)
3 => string '4' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:47:13' (length=19)
6 => string '2012-12-07 13:47:13' (length=19)
1 =>
array (size=14)
'name' => string 'Garage 3' (length=8)
0 => string 'Garage 3' (length=8)
'tablename' => string 'Garage3' (length=7)
1 => string 'Garage3' (length=7)
'numfloors' => string '2' (length=1)
2 => string '2' (length=1)
'status' => string '3' (length=1)
3 => string '3' (length=1)
'numspots' => string '0' (length=1)
4 => string '0' (length=1)
'spotsinuse' => string '0' (length=1)
5 => string '0' (length=1)
'time' => string '2012-12-07 13:49:46' (length=19)
6 => string '2012-12-07 13:49:46' (length=19)
While this does contain all the data, it has so much stuff that I don't want.
Is there a simple way to get a basic 2D array of a table using PDO?
$result = $this->statement->fetchAll(PDO::FETCH_NUM);
Returns correct type