NotionCommotion
-
Posts
2,446 -
Joined
-
Last visited
-
Days Won
10
Community Answers
-
NotionCommotion's post in Curl Request Append Response was marked as the answer
Okay, $textAr includes URLs for www.google.com\r and www.yahoo.com. Do you really want the \r with Google?
Then I see $line: www.google.com being echoed, but don't see the results being echoed. Replace print_r with var_dump as it likely is returning NULL.
Then I see $line: www.yahoo.com followed by the error message. I would have expected a line break but not a big deal.
Then I see the $results array with the first element being empty and the second being the error response.
Isn't this what you were expecting? Consider changing $result[] = $r; to $result[$line] = $r; to make it more clear.
-
NotionCommotion's post in Troubleshooting Segmentation fault was marked as the answer
As seen by the first line of my original post, I am using a debugger dbg-php-7.1.so. Disabled it, and now I see: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes). Okay, not quite the mystery anymore.
-
NotionCommotion's post in Validate a stdClass object against a blueprint or prototype was marked as the answer
I am sure it can be improved, but for what it is worth, this is what I will be doing.
<?php function validate($input,$blueprint,$level='base',$errors=[]){ /*Only validates types string, integer, double and boolean, object (stdClass only), and arrays but not resources. Arrays can not contain other arrays or objects, and all elements in the arrays must be of the same type Objects are validated by specifying an associated array for the object's attribute name and attribute type. */ if(!is_object($input)) return ['validate method must be passed an object']; $input=(array) $input; if($missing=array_diff_key($blueprint,$input)) { $errors[]="'$level' object is missing properties '".implode(', ',array_flip($missing))."'."; } foreach($input as $prop=>$value){ if(empty($blueprint[$prop])) $errors[]="Unexpected property '$prop' provided in the '$level' object."; else { $type=gettype($value); $typDesired=$blueprint[$prop]; if(gettype($blueprint[$prop])=='array') { $isAssoc=$blueprint[$prop]===[]?false:array_keys($blueprint[$prop]) !== range(0, count($blueprint[$prop]) - 1); $typDesired=$isAssoc?'object':'array'; } if( $type!=$typDesired) $errors[]="Property '$prop' in the '$level' object is a $type but should be a $typDesired."; elseif($type=='object') $errors=validate($value,$blueprint[$prop],"$level.$prop",$errors); elseif($type=='array') { foreach($value as $item) { if( gettype($item)!=$blueprint[$prop][0]) { $errors[]="At least one element of the '$prop' array in the '$level' object has the incorrect type and all must be a ".$blueprint[$prop][0].'.'; break; } } } } } return $errors; } $json='{"method":"maintenance.Tshark","params":{"paramString":" --guid d6f23460-0d77-400e-ae96-13f436e40245 --upload-server-ip 74.208.80.161 --upload-server-port 22 --upload-server-user bacnet --params -f \'port 1337 or port 47808\' -i eth0 -a duration:5"},"extra":{"name":"filename","test":[1,4,7]}}'; $data=json_decode($json); $blueprint=['method'=>'string','params'=>['paramString'=>'stringx'],'extra'=>['nasme'=>'string','test'=>['string']]]; echo('<pre>'.print_r(validate($data,$blueprint),1).'</pre>'); Array ( [0] => Property 'paramString' in the 'base.params' object is a string but should be a stringx. [1] => 'base.extra' object is missing properties 'nasme'. [2] => Unexpected property 'name' provided in the 'base.extra' object. [3] => At least one element of the 'test' array in the 'base.extra' object has the incorrect type and all must be a string. ) -
NotionCommotion's post in Can't open sqlite db was marked as the answer
What a difference a space makes!
$x=new PDO('sqlite:db.sqlite3'); -
NotionCommotion's post in Help! Parse error: syntax error, unexpected was marked as the answer
Your missing your trailing semicolon. Ps. You don’t need one in your query.
-
NotionCommotion's post in How to restrict mySQL results to single instance after using mb_substr was marked as the answer
Instead of having PHP do the trimming, have MySQL do it. See http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_left.
Do, why won't DISTINCT or GROUP BY work?. Are there records like ABCfoo and ABCbar? Maybe the following?
SELECT DISTINCT LEFT(order_discount_code,3) FROM orders ORDER BY order_discount_code ASC; -
NotionCommotion's post in How to Display Individual Tables in Dropdowns from SQL? was marked as the answer
Not sure I understand your question, so forgive me if you already know this.
Every time the browser makes a request to the server, it should include the region name in the URL (maybe use a default region name if none provided, and be sure to use isset() or something to make sure it exists). You then get the data based on the region name, pass it to a template, and display it.
$stmt = $conn->prepare('SELECT x,y,z FROM yourTable WHERE Region_Name=:Region_Name'); $stmt->execute(['Region_Name'=>$_GET['Region_Name']]); $rs=$stmt->fetch(); yourTemplate($rs); -
NotionCommotion's post in How do I extract partial string from a full string? was marked as the answer
Maybe?
<?php function getIt($var) { $pos = strpos($var, ' - '); echo(substr($var, $pos+3)."\n"); } $variable_1 = 'Lulu-free - Swimsuit swimwear red'; $variable_2 = 'Pap & Jones - Bard Mini Dress'; getIt($variable_1); getIt($variable_2); -
NotionCommotion's post in Preventing Twig from escaping JSON was marked as the answer
Ah, I see.
{% set _jsScript = [ 'var types='~types|json_encode()~';' ] %} {% macro listArray(list) %} {% for item in list %} {{ item|raw }} {% endfor %} {% endmacro %} {% if _jsScript|default %} <script type="text/javascript"> {{ forms.listArray(_jsScript) }} </script> {% endif %} -
NotionCommotion's post in PHP string adding extra forward slash was marked as the answer
Yea, that was the reason I added "EDIT. Please disregard (and hopefully delete this post), I am just blind." to the original post.
-
NotionCommotion's post in security related was marked as the answer
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. -
NotionCommotion's post in Array Inside Multidimensional Array was marked as the answer
Try the following. Also, experiment with using $key=>$value in your foreach loops.
<?php $array=[ 'catalog'=>[ [ 'attributes'=>['book'=>20160122], 'section'=>[ ['id'=>'F100','title'=>'Across the Sea'], ['id'=>'F101','title'=>'Blue Water'], ['id'=>'F102','title'=>'Red Rove'] ] ], [ 'attributes'=>['book'=>20160123], 'section'=>[ ['id'=>'F103','title'=>'xAcross the Sea'], ['id'=>'F104','title'=>'xBlue Water'], ['id'=>'F105','title'=>'xRed Rove'] ] ], ]]; foreach($array['catalog'] as $catalog){ echo('Book: '.$catalog['attributes']['book']."\n"); foreach($catalog['section'] as $section){ echo($section['id'].' '.$section['title']."\n"); } echo("\n"); } ?> -
NotionCommotion's post in Character issues when using DOMDocument was marked as the answer
Solved by using $message = mb_convert_encoding($message, 'HTML-ENTITIES', 'UTF-8');
Note that $message= str_replace(' ', ' ', $message); is also no longer needed.
-
NotionCommotion's post in How would you check if a variable was submitted into a field then submit it to the PDO statement? was marked as the answer
Maybe something like the following...
<?php $data=array(); $query=''; foreach(array('name','phone') as $key) { if(isset($_POST[$key]) && $_POST[$key]!=''){ $data[$key]=$_POST[$key]; $query.="$key=:$key,"; } } if(!empty($data)){ $sql = 'UPDATE myTable SET '.trim($query, ",").' WHERE id=:id'; $data['id']=123; $q = $conn->prepare($sql); $q->execute($data); }