Jump to content

Recommended Posts

Posted

Hi,

 

I'm quite new to PHP and experiencing some issues.

 

I understand programming in general as I have been using ASP for quite some time.

 

I am trying to get a record from a database using an sql lookup (sql1). This then returns as an array which is fine, but I need to use part of the array for my next stage.

 

My question is, does anyone know how to remove a fields value from the array?

 

My code is:

$opt=get_records_sql($sql1);

//Diags for SQL content
	print_object($opt);

$n = count($opt);
if (empty($opt))
	{
	echo 'No options selected';
	}
else
	{
	
	$optno = $opt["subjectid"];
	
	// Diags of $optno
		echo '
$optno = '.$optno;

 

As you can see, I tried to use this: $opt["subjectid"] as subjectid is the fieldname that I am trying to access and I was under the impression that this was correct for accessing an array, but I get the following error:

 

Notice: Undefined index: subjectid

 

Any thoughts would be much appreciated.

 

Thanks,

 

Barry.

Posted

Hi,

 

The lookup returned an array automatically.

 

I need access to several fields and to cross reference against another database for each record that gets returned.

 

For this reason I need the array broken into each field so that I can run the cross reference and then post the result before going on to the next record in the results.

 

I know how to do it in ASP, but not in PHP.

 

Thanks.

Posted

Not quite sure what you're doing, but $opt["subjectid"] is looking for the item in the array called subjectid

 

eg you can do:

$opt["chemistry"]=1;

or

$opt["physics"]=0;

 

if you've got an array $opt[] which has items corresponding to option subjects and values to go with them (perhaps ticked yes/no)

Posted

The array contains a field subjectid which will contain a number that will be cross referenced against a second database to find out which subject name goes against that number.

 

When I run the page, it prints the array contents:

Array
(
   [1] => stdClass Object
       (
           [uname] => JHollands06
           [tutor] => M LSt
           [subjectid] => 1
           [year] => 2010
           [optid] => 1
       )

)

this shows me that the subjectid field on this record contains a number 1, but I get an error when I try and lookup that specific field in order to find out which number it contains.

Posted

When I run the page, it prints the array contents:

Array
(
   [1] => stdClass Object
       (
           [uname] => JHollands06
           [tutor] => M LSt
           [subjectid] => 1
           [year] => 2010
           [optid] => 1
       )

)

 

It's because the array contains all the records returned by the query, not just "a" record. You need

$opt[1]["subjectid"].

Posted
You could use:

 

extract($opt,EXTR_PREFIX_ALL,'field');

 

This will create $field_1, $field_2, $field_3 variables. $field_3 would be your subjectid

 

Except that $opt is an array containing one object, which is also an array, so the return will be an array called $field_1.

Posted
It's because the array contains all the records returned by the query, not just "a" record. You need

$opt[1]["subjectid"].

 

Thanks guys for your assistance.

 

I did as was suggested above and got teh following error:

 

Fatal error: Cannot use object of type stdClass as array

 

Any thoughts?

 

Thanks.

Posted
Try this:

 

foreach($opt as $row) {

echo $row['subjectid'];

}

 

I changed it as you suggested, but got exactly the same error.

 

Could it have something to do with the fact that the stdClass Object is returned within the array? I don't know, just guessing! :confused:

 

Cheers for your help and ideas.

Posted

Paste the code where you retrieve the data. You're obviously using a library somewhere along the way, and you're getting an object back not just a plain array.

 

A PEAR SQL library by any chance?

Posted

Well spotted Powdarrmonkey,

 

I'm using a library supplied with a module for Moodle VLE.

 

$opt=get_records_sql($sql1);

 

If this is the cause of my problems, then I will take it up with the author of the module.

 

Let me know if you want me to print the code of the 'get_records_sql'.

 

Thanks.

Posted

Thanks to everyone on here who offered me help with this.

 

I have managed to work out how to do it.

 

The code I needed was:

foreach($opt as $row) {
$optno = $row->subjectid;
}

 

Hope that helps someone else at some point.

 

Barry

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...