Jump to content

ludo1960

Members
  • Content Count

    78
  • Joined

  • Last visited

Everything posted by ludo1960

  1. Hi guys, Yet another rookie question, trying to implement this: I have a function: <?php //MyHelper.php function getnext( $name ) { $conn = DB::table('counter')->where('name', $name )->first(); $result = $conn->findAndModify( ['name' => $name], ['_id' => ['seq' => 1]], ['seq' => true], ['new' => true, 'upsert' => true] ); return $result['seq']; } ?> And I am calling it so: $db_array = getnext(array('name' => 'role_id')); print_r( $db_array); Results in call to a member function on NULL, not sure where I'm going wrong! It is making a connection and the counter table has a 'role_id' in it. Can anyone tell me where I'm going wrong?
  2. ludo1960

    findAndModify() usage in a function

    Got it working, <?php use Jenssegers\Mongodb\Eloquent\Model; use Illuminate\Support\Facades\DB; function get_next( $name ) { $old_num = DB::collection('counter') ->where( 'name', $name )->pluck('_id'); $new_num = $old_num[0] + 1; DB::collection('counter') ->where( 'name', $name )->delete(); DB::collection('counter')->insert( ['_id' => $new_num, 'name' => $name ] ); } Voila, auto incrementing _id field for mongoDB, Thanks for the pointers guys, got a much better understanding of how php works in the laravel framework
  3. ludo1960

    findAndModify() usage in a function

    After more digging, i've installed a function helper So to follow your comments I think the classes I need are like this: <?php //namespace app; use Jenssegers\Mongodb\Eloquent\Model; use Illuminate\Support\Facades\DB; if (!function_exists('get_next')) { /** * description * * @param * @return */ function get_next( $name ) { DB::table('counter') ->where('name', $name )->increment('_id'); $retval = array_values(['_id']) ; return $retval; } } The methods I need should be in the classes listed above. Not sure what namespace I need, or if I even need one . Excuse my lack of knowledge, I'm just learning this stuff.
  4. ludo1960

    findAndModify() usage in a function

    Ok, if you look here that single line should give access to the underlying table data in laravel. Then if you look here and scroll down to the "Incrementing or decrementing a value of a column" it shows how to increment a value in a database field, and the text editor here wont let me switch off bold type, not my day I think. I thought it was clear what I am trying to achieve, a global function to take care of increments.
  5. ludo1960

    findAndModify() usage in a function

    My latest effort: <?php //MyHelper.php function get_next( $name ) { DB::collection('counter') ->where('name', $name )->increment('_id'); return array_values(['_id']) ; } ?>
  6. ludo1960

    findAndModify() usage in a function

    Hands up!! So goes the learning curve, maybe I should start at the beginning. I am trying (not very successfully) to create an increment function in the Laravel framework for a mongodb database. _id column . Just to confuse me even more not all of mongoDB functions are available to me as I am using this package. As I understand it mongoDB does not have an native increment field, so my cunning plan was to create a collection and store the incremented values for each $name. So that for example I have a collection named 'counter' and the records are _id = 100 and name = 'some_id' I want to call a function to increment the value and return the current value of the _id i.e. 101. Sounds simple, but too many things to take into consideration for a rookie. If you help me out, I promise never to laugh at your glorious President ever again, but only if you stop laughing at the joke that is Westminster. Is it a deal? PS I'm Scottish, so don't care what's going on in London, Angerland as we call it!
  7. ludo1960

    phppgadmin sql error

    Hi guys, Thought I would give postgres a go, installed it on debian 9 then installed phppgadmin. When I try to run SQL in phppgadmin: CREATE TABLE `posts` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `author_id` int(10) unsigned NOT NULL , `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `content` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `rate` int(255) COLLATE utf8_unicode_ci NOT NULL, `release_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; I get the following error: Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /usr/share/php/adodb/adodb-error.inc.phpon line 114 Even the SQL does not run, as I get the error: SQL error: ERROR: syntax error at or near "`" LINE 1: CREATE TABLE `posts` ( ^ Am i missing some sort of configuration in postgres or phppgadmin. I should add that I have installed drupal and laravel using the postgres database with no issues whatsoever, so postgres is working. I have got php7-2 installed, so I don't understand why phppgadmin is looking in /usr/share/php. Any ideas guys?
  8. ludo1960

    phppgadmin sql error

    For anyone wanting a quick intro into Postgres, this site is easy reading http://www.postgresqltutorial.com
  9. ludo1960

    phppgadmin sql error

    Point taken, this will help in the meantime https://github.com/dimitri/pgloader/releases/tag/v3.4.1 until I suss out postgres. But why is phppgadmin using adodb and not php7.2-pgsql, Can't find anything the config.inc.php that mentions the driver to use?
  10. ludo1960

    count childNodes in domDocument

    My crystal ball says that there are some in_array() gymnastics coming my way! Cheers!
  11. Hi guys, Reading this from php.net, has got me a wee bit confused. Trying to implement is has got me doubly confused! My code: $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTMLFile($parent_node); if($dom->childNodes <>0) { $kids = array ( 'url' => $parent_node, 'No_of_kids' => count($dom->childNodes) ); } Results in '' Notice: Object of class DOMNodeList could not be converted to int' How the heck am i supposed to count the childNodes?
  12. ludo1960

    count childNodes in domDocument

    One last question, if i'm crawling a site e.g. index page -> level 1 page -> level 2 page etc (no more child pages after this) How do I know i've reached the end point? Should I expect no. of childnodes = 0 ? Or have I got the wrong end of the stick?
  13. ludo1960

    count childNodes in domDocument

    Thanks for your reply, lots of stuff to read up on, then it's play time
  14. ludo1960

    count childNodes in domDocument

    Hmm, $dom = new DOMDocument; libxml_use_internal_errors(true) ; $dom->loadHTMLFile($parent_node) ; if($dom->childNodes->length <>0) { $kids[] = array ( 'url' => $parent_node, 'No_of_kids' => count($dom->childNodes) ); } echo '<pre>',print_r( $kids ),'</pre>'; Results in: [0] => Array ( [url] => http://mysite.com/test/php/intro.pdo.html [No_of_kids] => 2 ) [1] => Array ( [url] => http://mysite.com/test/php/pdo.setup.html [No_of_kids] => 2 ) [2] => Array ( [url] => http://mysite.com/test/php/pdo.constants.html [No_of_kids] => 2 Pretty sure the answer aint 2 every time, something fishy going on. Any ideas guys?
  15. ludo1960

    count childNodes in domDocument

    Surely you mean this link http://php.net/manual/en/domnodelist.count.php and not the link you sent http://php.net/manual/en/class.domnodelist.php which says the object is countable, but thanks for your non-answer anyway.
  16. Hi guys, Just starting to play with PHP Domdocument, only to fail at the very first step: <?php $html = 'test/php/somefile.html' ; if(!empty($html)){ $dom_1 = new domDocument ; $dom_1->loadHTML($html) ; $links = $dom_1->getElementsByTagName('li') ; foreach ( $links as $link) { // echo $link ; echo $link->nodeValue, PHP_EOL; } } ?> When I visit it in a browser I get a WSOD, what am I missing?
  17. ludo1960

    getting started with domdocument

    oops, all good now. Thanks for pointing me in the right direction, off to play now and I promise to read the manual .
  18. Hi guys, I'm using PHP Simple DOM, thanks to the good folk her I'm making progress. The html i'm parsing has a bunch of links in a li ul structure. I've managed to get the top layer of links extracted and I would like to have a count of the number of child nodes in the layer below the main links. Here is my code: $html = file_get_html('test/php/book.html'); if(!empty($html)){ $lis = $html->find('.chunklist', 0)->children() ; for ( $i = 0 ; $i < count($lis) ; $i++ ) { $parent_term = $lis[$i]->first_child()->innertext . ', ' ; $parent_node = $lis[$i]->children[0]->attr['href'] . '<br>' ; //echo count($parent_node->children()) ; this gives error Warning: count(): Parameter must be an array or an object that implements Countable echo $parent_term . $parent_node ; $parent_node = $const . $parent_node ; echo $parent_node ; $html2 = file_get_html($parent_node) ; $lis2 = $html2->find('.chunklist', 0)->children() ; } } I don't see anything in the manual regarding counting nodes, any idea how to go about this?
  19. ludo1960

    count child nodes in phpsimpledom

    Good guess! Yeah I tried that, but some of the $lis2 don't have children, and I'm not sure how to deal with null results, $lis2 = $html2->find('.chunklist', 0)->children() ; results in: Fatal error: Uncaught Error: Call to a member function children() on null I've tried: for ( $j = 0 ; $j < count($lis2) ; $j++ ) { if ($lis2 > 0) { // and tried $lis2 <> 0 $parent_term = $lis2[$j]->first_child()->innertext . ', ' ; $parent_node = $lis2[$j]->children[0]->attr['href'] . '<br>' ; // echo count( $parent_node ) ; } else { echo "no data found" ; } } kinda stuck as to what to try next?
  20. ludo1960

    array hierarchy and filter

    Thanks again, changed a bit of your code and it works great for ( $i = 0 ; $i < count($li) ; $i++ ) { echo $li[$i]->children[0]->attr['href'] . '<br>' ; //echo $li[$i]->children[0]->children[0]->_[4] . '<br>' ; This was my effort lol! echo $li[$i]->first_child()->innertext } So now I have all I need to construct my associative array ! Great answer! thanks again Maxxd
  21. ludo1960

    array hierarchy and filter

    Hi guys, I'm trying to build an array to replicate the hierarchy in a menu: <ul> <li><a href="file1.html">text1</a></li> <ul> <li><a href="file2.html">text2</a></li> <li><a href="file3.html">text3</a></li> <li><a href="file4.html">text4</a></li> <li><a href="file5.html">text5</a></li> </ul> </ul> An i would like the output to be: "text1" "text1", "text2" "text1", "text3" "text1", "text4" "text1", "text5" Here is my loop to go through the html hierarchy: foreach ($html2->find('ul') as $ul) { foreach ($ul->find('li') as $li) { foreach($li->find('a') as $a) { // need to filter out empty and index.html, tried if(!$->href = 'index.html) {do stuff} but didn't work $links2[] = $a->href ; $taxo2[] = $a->plaintext ; } } } This finds all the links but not the hierarchy, any ideas how to approach this? And also how to filter out blanks and references to index.html?
  22. ludo1960

    array hierarchy and filter

    Yeah, got lots to learn. Wouldn't want it any other way! At least I try, that's got to count for something?
  23. ludo1960

    array hierarchy and filter

    Already tried that: echo $li[$i]->children[0]->["parent"]->["_"]->[1]->[4] . '<br>' ; and echo $li[$i]->children[0]->["parent"]->['_']->[1]->[4] . '<br>' ; and lots of other guesses Thanks for chipping in though!
  24. ludo1960

    array hierarchy and filter

    Trust me I'm trying, $li[$i]->children[0]->attr['href'] is obvious now $li[$i]->children[0]->children[0]->_[4] aint so obvious! I need a Prolific Member.....but I suppose we all do
  25. ludo1960

    array hierarchy and filter

    Halle friggen lujah!!! Am I using the right approach? for ( $i = 0 ; $i < count($li) ; $i++ ) { echo $li[$i]->children[0]->attr['href'] . '<br>' ; //echo $li[$i]->children[0]->children[0] . '<br>' ; } Gets me the child nodes on the page visited, all good and well but I also need the text from the href, it's buried deeper in the array/object: object(simple_html_dom_node)#66 (9) { ["nodetype"]=> int(1) ["tag"]=> string(2) "li" ["attr"]=> array(0) { } ["children"]=> array(1) { [0]=> object(simple_html_dom_node)#67 (9) { ["nodetype"]=> int(1) ["tag"]=> string(1) "a" ["attr"]=> array(1) { ["href"]=> string(21) "pdo.requirements.html" } ["children"]=> array(0) { } ["nodes"]=> array(1) { [0]=> object(simple_html_dom_node)#68 (9) { ["nodetype"]=> int(3) ["tag"]=> string(4) "text" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* ["_"]=> array(1) { [4]=> string(12) "Requirements" } The last bit "Requirements" just after the suspicious looking *RECURSION* I can see now how the objects and arrays work at the top level but how to address the ["_"][4]?
×

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.