Jump to content

ludo1960

Members
  • Content Count

    45
  • Joined

  • Last visited

Everything posted by ludo1960

  1. Hi guys, I need to transform a filename like one_test to one-test and just_another_test to just-another-test. I'm just getting started with PCRE and this is beyond my new found skills $str = $const . 'function.' . $o . '.html' ; need to check $o for filenames like one_file becomes one-file and just_another_file becomes just-another-file Thanks in advance...
  2. ludo1960

    replace underscores with hyphens in a string

    I managed it with str_replace(), thought I was going to be forced to use preg_replace(), and that I don't know about
  3. Hi guys, I am using this dom parser, and its pretty easy to find all links on a page then following the links to sub pages, but how do I know when to stop, i.e.when the page has no more children? I came across this script for dealing with child pages, but needless to say it outputs nothing: $links = $html->find('div.layout'); foreach ($links as $link) { foreach ($link->children() as $children) { if ($children->tag == 'a') { $output [] = $children->plaintext; //or whatever you want } } } How do I test for a link that has no more children?
  4. ludo1960

    An array, a string or an object???

    And just after I posted I found the answer. grrrrrrrrrr!!!!!! if(!empty($html)){ foreach($html->find('ul') as $ul) { $testul[] = $ul->outertext; if (isset($testul[2])) { $links = $testul[2] ; } } $html2 = str_get_html($links) ; foreach($html2->find('a') as $a) { $links2[] = $a->href ; } }
  5. Hi guys, I'm using http://simplehtmldom.sourceforge.net/manual.htm and my code is : $html = file_get_html('test/somefile.html'); if(!empty($html)){ foreach($html->find('ul') as $ul) { $testul[] = $ul->outertext; if (isset($testul[2])) { $links = $testul[2] ; } } $html2 = str_get_html($links) ; foreach($html2->find('a') as $a) { $links2 = $a->href ; } } echo '<pre>',print_r($links, 1),'</pre>'; The links array prints out a nice html page, and I would like to grab all the a tags for that page. I tried $html2 = str_get_html($links) ; as you see in my code above, but the $links2 page only prints out the last a tag. So I need to either convert the $html2 string to an array or find a way to extract the (a) tags from a string. Any ideas?
  6. ludo1960

    Strange query behavior

    Hi guys, In my connection.php I have: $db->query("DROP TABLE IF EXISTS mydata") ; $db->query("CREATE TABLE mydata ( ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, guid INT, title VARCHAR(100), body LONGTEXT, term VARCHAR(100) )"); and my query code : $myarray = array ( guid => 100, title => "title test", body => "just a test", term => "term test", ); $myplaceholders[] = '(' . implode (", ", array_fill(0, count($myarray), '?')) . ')'; //also tried '(?,?,?,?)' $mykeys = implode(', ', array_keys($myarray)); array_push($values, ...array_values($myarray)); //also tried $values = array_values($myarray) ; $res = $db->prepare("INSERT INTO mydata ($mykeys) VALUES " . join(', ', $myplaceholders)) ; if ($res->execute($values)) { echo 'data inserted'; } else { echo 'error in query'; } After executing the code, the table is created but no data is inserted. The strange thing is when I leave the create table statement out of the connection.php and run the code the data is inserted. Any ideas where I'm going wrong?
  7. ludo1960

    Strange query behavior

    Yeah that makes sense, stick all the create table stuff in an install file
  8. ludo1960

    Strange query behavior

    This works: if ($db->query ( "DESCRIBE mydata" )) { } else { $db->query("CREATE TABLE drupaldata ( ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, guid INT, title VARCHAR(100), body LONGTEXT, term VARCHAR(100) )"); } oops, no it doesn't, how to create table if it does not exist?
  9. ludo1960

    Strange query behavior

    That would explain it, if I remove the drop and create from the connection.php, it starts working. IF (!EXISTS mydata) { CREATE TABLE ...... }; Is the above code the way to go?
  10. ludo1960

    Strange query behavior

    Changed the name of the database table in the code to make sure the database was ok and tried again, restarted mysql, still says 'data inserted' but database is empty?
  11. ludo1960

    Strange query behavior

    oops should of said, it prints out "data inserted"
  12. ludo1960

    Strange query behavior

    Ok changed to display_errors = On, changed error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT to error_reporting = -1 added quotation marks to $myarray keys. No errors show, database still not updated
  13. ludo1960

    PDO double entries

    Hi folks, Can anybody spot my rookie mistake in a PDO insert query? $myarray prints out as expected, but the database has results doubled up i.e. Page_id 1 to 10 then again Page_id 1 to 10 <?php include 'connection.php' ; $startnum = 1; $endnum = 10; for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true) $myarray = array( 'Page_id' => $i, 'County' => $response['county'], 'Country' => $response['country'] ); echo '<pre>'; print_r($myarray); echo '</pre>'; $mykeys = implode (", ", array_keys($myarray)) ; $myvals = implode (", ",array_fill(0, count($myarray), '?')); $res = $db->prepare("INSERT INTO TestDB ($mykeys) VALUES ($myvals)") ; $res->execute(array_values($myarray)); } //endfor
  14. ludo1960

    PDO double entries

    The County and Country were the easy ones, do the $response['price'] ? Please!
  15. ludo1960

    PDO double entries

    If you are a black belt at multi-D arrays the size of Donald Trumps' head, you're probably right, if not, use the flatten function.
  16. ludo1960

    PDO double entries

    For anybody following on, here is the finished working code. If you're faced with a crazy multi dimensional array from a JSON source that you have to get into a mysql database, this is for you! <?php function FlattenMultiArray($array,$bKeepKeys=true,$key_prefix='') { $array_flattened=Array(); foreach($array as $key=>$value){ if(Is_Array($value)){ $array_flattened=Array_Merge( $array_flattened, FlattenMultiArray($value,$bKeepKeys,$key) ); } else{ if($bKeepKeys){ $array_flattened["{$key_prefix}{$key}"]=$value; } else{ $array_flattened[]=$value; } } } return $array_flattened; } include 'connection.php' ; $startnum = 41; $endnum = 45; //$placeholders = []; //not required! $values = []; for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $resp = FlattenMultiArray($response, true, NULL); $myarray = array( 'Page_id' => $i, //your database table column name => data you want to insert 'County' => $resp['county'], 'Country' => $resp['country'], 'Post_Town' => $resp['0post_town'], ); $mykeys = implode(', ', array_keys($myarray)); $myplaceholders[] = '(' . implode (", ", array_fill(0, count($myarray), '?')) . ')'; array_push($values, ...array_values($myarray)); } //endfor $res = $db->prepare("INSERT INTO YourDB ($mykeys) VALUES " . join(', ', $myplaceholders)) ; $res->execute($values);
  17. ludo1960

    PDO double entries

    That works just fine, where can I find info describing the use of "..." never seen that in anybody elses code before?
  18. ludo1960

    PDO double entries

    Hmmm, the only time i've seen odd characters floating about, was when I copied and pasted from this site. Here's the code again. i've triec mousepad and open office but nothing shows up, even tried https://marketplace.visualstudio.com/items?itemName=ShaneRay.InvisibleCharacterVisualizer#qna still no superfluous characters $db = pdoConnect(); $db->query("DROP TABLE IF EXISTS test2DB"); $db->query("CREATE TABLE test2DB ( ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, Page_id INT, County VARCHAR(50) NOT NULL, Country VARCHAR(50) NOT NULL, Post_Town VARCHAR(50) NOT NULL )");
  19. ludo1960

    PDO double entries

    I use visual studio code (latest version), is this Bill Gates revenge? What editor have you got? The only line that looks suspicious is: array_push($values, array_values($myarray));
  20. ludo1960

    PDO double entries

    $startnum = 1; $endnum = 5; $placeholders = []; $values = []; for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $resp = FlattenMultiArray($response, true, NULL); print_r($resp['county']); }// end for Prints out county name 5 times , so I guess that means it works
  21. ludo1960

    PDO double entries

    Ok heres the for loop, for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $resp = FlattenMultiArray($response, true, NULL); $myarray = array( 'Page_id' => $i, 'County' => $resp['county'], 'Country' => $resp['country'], 'Post_Town' => $resp['0post_town'] ); $mykeys = implode(', ' , array_keys($myarray)); $placeholders[] = "(?,?,?,?)"; array_push($values, array_values($myarray)); } //endfor $res = $db->prepare("INSERT INTO test2DB ($mykeys) VALUES " . join(', ', $placeholders)); $res->execute($values); echo $res->queryString; When I try this i get a http error 500 and nothing is posted to the database
  22. ludo1960

    PDO double entries

    Okay, here goes... connection.php (used your example connection.php) and working! function pdoConnect() { $dsn = "mysql:dbname=".DBNAME."; host=".HOST."; charset=utf8"; $db = new pdo($dsn, USERNAME, PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); return $db; } $db = pdoConnect(); $db->query("DROP TABLE IF EXISTS test2DB"); $db->query("CREATE TABLE test2DB ( ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, Page_id INT, County VARCHAR(50) NOT NULL, Country VARCHAR(50) NOT NULL, Post_Town VARCHAR(50) NOT NULL )"); index.php connects to API and sends a single Page_id to mysql database, includes an array flatten function because the API returns a dogs dinner of a multi dimensional array. <?php function FlattenMultiArray($array,$bKeepKeys=true,$key_prefix='') { $array_flattened=Array(); foreach($array as $key=>$value){ if(Is_Array($value)){ $array_flattened=Array_Merge( $array_flattened, FlattenMultiArray($value,$bKeepKeys,$key) ); } else{ if($bKeepKeys){ $array_flattened["{$key_prefix}{$key}"]=$value; } else{ $array_flattened[]=$value; } } } return $array_flattened; } include 'connection.php'; $startnum = 1; $endnum = 10; $placeholders = []; $values = []; $i = 12; $url = "http://apii.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $resp = FlattenMultiArray($response, true, NULL); //echo "<pre>"; print_r($resp); echo "</pre>"; $myarray = array( 'Page_id' => $i, 'County' => $resp['county'], 'Country' => $resp['country'], 'Post_Town' => $resp['0post_town'] ); //echo "<pre>"; print_r($myarray); echo "</pre>"; $mykeys = implode(', ' , array_keys($myarray)); $myvalues = array_values($myarray); $placeholders[] = "(?,?,?,?)"; $res = $db->prepare("INSERT INTO test2DB ($mykeys) VALUES " . join(', ', $placeholders)); $res->execute($myvalues); echo $res->queryString; What is required is for a range of pages to be processed ie start page 10 and process to page 14. As soon as I introduce a for loop, my cunning plan goes to pot! Oh and when i paste code into the editor here all my nice coding formatting goes to pot too!
  23. ludo1960

    PDO double entries

    I only want certain values from the $resp array, and change the key names to match the database column names e.g. $resp['county'] = County or $['post code'] to become Post_Code as mysql does not like 'post code' as a column name: Just a few lines before the array_push... you have $response = $mydata[$i] What does that do? I tried to select a few array values from my array_push but it does not work: array_push($values, $i, $resp['county'], $resp['country']);
  24. ludo1960

    PDO double entries

    That's what I don't understand, the $response array has the ['county'] and ['country'] values in it, but I can't access them from within a loop. this a chicken and egg situation. How am I supposed to access the values from the $response array and make them available within the loop?
  25. ludo1960

    PDO double entries

    oops, I meant your $mydata array is outwith the loop, whereas $myarray is inside the loop $mydata =[10=>['county' => 'Cheshire', 'country' => 'England'], ['county' => 'Lancashire', 'country' => 'England'], ['county' => 'Cumbria', 'country' => 'England'], ['county' => 'Derbyshire', 'country' => 'England'], ['county' => 'Yorkshire', 'country' => 'England'], ['county' => 'Lincolnshire', 'country' => 'England'], ['county' => 'Surrey', 'country' => 'England'], ['county' => 'Hampshire', 'country' => 'England'], ['county' => 'Pembrokeshiire', 'country' => 'Wales'], ['county' => 'Devon', 'country' => 'England']
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.