p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: what is a scalar value?


Message #1 by "Juliet May" <jmay@s...> on Tue, 7 May 2002 17:14:45 -0600
This is a multi-part message in MIME format.

------=_NextPart_000_0043_01C1F5EA.AF650470
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

What should I be looking for when I get the following error message?

  Warning: Cannot use a scalar value as an array in 
C:\Inetpub\wwwroot\agtrac\select_protection.php on line 126
  

I'm more curious than worried because the portion of the script that it 
is referencing isn't important for the page where it is included . I 
know I should only include the portions that I need but my boss needed 
this done yesterday and I'm learning as I go and developing the action 
plan as I go. I will be going back through all my scripts and cleaning 
them up once I make my boss happy.

The script where it was found is:
<?
    for ($z=3D2; $z<=3D4; ++$z):
  ?>
  <?
    $application_number=3D$z;
    include ("select_protection.php");
  ?>

The following is the referenced area in select_protection.php

/select other fields that the application was also applied to

$sql=3D"select activity.field_id
      from activity, protection_application
      where activity.activity_type_id=3D3
       and activity.occurance_id=3Dprotection_application.occurance_id
       and 
protection_application.application_number=3D$application_number
       and protection_application.same_as=3D$field_id";

$result=3Dexecute_query($sql);
$rows=3Dpg_numrows($result);
if ($rows=3D=3D0)$herbicide_other_fields=3D0;

 else
   {
     $p=3D0;
     while ($row =3D pg_fetch_array($result))
      {
        $herbicide_other_fields[$p]=3D$row["field_id"];
        file://echo $p .":" .$herbicide_other_fields[$p];
        $p++;
      }
   }




Message #2 by "Gellings, C.O." <gellingsco@p...> on Wed, 8 May 2002 01:27:33 +0200
This is a multi-part message in MIME format.

------=_NextPart_000_0002_01C1F62F.873D6C90
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

scalar means that is part of a scale i.e. from 0 to 10 or all uneven numbers
from 3 to 11 or a to z
in this case the scale is from 2 to 5
if $z = 2 or 3 or 4 or 5 (if $z>= 2 and $z<= 4 then $z = $z+1)

Grrrrrrrrrrrrrrr,

Carl
  -----Original Message-----
  From: Juliet May [mailto:jmay@s...]
  Sent: 08 May 2002 01:15
  To: beginning php
  Subject: [beginning_php] what is a scalar value?


  What should I be looking for when I get the following error message?

    Warning: Cannot use a scalar value as an array in
C:\Inetpub\wwwroot\agtrac\select_protection.php on line 126


  I'm more curious than worried because the portion of the script that it is
referencing isn't important for the page where it is included . I know I
should only include the portions that I need but my boss needed this done
yesterday and I'm learning as I go and developing the action plan as I go. I
will be going back through all my scripts and cleaning them up once I make
my boss happy.

  The script where it was found is:
  <?
      for ($z=2; $z<=4; ++$z):
    ?>
    <?
      $application_number=$z;
      include ("select_protection.php");
    ?>

  The following is the referenced area in select_protection.php

  /select other fields that the application was also applied to

  $sql="select activity.field_id
        from activity, protection_application
        where activity.activity_type_id=3
         and activity.occurance_id=protection_application.occurance_id
         and protection_application.application_number=$application_number
         and protection_application.same_as=$field_id";

  $result=execute_query($sql);
  $rows=pg_numrows($result);
  if ($rows==0)$herbicide_other_fields=0;

   else
     {
       $p=0;
       while ($row = pg_fetch_array($result))
        {
          $herbicide_other_fields[$p]=$row["field_id"];
          file://echo $p .":" .$herbicide_other_fields[$p];
          $p++;
        }
     }




Message #3 by "Nikolai Devereaux" <yomama@u...> on Tue, 7 May 2002 16:40:01 -0700
As an addendum to Carl's response, you should look at where you're using
arrays.

In particular, you're looping around a few times and either setting
$herbicide_other_fields to 0 or treating it like an array.  I don't see
anywhere that it's actually initialized as an array.

nik

Message #4 by "Juliet May" <jmay@s...> on Tue, 7 May 2002 17:56:28 -0600
Thanks for the help. It turns out a typo was my error. I had forgotten to
add the [0] to herbacide_other_fields when no rows were returned.

Now for the real question. If  I use the corrected code below have I
initialized my array? The data comes out of a database and I have no clue
before hand if or how many rows will be returned. Later when I use the
variable I start by testing if herbicide_other_fields[1] is set.:

$result=execute_query($sql);
$rows=pg_numrows($result);
if ($rows==0)$herbicide_other_fields[0]=0;

 else
   {
     $p=1;
     while ($row = pg_fetch_array($result))
      {
        $herbicide_other_fields[$p]=$row["field_id"];
           /echo $p .":" .$herbicide_other_fields[$p];
        $p++;
      }
   }

Message #5 by "Nikolai Devereaux" <yomama@u...> on Tue, 7 May 2002 17:18:50 -0700
> Thanks for the help. It turns out a typo was my error. I had forgotten to
> add the [0] to herbacide_other_fields when no rows were returned.

if you want an empty array, just set the variable to array();

> Now for the real question. If  I use the corrected code below have I
> initialized my array? The data comes out of a database and I have no clue
> before hand if or how many rows will be returned. Later when I use the
> variable I start by testing if herbicide_other_fields[1] is set.:

Well, you need to ask yourself how you want the data to look when you're
done.  If you expect to get multiple rows back from the DB, your array
should contain one row for each result row.  You shouldn't ever have to use
a num_rows function, really.  Do something like this:



$hof = array(); // short for herbicide_other_fields
while ($row = pg_fetch_array($result))
{
    $hof[] = $row['field_id'];
}


If you need to know the numerical index during the loop, use for() instead:

$hof = array(); // short for herbicide_other_fields
for($i = 0; false !== (($row = pg_fetch_array($result)); ++$i)
{
    $hof[$i] = $row['field_id'];
    echo "$i : $hof[$i] \n";

    // this is equivalent:
    echo "$i : $row[field_id] \n";
}

In both cases, the number of rows returned is count($hof);  In the second
case, you can also use $i.


take care,

nik


  Return to Index