473,232 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

Adding fields to an object in PHP 4

Hello,

I have an object that looks like this:

shelterrental Object
(
[connectionError] =>
[changedVals] => Array ( )
[dbh] => Resource id #2
[id] => 1 [shelter] => 1
[renter] => Kevin Shakakhnopolis
[contact] => 504-899-4028
[date] => 2005-04-11
)

I would like to add a field or two under certain circumstances. I have
tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';
$obj['newfield'] = 'zzz';

None of these methods work.

What is the correct syntax to add a field to an object in PHP 4?

TIA!

jg
Jul 17 '05 #1
5 3428
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the initial
object.
JW

Jul 17 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the initial
object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused by
using foreach on the array of objects and in PHP 4 I can't pass reference
like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance data
*/
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}
Jul 17 '05 #3

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:IDg6e.965$H53.255@lakeread05...
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the
initial object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused
by using foreach on the array of objects and in PHP 4 I can't pass
reference like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance data
*/
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}


My tests confirm that my problem is that foreach operates on a copy and i
need to pass by reference or use while().
Jul 17 '05 #4
jerrygarciuh wrote:
Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually
caused by using foreach on the array of objects and in PHP 4 I can't
pass reference like foreach ($arr as &$v).


Passing arguments by reference to foreach would only be a benefit if you
want to modify them.

The problem is probably that you are doing something like:

$obj = buildObj($result, "stdClass");
$obj->foo = 'bar';

This obviously doesn't work, because buildObj isn't returning an object, but
an array of objects.

If you would do something like the following, it should work fine:

$obj[0]->foo = 'bar';
JW

Jul 17 '05 #5

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:rNg6e.967$H53.934@lakeread05...

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:IDg6e.965$H53.255@lakeread05...
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';
Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the
initial object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused
by using foreach on the array of objects and in PHP 4 I can't pass
reference like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance
data */
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}


My tests confirm that my problem is that foreach operates on a copy and i
need to pass by reference or use while().


function forDate($date) { // $date = Y-m-d
$shelters = $this->retrieve("facility = '$_SESSION[facility]'");
$srObj = new ShelterRental;
if ($shelters) {
while ($s = current($shelters) ){
// retrieve today's rentals by shelter
$where = array("date = '$date'", "shelter = '$s->id'");
$rentals = $srObj->retrieve($where);
if ($rentals) {
echo $s->id;
foreach ($rentals as $r) {
$s->renter = $r->renter;
$s->contact = $r->contact;
$s->rentalid = $r->id;
} // foreach
} // if
$rentalInfo[] = $s;
next($shelters);
} // while
} // if
return $rentalInfo;
} // end forDate()
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
2
by: Robin S. | last post by:
This is an "Add product" form. The user will enter a ProductNo (catalog number), select a Product Class (from cascading combo boxes) and then click a button to create the product. When a...
2
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a...
0
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
12
by: Art | last post by:
Hi everyone I was hoping someone might be able to help me with this. I'm just starting to try to work with MS Access tables through VB.net. In Access I can take an existing table and add a new...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
5
by: Kosmos | last post by:
I have traveled the world and the seven seas and I have yet to come up with an answer to this question.... So I'm adding an attachment to an email from access... The following is the code: ...
1
by: vivekvt | last post by:
-------------------------------------------------------------------------------- here is the code to the asp page that adds data to the access database! <html> <body> <% Dim recordsaffected
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.