Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Ok, this is a really, really newbie question, but I've googled and tried several different way of doing this with no luck. My first time making a class in PHP. I'm making a simple class that will query an e-mail to a DataBase and it will return if the e-mail exists in that database or not. Here is how I made the class.
<?php
class isEmailUnique{
private $email;
include '../dbc/DBC.php';
$collection = $db->members;
$ifEmailRaw = $collection->find(array('eMail' => $email));
$ifEmail = $ifEmailRaw -> count();
if($ifEmail > 0){
return false;
}
else{
return true;
}
}
?>
Here is how I am using the class:
<?php
include 'classes/checkEmailIsUnique.php';
echo isEmailUnique->'text#test.com';
?>
Here is my error:
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' in /var/www/partycloud.tv/public_html/php/test.php on line 5
My desired result would that it returns either 1 or 0.
I know this is probably very easy, I just don't get it how to construct a class, I'm having a very difficult time comprehending the few articles I've read. Any help with this would be awesome! Thanks for taking the time to read this!
That's not how classes work. Here's an example of what an OOP version of your code could look like:
<?php
class EmailHelper {
private $db;
public function __construct() {
include '../dbc/DBC.php';
$this->db = new DBC();
}
public function IsUnique($email) {
$collection = $this->db->members;
$ifEmailRaw = $collection->find(array('eMail' => $email));
$ifEmail = $ifEmailRaw->count();
return ($ifEmail <= 0);
}
public function IsValid($email) {
/* Logic */
}
public function IsFromDomain($email, $domain) {
/* Logic */
}
}
$emailHelper = new EmailHelper();
if ($emailHelper->IsUnique('text#test.com')) {
echo 'Unique!';
}
?>
Your definition is kind of an (illegal) hybrid of a class and a function. Look up how to define a member function within a class. As a hint, the line that is erroring is going to have to look more something like this, well, as your class is now defined:
echo isEmailUnique->check('text#test.com');
...where check() is the member function you define. Probably you mean something more like this:
echo whateverYourClassNameIs->isEmailUnique('text#test.com');
...where your current class name is really meant to be a function name within the class...
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am not able understand below mentioned code. in if conditional both assignment and negate operators are used. if anyone explains me this i will be very much thankful to you.
<?php
function foo() {
return mysqli_connect('localhost', 'username', 'password', 'dbname');
}
function start(){
if(!$con=foo()){
exit();
}
return $con;
}
?>
In php we don't initialize functions like variables. We can define function in this way
function foo() {
$connection = mysqli_connect('localhost', 'username', 'password', 'dbname');
return $connection; // it will return true or false on the basis of mysqli_connect() function
}
if(!$a =foo()) { //The negate in if tells us if it is not returning true
//code goes here
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hi guys i'm having a small issue when i try and convert my exisiting script into a simple function. I'll leave my code below.
PHP:
function service_check($postcode) {
$op_postcodes = array(... "DG10","DG3","DG4" ...);
if(isset(htmlentities($postcode))) {
$postcode_checker = trim(strtoupper(htmlentities($postcode)));
$trim_postcode = trim(substr(htmlentities($postcode_checker, 0, -3)));
if(empty($postcode_checker)) {
$error = "We require your postcode to check our service in your area.";
} else if(!valid_postcode($postcode_checker)) {
$otp = "The postcode you entered is invalid.";
} else if(!in_array($trim_postcode, $op_postcodes)) {
$otp = "Sorry, but we don't provide our service's in your area, just yet.";
} else {
$otp = "Great news! We're in your area and you are eligible to order our services!";
$_SESSION['customer_postcode'] = $postcode_checker;
}
} else {
$otp = "To get started please enter your postcode.";
}
}
My current usage of the function is <?php service_check($_POST['service_check']); ?>
My error is:
Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in /home/domain.com/public_html/controller/application/functions/locale.SM.php on line 27
change this
if(isset(htmlentities($postcode))) {
to this
$pc = htmlentities($postcode);
if(isset($pc)) {
read this if you get sometime - http://php.net/manual/en/function.isset.php http://php.net/manual/en/function.htmlentities.php
Since your questions wasn't complete i assume so now an edit. The better approach could be to use !empty() instead of isset() on our if condition.
Even better, reove htmlentities method call from your if block and then later use html entities when you actually need it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can i do something like this?
class record
{
public $count;
}
$i = 0;
foreach($entry as $item) {
$i++;
$record$i = new record();
$record$i->count = $item['count'];
print $page$i;
}
Specifically, this part here is giving me errors.
$record$i = new record();
Note: the loop works fine if i just do print $item['count']
The error is: Parse error: syntax error, unexpected T_VARIABLE
see http://3v4l.org/lB4sR
${'record'.$i};
you can create a string that holds a varname and use it as a variable later - see the example
**Edit: but like #h2ooooooo sais in the comment to your question: use an array.
Hope is that what you asking, but I do not understand why to use such bad methods to count...
class record
{
static $countq = 0;
public function count(){
self::$countq++;
}
}
$entry = array(1, 4, 6, 7);
$obj = new record();
foreach($entry as $item) {
$obj->count();
}
echo record::$countq;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am setting up a web API, which I'm sure can be done more efficiently, but this is v0.1. The first step is visiting localhost/serverList/api/rest.php?action=allServers&format=xml. This begins the below chain. I've removed the non relevant parts of the code so that this question is shorter
serverList/api/rest.php
<?php
include 'inc/restFunctions.php';
//several lines of code removed. $functionName = allserversxml
if(in_array($action,$possibleActions)){
if(in_array($format,$possibleFormats)){
$functionName = $action.$format;
$result = $functionName();
header('Content-type: text/xml');
$return->flush();
}
}
?>
serverList/api/inc/restFunctions.php
<?php
function getArrayOfFieldNames($queryResults){
$fieldList = array();
while($finfo = $queryResults->fetch_field()){
$fieldName = $finfo->name;
array_push($fieldList, $fieldName);
}
return $fieldList;
}
function getXMLofQuery($queryResults,$xmlTitle){
$fieldList = getArrayOfFieldNames($queryResults);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$title = $xmlTitle;
$titlePlural = $xmlTitle."s";
$xml->startElement($titlePlural);
$fieldIDName = $title."ID";
while($row = $queryResults->fetch_assoc()){
$xml->startElement($title);
$xml->writeAttribute('id', $row[$fieldIDName]);
foreach($fieldList as $field){
$xml->startElement($field);
$xml->writeRaw($row[$field]);
$xml->endElement();
}
$xml->endElement();
}
$xml->endElement();
return $xml;
}
function allserversxml(){
global $link; //from config.php file
$allServerResults = $link->query("SELECT * FROM servers");
$xml = getXMLofQuery($allServerResults,"server");
return $xml;
}
?>
The problem is that when I go to the URL, I get the error error on line 2 at column 1: Extra content at the end of the document. Below is a rendering of the page up to the first error.
Yet...below there is no rendering. What gives?
EDIT: as per ndm's recommendation, I was able to get the error via the source of the page.
Call to a member function flush() on a non-object in C:\path\serverList\api\rest.php on line 29
So I suppose my question would be then, what is the best way to display xml on a page when it is returned from a function?
As far as I can tell from the error message and the code, assuming that "removing non relevant code parts" does not include removing code from the posted functions and the logic flow, it looks like that the variable you'd want to invoke flush() on should be $result instead of $return.
...
$result = $functionName();
header('Content-type: text/xml');
$result->flush(); // like this
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a problem with my code. I have code like this:
<?php
include('php/SelectHistory.php');
include('php/SelectSmallHistoryUser.php');
include('php/SelectSmallHistoryProject.php');
include('php/SelectSmallHistoryFunctionality.php');
$newHistoryRow = SelectHistory();
echo "<table width='100%'>";
if (count($newHistoryRow) > 0)
{
foreach ($newHistoryRow as $current)
{
$chosenUser = SelectSmallHistoryUser($current->userID);
$chosenProject = SelectSmallHistoryProject($current->projectID);
$chosenFunctionality = SelectSmallHistoryFunctionality($current->functionalityID);
echo "<tr>";
echo "<td>" . $chosenUser->fullName . " was busy with " . $chosenFunctionality->functionalityName . " on " . $chosenProject->projectName . " at " . $current->lastModifiedDate;
echo "</tr>";
unset($chosenUser);
unset($chosenProject);
unset($chosenFunctionality);
}
}
else
{
echo "<tr><td>No History To Display.</td></tr>";
}
echo "</table>";
?>
The problem that I have with it is that within the loop, it declares a method which resides in a class. Now because it is working with data from a database, if the amount of things is more than one, I get a "Class already declared" error.
Is there a way I can fix this or is there another method I can use?
This is the right way of creating class :
class TestClass {
public $property;
//some other properties
function __construct($id) {
$this->property=$id;
//do some other stuff
}
//some other functions
}
And the right way of creating an instance of class is :
$test = new TestClass($id);
Mind the __construct() function and the new keyword and try executing your script again.
Make a new one i guess. $var = new SelectHistory();
Rather than creating object like above you should do is, define a static function to get the data you want. You would be able to call the function as below without creating an object.
SelectSmallHistoryUser::getData($current->userID);
Creating objects withing a loop is not acceptable. Read more
Declaring or including classes in a loop is not good.
But check this : http://www.php.net/manual/en/function.class-exists.php