i have this error displayed while working on my php code
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
on this line
if(isset('firstname','lastname','username','password','email'
Would appreciate if anyone can help.
You forget the variable name. In isset has to be variable(s), not strings. If I take an example with testing keys in POST, it should looks like:
if (isset($_POST['firstname'], $_POST['lastname'], ...)) {
//
}
Generally with $array, $_POST can looks like something magic:
if (isset($array['firstname'], $array['lastname'], ...)) {
//
}
Or you maybe want to test if variables exist, not keys in array. Then the code will be:
if (isset($firstname, $lastname, ...)) {
//
}
Related
I have an object of $person as below:
$person = Person::where('id', $id)->first();
According to which $person exists or not I load other data:
if($person) {
$person->family_members = FamilyMemberController::FamilyMemberOf($person->id);
} else {
$person->family_members = [];
}
In the view file, I check the $person->family_members if not empty and exists to add a generated value :
if(!empty(array_filter($person->family_members))) {
// my code
}
But it throws an error:
array_filter(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given
I need to check this $person->family_members to make sure whether it's an array or a collection is not empty.
Writing code for if array do something if collection do something is the wrong way of implementation.
You can do two things.
use both returns as collection()
or either use both returns as an array[]
If collection
else {
$person->family_members = collect();
}
If array
use ->toArray() at the end of Eloquent. Check this answer
As well, I think you are confused with array_filter(). Maybe you are searching for in_array() or contains()
Use count method
if(count($person->family_members)>0){
//your code
}
We don't know your code, but given the error, it's safe to assume your method returns a Collection. You can see available methods in the docs. However, if you need it as array, all you need to do is call ->toArray() on the result Collection. Then you can use array_filter.
What about just doing
if(!$person->family_members){
// your code here
}
or
if($person->family_members){
// your code here
} else {
// code of "if it's empty" goes here
}
You can use the count() function to return a count of an index. ex
if(count($person->family_members)){
//do your true algo
}
Why you are using the empty method ?! Try this:
$person->family_members = $person ? FamilyMemberController::FamilyMemberOf($person->id) : null;
if($person->family_members){ // your code }
Try simple ways in Programming ;)
Alternative title: how does one "nicely" "update" the contents of text files with Kotlin?
I have seen how do i write to a file in kotlin.
Basically, the suggested solution there is based on creating a File object, to then write to it: File("somefile.txt").printWriter().use ...
But well: I already have code like this:
File(fname).bufferedReader().lineSequence().flatMap { transform(it) }
where transform() is my own method that either returns the original line, or some updates lines when some specific match matched. (its signature is transform(line : String) : Sequence<String>)
Sure: I could assign the result of flatMap() to some temporary variable, to then use that with printWriter().use.
But is there a nice, canonical way that works without a temporary variable?
Note that your reading code (File().bufferedReader().lineSequence()) can already be simplified to:
File(fname).useLines {
it.flatMap(::transform)
}
Now regarding writing without putting it in an intermediate variable...
File("writeTo").bufferedWriter().use { writer ->
File("readFrom").useLines { lines ->
lines.flatMap(::transform)
.forEach {
writer.appendln(it)
}
}
}
But I am not yet really convinced about it... I don't really like it ;-)
I rather like something like:
File("readFrom").useLines {
..
}.writeTo("writeTo")
but that is not going to work as long as I use useLines ;-)
But here is something similar:
File("readFrom").useLines {
it.flatMap(::transform)
.writeTo(File("writeTo"))
}
But this requires the following extension function then:
fun Sequence<String>.writeTo(file: File) {
file.bufferedWriter().use { writer -> forEach { writer.appendln(it) } }
}
On the other hand... I always wanted to have a sequence.toFile-writer ;-) adapted it already to use a delimiter as parameter...
I'm trying to delete a database using the postgres driver (lib/pq) by doing a:
db.Exec("DROP DATABASE dbName;")
But I'd like to do a different conditional based on whether the error received is something strange, or is a "database does not exist" error.
Is there a constant variable or something I can use to check if the error returned is a "database does not exist" error message, or would I have to manually parse the error string myself?
I tried to look in the documentation, but could not find anything for "database does not exist". I did however find this list.
Perhaps it fits under some other error code? Also I'm not quite sure the semantically correct way of fetching and comparing the error codes through the Postgres driver. I presume I should do something like this:
if err.ErrorCode != "xxx"
The lib/pq package may return errors of type *pq.Error, which is a struct. If it does, you may use all its fields to inspect for details of the error.
This is how it can be done:
if err, ok := err.(*pq.Error); ok {
// Here err is of type *pq.Error, you may inspect all its fields, e.g.:
fmt.Println("pq error:", err.Code.Name())
}
pq.Error has the following fields:
type Error struct {
Severity string
Code ErrorCode
Message string
Detail string
Hint string
Position string
InternalPosition string
InternalQuery string
Where string
Schema string
Table string
Column string
DataTypeName string
Constraint string
File string
Line string
Routine string
}
The meaning and possible values of these fields are Postres specific and the full list can be found here: Error and Notice Message Fields
You could use this: https://github.com/omeid/pgerror
It has lots of mappings for various postgres errors.
With the package, you can do the following (taken from the README):
// example use:
_, err = stmt.Exec(SomeInsertStateMent, params...)
if err != nil {
if e := pgerror.UniqueViolation(err); e != nil {
// you can use e here to check the fields et al
return SomeThingAlreadyExists
}
return err // other cases.
}
This package has all the PG error constants:
https://github.com/jackc/pgerrcode
Just import and you're good to go:
import "github.com/jackc/pgerrcode"
// ...
if err, ok := err.(*pq.Error); ok {
if err.Code == pgerrcode.UniqueViolation {
return fmt.Errorf("unique field violation on column %s", err.Column)
}
}
The package is also in the family of one of the 2 or 3 most popular Go PostgreSQL drivers, called "pgx", so it should be reliable enough.
My question is this with an example from the parse docs:
You have in parse the column with all kind of types, like string, array, etc.
Now I have the following kind of code:
var gameScore = PFObject(className:"GameScore")
gameScore["score"] = 1337
gameScore["playerName"] = "Sean Plott"
gameScore.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The object has been saved.
} else {
// There was a problem, check error.description
}
}
Ok now I want to save another score behind the first score: 1337.
So when I retrieve from parse it will look like this: Sean: 1337, 1207.
Or something like that when I retrieve the name and scores. I have in parse it already set to be an array.
So my question is: How do I add a string to an array in parse.
Thank you beforehand.
You need "score" to be an Array in your DB. Then in your code, you do this:
gameScore.addObject([1337], forKey:"score")
I want to get rid of the xml declaration when I use libxml2's function xmlSaveFile. How can I accomplish that ?
Is there any macro to do that or can I use another function (I tried with xmlSaveToFilename from xmlsave.h but I do not know how it works) ?
Something like this should work:
xmlSaveCtxtPtr saveCtxt = xmlSaveToFilename(filename, NULL, XML_SAVE_NO_DECL);
if (saveCtxt == NULL) {
// Error handling
}
if (xmlSaveDoc(saveCtxt, doc) < 0) {
// Error handling
}
xmlSaveClose(saveCtxt);
The documentation of the xmlsave module can be found here.