NotionCommotion
Members-
Posts
2,446 -
Joined
-
Last visited
-
Days Won
10
Everything posted by NotionCommotion
-
Validating HTML and counting elements
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thank you, yes, it seems to help. I've used the DOMDocument class in the past, but never simplexml_load_string(), and need to spend a little more time playing around with it. -
Given string $html, how can I: 1) Tell that it is valid HTML? 2) Count p.p for each given data-id (i.e. 1=1, 2=2, 3=1) $html = <<<EOD <p>Hello</p> <div> <p class="p" data-id="3"></p> <p class="p" data-id="2"></p> </div> <div> <p class="p" data-id="1"></p> <div> <p class="p" data-id="2"></p> </div> </div> EOD;
-
Mimicking autoincrement primary key using PHP
NotionCommotion replied to NotionCommotion's topic in MySQL Help
Thanks requinix, Right now I have the stored in multiple places and am using a 1-to-1 fk. Had me stumped with the order of the keys. Maybe go back to a stored procedure. I've never attempted to lock a table before, but suppose it is time to learn. -
Mimicking autoincrement primary key using PHP
NotionCommotion replied to NotionCommotion's topic in MySQL Help
Okay, this question really belongs in the MySQL forum, but it is just a carry on from above. I've always thought MyISAM supported composite primary keys, but have never tried it. Well, I tried it, and it doesn't work. What am I doing wrong? mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | accounts | | checks | +----------------+ 2 rows in set (0.00 sec) mysql> EXPLAIN accounts; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | +-------+---------+------+-----+---------+----------------+ 1 row in set (0.01 sec) mysql> EXPLAIN checks; +-------------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | accounts_id | int(11) | NO | PRI | NULL | | +-------------+---------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> SHOW CREATE TABLE accounts; +----------+----------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+----------------------------------------------------------------------------------------------------------------------------------------------+ | accounts | CREATE TABLE `accounts` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 | +----------+----------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> SHOW CREATE TABLE checks; +--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | checks | CREATE TABLE `checks` ( `id` int(11) NOT NULL AUTO_INCREMENT, `accounts_id` int(11) NOT NULL, PRIMARY KEY (`id`,`accounts_id`), KEY `fk_checks_accounts_idx` (`accounts_id`) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 | +--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> INSERT INTO accounts(id) VALUES(0); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO accounts(id) VALUES(0); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO accounts(id) VALUES(0); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM accounts; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(1); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(1); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(2); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(2); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(1); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO checks(accounts_id) VALUES(3); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM checks; +----+-------------+ | id | accounts_id | +----+-------------+ | 1 | 1 | | 2 | 1 | | 3 | 2 | | 4 | 2 | | 5 | 1 | | 6 | 3 | +----+-------------+ 6 rows in set (0.00 sec) mysql> -
Mimicking autoincrement primary key using PHP
NotionCommotion replied to NotionCommotion's topic in MySQL Help
Hey, you just stole my solution of using a separate MyISAM table to generate the IDs! I actually started to reply to your post to say thank you for giving me the clue when you brought up MyISAM, and see you spelled it out. For my case, there are many more queries than inserts, so I think it is acceptable. Trigger won't return the inserted key. Stored procedures could, but not 100% guaranteed. Thanks! -
Mimicking autoincrement primary key using PHP
NotionCommotion replied to NotionCommotion's topic in MySQL Help
Yes, I understand your concerns, but it seems to be proper sometimes. For instance, let's say you had table accounts and table checks. checks would then have a primary key which contained both the accounts primary key plus an auto-incrementing primary key. Why is this bad? EDIT. the checks auto-incrementing key effectively becomes a natural key which can bring performance benefits. -
I am trying to implement a composite primary key with MySQL InnoDB where one of the keys is incremental relative to the other. For instance, the below table (PS. How can I make it look better?) shows id1 as the incrementing column and ad2 as the non-incrementing column. SQL's auto_increment won't work as it produces a single series and not an individual series relative to the other key. I've looked into stored procedures and triggers, however, I don't believe they can solve the problem. They can atomically insert an incrementing id, however, there appears to be no way for PHP to retrieve the new ID and be guaranteed that it is correct due to concurrency/consistency issues. Can this be solved with brute force PHP? id1 id2 Data 1 1 bla 2 1 bla 3 1 bla 1 2 bla 2 2 bla 4 1 bla 1 3 bla 3 2 bla
-
PHP string adding extra forward slash
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Yea, that was the reason I added "EDIT. Please disregard (and hopefully delete this post), I am just blind." to the original post. -
EDIT. Please disregard (and hopefully delete this post), I am just blind. I am logging data using Monolog. The following command: $this->logger->addInfo($this->server['ip'].' xxx '.$command.' xxx '."{$this->server['ip']}/$command"); produces the following log record: [2016-07-07 06:47:05] my_logger.INFO: http://sub.example.com xxx /task xxx http://sub.example.com//task [] [] What is causing the second forward slash between {$this->server['ip']} and /$command
-
Preventing main file functions from being global
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks Jacques1, Will go for the separate class approach, but will consider the closure solution when it makes sense. -
Preventing main file functions from being global
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
I always have a single file index.php which does a little work, creates an object from a class, and does something. Sometimes a function in that file will make the code more concise, however, felt that it shouldn't be global. -
How can I add a function to my main PHP file, yet not let it be available withing a class? For instance, prevent baseFunction() from being available within foo. <?php class foo{ public function bar() { echo(baseFunction()); //Global echo($baseVariable); //Not global } } function baseFunction() {return 'baseFunction';} $baseVariable='baseVariable'; $foo=new foo(); $foo->bar();
-
That was the part I wasn't sure about. Thanks!
-
This was the part I wasn't sure about. Guess it is a mute point. What about the ternary $k = $k == $from ? $k = $to : $k;?
-
Note sure if this will work with the nested arrays, but I am interested on how it works. I've never done references within references. Let me make sure I know what is going on. Send $arr to changeKeyName as a reference, so I am really working on the array outside of the function. Pass $k as a reference to the foreach loop, so I am really working on the $k outside of the foreach loop and therefore working on the key of $arr outside of the function? And then, if $k==$from, set $k=$to within the ternary and set $k equal to the new $k outside of the ternary?
-
The PHP webserver retrieves the data from another server's API using cURL as [['id'=>123,'name'='hello]...], and needs to send it to the browser client as [['id'=>123,'label='hello]...] and will be used by jQueryUI's autocomplete plugin. The key name "name" always needs to change to "label".
-
Other than manually iterating over $arr1, and slick ways to change it to $arr2? Thanks $arr1= [ ['a'=>123,'b'=321], ['a'=>222,'b'=234], ['a'=>321,'b'=123] ]; $arr2= [ ['c'=>123,'b'=321], ['c'=>222,'b'=234], ['c'=>321,'b'=123] ]
-
Bummer. I was hoping I could be more lazy. Thanks!
-
I wish to urlencode part of a URL, and can do the following: sendIt("/cars/".urlencode("Model T")); function sendIt($u) { $ch = curl_init("http://forums.phpfreaks.com{$u}"); } Instead, I wish to put the urlencode statement in the function as follows. sendIt("/cars/Model T"); function sendIt($u) { $ch = curl_init("http://forums.phpfreaks.com".urlencode($u)); } Problem is that it encodes the forward slashes. How can this be done?
-
When is http_response_code() ever needed?
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
So, I just do http_response_code(400); and call it good? Thanks -
http://php.net/manual/en/function.http-response-code.php will Get or Set the HTTP response code. Why would one ever need to do so? The documentation appears to just return an integer. Does it send headers as well? Guess that would be nice so I don't need to create them myself such as "HTTP/1.1 200 OK". I would like to leave the default at 200, but only do differently explicitly. But doesn't it change it for future?
-
What are you talking about? This is the new and improved Jaques1! Now, maybe a few years ago... Joking aside, go with his suggestion to learn PDO and prepared statements.
-
It is good that you are aware of real_escape_string(), however, no one uses it anymore. Instead, use a prepared statement ideally PDO. While there is fancy binding and the like, start off simple. $username = $_SESSION[info]['username']; $query = "SELECT $column FROM $table WHERE username =? limit 1"; //Why not hard code your column and table names? $stmt->$yourDataBaseConnection->prepare($query); $stmt->execute([$username]); $rs=$stmt->fetch(); //Or fetchAll() or fetchColumn(). Also, add parameters such as PDO::FETCH_OBJ.
-
Looks like you example was not meant to be implemented, but only to educate yourself, true? While print_r() is helpful to quickly view data, var_dump() gives you more information. Try the following, you will will see what is going on $_get = array_merge($_get, $_GET); var_dump($_get);
-
Thanks Psycho, That is what I was thinking, but wasn't sure.